Gossamer Forum
Home : General : Perl Programming :

Auto Backup script..

(Page 1 of 2)
> >
Quote Reply
Auto Backup script..
Just a new script I've been working on. Need some people to be beta-testers Tongue

Basic features;

+ You can add folders to backup...
+ You can add MySQL database's to backup...
+ You can set up the daily/weekly/monthly/annual backup cron automatically via a browser.
+ Automatic emailing of report after running..if turned on.

Coming Soon;

+ Code to verify that a username/password/database/host combination is valied before adding, and before an attempted backup.
+ Better regex to stop dodgy stuff being passed to the script
+ Verification code to see if the folder exists.
+ Lets you define where mysqldump is.

And whatever you can think of Tongue

Anyway...please find the script attached. You need to CHMOD the script to 755 (obviously), and the directory to 777, so that folders.pl and databases.pl can be created.

Well, if you find anything major, please let me know Pirate

Thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Auto Backup script.. In reply to
Hi Andy

I stopped reading after the first 9 lines.

use strict;
use warnings;

And if you're going to be dealing with user input, remember to turn on Taint checking with the T switch.

Oh, and never, ever, ever, leave use CGI::Carp qw(fatalsToBrowser); in a script that's in a production environment. Never. If anything goes wrong with your script, you're just going to outputting tons of useful information to whatever wants to hack you.

You should only output this kind of information if a DEBUG flag has been set from within your code.

Cheers

- wil

Last edited by:

Wil: Jun 25, 2002, 11:50 AM
Quote Reply
Re: [Wil] Auto Backup script.. In reply to
And if you're going to be using the OO interface of CGI.pm i.e. I see that you have:

$input = new CGI;

in your code, then you don't need to specify what to import here: use CGI qw(:standard);. Use CGI will do nicely if you're taking advantage of the OO interface.

- wil
Quote Reply
Re: [Wil] Auto Backup script.. In reply to
And if you are going to use CGI.pm, then let it handle the headers for you, too. It does a much cleaner job. Replace:

print "Content-type: text/html \n\n";

With

print $input->header;

- wil
Quote Reply
Re: [Andy] Auto Backup script.. In reply to
Hi,

I notice with some routines you use:

sub routine() {

and others...

sub routine {

...you do realise that using () is different?

eg...

sub FOO() { 1 }

...I think FOO then becomes a constant.

Last edited by:

Paul: Jun 25, 2002, 1:09 PM
Quote Reply
Re: [Paul] Auto Backup script.. In reply to
I thought

Code:
sub() {
something;
}


and

Code:
sub {
something;
}


were the same. They are both sub routines that just don't accept any input aren't they? They'd end up both being constants as there isn't any variables that would effect the output?
Cheers,
Michael Bray

Last edited by:

Michael_Bray: Jun 25, 2002, 8:37 PM
Quote Reply
Re: [Michael_Bray] Auto Backup script.. In reply to
Ok...heres an update. I'm using $input-header();, and got rid of the CGI qw(:standard);. I've added a new variable, called $debug, which when turned on, will enable fatalsToBrowser to work.

Well..I'm gonna get on with more stuff on it now Tongue

What is everyones general thoughts on the script, working wise?

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Wil] Auto Backup script.. In reply to
BTW: Wil, removing the use CGI qw(:standard); gives a major boo boo ;)

-----------
Software error:
Can't locate object method "new" via package "CGI" at /home/dumps/public_html/backup.cgi line 20.

For help, please send mail to the webmaster (support@focalnetworks.net), giving this error message and the time and date of the error.
------------

So I think I'll be putting that back in Wink

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Paul] Auto Backup script.. In reply to
Mmm..another great problem with the setting up of the cron Tongue I have used the $? and $! variable in checking to see if it was updated successfully (set_cron_2)...buty for some reason I keep getting the error returned: 256. This is really pissing me off...as I'm almost definate the code worked before!

Thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Auto Backup script.. In reply to
> BTW: Wil, removing the use CGI qw(:standard); gives a major boo boo ;)

Of course it does! What did you think would happen? I said you could drop the impoting of methods there, as they will be called into action when needed with the OO interface. In other words, you need to replace:

use CGI qw(:standard);

with:

use CGI;

Cheers

- wil
Quote Reply
Re: [Andy] Auto Backup script.. In reply to
use strict;
use warnings;

Honestly now, I'm not joking, you'll save yourself a lot of headaches and confusion if you add these in. They will make your life easier. They are there to help you. Do consider using them. They are your friend. :-)

I wish I started coding using use strict; instead of learning about it two years down the line. I blame the Matt's Script Archive myself for louzy coding, and therefore me copying louzy coding which doesn't help. Get into the habbit, it'll take you a fraction longer to code up in the first place but the benefits are immense.

- wil

Last edited by:

Wil: Jun 26, 2002, 2:51 AM
Quote Reply
Re: [Wil] Auto Backup script.. In reply to
How nice of you to reference me in your code, Andy. :-)

Code:
# get all the vars we wil need...

