Ciao a tutti,
avevo bisogno di uno script che, eseguito tramite cron, facesse il back-up di una directory ogni x ore.
Ne ho trovato uno che fa proprio al caso mio, soltanto che quando crea il file compresso, gli attribuisce un nome tipo backup2-29-2004.tar.gz.
Quindi, se lancio lo script piu' volte al giorno, il nuovo sovrascrivera' quello vecchio, finche' non e' passata la mezzanotte.
Vorrei semplicemente fare in modo che, al nome, aggiunga oltre a mese, giorno e anno, aggiunga anche ore e minuti.
Di seguito il codice. E' fattibile?
codice:
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
###############################################
##
## World Wide Backup Copywright 2001 WORLD WIDE CREATIONS www.worldwidecreations.com
##
## You are free to use this software any way you wish provided you follow the following terms:
## That you cannot use the source in any script/software you redistribute or sell in any way.
## You can NOT redistribute this software in any way.
##
## For complete licensing terms, please contact us at our website at www.worldwidecreations.com
##
## This software was created on October 4th 2001.
##
## This software is intended to create a backup file called backup.tar.gz on your server
## then the software will FTP this software anywhere you wish, providing you have
## access to the said FTP server. You can also choose NOT to FTP the backup file
## if all you wish is to use this to create a backup file.
##
## You can choose to timestamp the backups.
##
## This software was tested on a Linux platform with a GNU version of Tar
## You may need to modify the code slightly if your system will not use gzip.
##
## You can crontab this script, but you should make sure you do not set the cronjob
## too frequently as this could use up your bandwidth quickly if you set a cronjob to
## execute too often. You should test this script first without a cronjob and make sure everything is working
## correctly, before you set this up as a working backup.
##
## A weekly crontab would look something like:
##
## 0 0 * * 1 perl /path/to/backup.pl
##
## This would execute the script every Monday.
##
## Dont forget to define your Path to Perl and to CHMOD this 755.
##
## This script is not designed to run as a CGI WEB script.
## you can attempt it if you wish, but with a large backup your browser will likely
## time out. Use telnet or crontab instead.
##
## Support can be found in our forum at www.worldwidecreations.com
##
###############################################
#Path to the directory you want to be as your base directory to back up.
#All files AND directories off of this path will be archived. NO TRAILING SLASH
$basepath = "/absolute/path/to/directory";
##Where you want the backup.tar.gz file to be placed (not where it will be FTP'd). NO TRAILING SLASH!
$tarpath = "/absolute/path/to/storage/directory";
##If you want to timestamp the filename, select this as 1, otherwise 0;
$date_yes = 1;
##If you DONT want the script to FTP this somewhere then check this 1, otherwise leave as 0
$just_back_up = 0;
#If you want the backup file deleted on the SOURCE server after FTP quits, check this as 1;
$delete_source = 1;
##FTP server
$ftpserver = "yourftpserver.com";
##FTP username
$ftpusername = "yourftpusername";
##FTP password
$ftppass = "ftppassword";
##CWD directory, in other words, when the script logs into the FTP server, where would you like the backup.tar.gz file to be placed
$cwdd = "cgi-bin";
##want to be notified of transfer status by email (sendmail required)?
$notify = 1;
#path to sendmail
$sendmail = "/usr/lib/sendmail";
##email address:
$to = 'youremail@address.com';
##Who will the email address be from
$from = 'from@address.com';
##############################NO MORE EDITING REQUIRED ####################################
use Net::FTP;
$hitch = ();
($day, $month, $year) = (localtime)[3,4,5];
$year = $year + 1900; $month++;
if ($date_yes) { $hitch = "$month-$day-$year"; }
print "ARCHIVING FILES\n";
@the = `tar --exclude backup$hitch.tar.gz -czf $tarpath/backup$hitch.tar.gz $basepath`;
@mat = stat("$tarpath/backup$hitch.tar.gz");
if ($mat[7] <= 0) { print "Backup didnt happen. The error returned by the system (if any) is @the\n"; &send("Backup FAILED. Tar did not backup any files. Error message returned from server (if any) is @the", "Backup FAILED"); exit; }
print "Files Archived to backup$hitch.tar.gz\nFile Size Is $mat[7] Bytes\n";
if ($just_back_up) {
&send("Backup success. Backup size $mat[7] bytes", "Backup Success");
print "Ok, files are backed up, No FTP requested. Shutting down\n";
exit;
}
print "Connecting into server\n";
if ($ftp = Net::FTP->new("$ftpserver", Debug => 0)) { print "Logging in to server\n"; } else { print "could not find server! Exiting\n"; &send("Backup FAILED. Could NOT connect to $ftpserver", "Backup FAILED"); exit; }
if ($ftp->login("$ftpusername","$ftppass")) { print "Username and Password ACCEPTED\n"; } else { print "could not log into server with Username and Password. Exiting!\n"; &send("Backup FAILED. Could NOT connect to $ftpserver with the Username And Password provided - Authentication error.", "Backup FAILED"); exit; }
if ($ftp->cwd("$cwdd")) { print "Changed to the directory without any trouble - UPLOADING\n"; } else { print "Could not change to the appropriate directory, uploading anyway\n"; }
if ($ftp->binary) { print "Changed to Binary Mode No Problem\n"; } else { print "Could not change to binmode $!\n"; exit; }
if ($ftp->put("$tarpath/backup$hitch.tar.gz")) { print "Everything appears ok with the upload\n"; } else { print "Can not upload file $!\n EXITING\n"; &send("Backup FAILED. Could NOT upload to $ftpserver. Error message returned from server is: $1", "Backup FAILED"); exit; }
$ftp->quit;
if ($delete_source) {
print "You chose to delete the source after FTP, deleting $tarpath/backup$hitch.tar.gz...";
unlink("$tarpath/backup$hitch.tar.gz") or print "COULD NOT DELETE! $!";
print "\nDeleted!\n";
}
print "File Transferred OK!\nFinished\n";
if ($notify) {
&send("Backup success. Transferred $mat[7] bytes to $ftpserver", "Backup Success");
}
sub send {
if ($notify) {
open MAIL, "|$sendmail -t" or print "Could not open Sendmail $!\n";
print MAIL "To: $to\nFrom: $from\nSubject: $_[1]\n\n$_[0]\n\n.\n";
close MAIL;
}
}