#!/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 ## ## Several slight modifications made by Jon Whiting. ## ############################################### #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 #Example: /files/homeX/username $basepath = ""; ##Where you want the backup.tar.gz file to be placed (not where it will be FTP'd). NO TRAILING SLASH! #Example: /files/homeX/username/backups $tarpath = ""; ##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 = ""; ##FTP username $ftpusername = ""; ##FTP password $ftppass = ""; ##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 #Example: /htdocs/ccgi-backups - make sure this folder is created on the ftp server. $cwdd = ""; ##want to be notified of transfer status by email (sendmail required)? $notify = 1; #path to sendmail $sendmail = "/usr/lib/sendmail"; ##email address: $to = ''; ##Who will the email address be from $from = ''; ##############################NO MORE EDITING REQUIRED #################################### use Net::FTP; $hitch2 = (); $hitch = (); ($day, $month, $year) = (localtime)[3,4,5]; $year = $year + 1900; $month++; $hitch2 = "$day-$month-$year"; if ($date_yes) { $hitch = "$month-$day-$year"; } print "ARCHIVING FILES\n"; @the = `tar --exclude backup.$hitch2.tar.gz -czf $tarpath/backup.$hitch2.tar.gz $basepath`; @mat = stat("$tarpath/backup.$hitch2.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.$hitch2.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.$hitch2.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.$hitch2.tar.gz..."; unlink("$tarpath/backup.$hitch2.tar.gz") or print "COULD NOT DELETE! $!"; print "\nDeleted!\n"; } print "File Transferred OK!\nFinished\n"; if ($notify) { &send("Daily Backup success. Transferred $mat[7] bytes to $ftpserver", "Daily Backup Success $hitch2"); } 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; } }