- wil
Quote Reply
Re: [Wil] Auto Backup script.. In reply to
LOL...updated typo ;)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Michael_Bray] Auto Backup script.. In reply to
Code:
I thought


--------------------------------------------------------------------------------
Code
--------------------------------------------------------------------------------


sub() { something; }

--------------------------------------------------------------------------------



and


--------------------------------------------------------------------------------
Code
--------------------------------------------------------------------------------


sub { something; }

--------------------------------------------------------------------------------



were the same. They are both sub routines that just don't accept any input aren't they? They'd end up both being constants as there isn't any variables that would effect the output?

sub { something } is an anonymous subroutine....you can pass in input like:

Code:
my $var = sub { return $_[0] x 5 };

print $var->(5);

Have a guess at what that prints :)

sub FOO() { 1 }

....becomes a constant meaning you could do something like:

Code:
if (FOO == 1) {
print "FOO is 1";
}

Last edited by:

Paul: Jun 26, 2002, 3:08 AM
Quote Reply
Re: [Paul] Auto Backup script.. In reply to
Mmm..this is rather odd. Is $ENV{'SCRIPT_FILENAME'} mean't to be accessable to a script when being run in Telnet, or is there another way to get the variable? For some reason, it works fine when being run in a browser, but not via Cron/SSH Frown

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Auto Backup script.. In reply to
Well seeing as you are using CGI.pm, use:

$input->url( { absolute => 1 } );

Last edited by:

Paul: Jun 26, 2002, 3:22 AM
Quote Reply
Re: [Andy] Auto Backup script.. In reply to
Andy just try adding strict to your script Wink

Your if ($debug) { use ... } is a bit dodgy too. You should use modules at the top of your script and then eval/require then after that point.

Also you are printing a header right at the top....so if you use telnet you'll see a header printed out.

>>
$current_dir = $ENV{'SCRIPT_FILENAME'}; $current_dir =~ s/backup.cgi//; # get the folder location of this script...
<<

What happens if someone renames the script?

Also you aren't escaping . which is a metacharacter so if I had a directory called:

backup-cgi

....that would be stripped.

>>$back_in .= $cron unless $cron =~ /^#/;<<

Or:

$back_in .= $cron unless substr($cron, 0, 1) eq '#';

substr is quicker.

>>$exists = -z "folder.pl" ? 1 : 0;<<

-z will return 1 if successfull anyway so:

$exists = -z "folder.pl";

...will do.

Last edited by:

Paul: Jun 26, 2002, 3:47 AM
Quote Reply
Re: [Paul] Auto Backup script.. In reply to
Ah..cheers for the CGI.pm variable thing Smile

>>>Your if ($debug) { use ... } is a bit dodgy too. You should use modules at the top of your script and then eval/require then after that point. <<<

Shouldn't have any problems with it. If they do, thats the whole point in a support forum, where I can just tell them to install that module, or remove that line Wink

>>>What happens if someone renames the script? <<<

Tough luck. I'll put something in the Readme telling them not to rename the script...unless you can think of a way around it Wink

>>>Also you aren't escaping . which is a metacharacter so if I had a directory called:

backup-cgi

....that would be stripped. <<<


Ah..I'll change that....thanks Smile

>>>Or:

$back_in .= $cron unless substr($cron, 0, 1) eq '#';

substr is quicker. <<<


I'm not sure it would make that much of a difference in this script. It is only likely to be a max of 100 lines in the cron file (unless they really DO love their cron's Tongue)...so I don't envisage this being a problem. I also prefer to understand the code myself...rather than just copying your stuff...LOL

>>>-z will return 1 if successfull anyway so: <<<

Fair enough. I'll loose the ? 1 : 0;

Thanks Smile

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Paul] Auto Backup script.. In reply to
In Reply To:
Well seeing as you are using CGI.pm, use:

$input->url( { absolute => 1 } );

I'm trying to work out how to make it show the full path, not just the absolute one. What site did you read up on the CGI.pm, absolute function? Looks like CGi.pm does a LOT more than I though Tongue

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Auto Backup script.. In reply to
Just remove { absolute => 1 }

>>
What site did you read up on the CGI.pm, absolute function?
<<

I didn't....it comes from spending hours snoopin at Jason's code.

Last edited by:

Paul: Jun 26, 2002, 6:54 AM
Quote Reply
Re: [Paul] Auto Backup script.. In reply to
>>>Just remove { absolute => 1 } <<<

Already tried it...that just gives a URL, and not a path Frown

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Auto Backup script.. In reply to
Ugh I thought you wanted a URL.

You can use $0 but that varies depending on how you call the script, ie....

perl test.cgi : $0 = test.cgi

perl /path/to/test.cgi : $0 = /path/to/test.cgi

Last edited by:

Paul: Jun 26, 2002, 7:08 AM
Quote Reply
Re: [Paul] Auto Backup script.. In reply to
At the moment the script is being called by cron with;

perl /path/to/file/backup.cgi

Unsure

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Auto Backup script.. In reply to
$0 will work then.
Quote Reply
Re: [Paul] Auto Backup script.. In reply to
Cheers...works great :)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
> >