Gossamer Forum
Home : General : Perl Programming :

Backup script Cannot open zip backups

Quote Reply
Backup script Cannot open zip backups
It seems to create a backup although I get a server error when I run this script, but when I download the tar.gz backup and try to open it with winzip I keep getting errors like no directory structure or unable to decompress if somone could take a look at this and mybe see why it shows a server error when run, again, it sends the email and does make a currupted copy it could really come in handy here it is. please post what could be wrong

#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);

#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 = "/home/virtual/site2/fst/var/www/html/test";

##Where you want the backup.tar.gz file to be placed (not where it will be FTP'd). NO TRAILING SLASH!
$tarpath = "/home/virtual/site2/fst/var/www/html/backup";
##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 = 1;

#If you want the backup file deleted on the SOURCE server after FTP quits, check this as 1;
$delete_source = 0;

##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
$cwdd = "";

##want to be notified of transfer status by email (sendmail required)?
$notify = 1;

#path to sendmail
$sendmail = "/usr/sbin/sendmail";

##email address:
$to = 'test@your email address.net';

##Who will the email address be from
$from = 'server';

##############################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 Passwor

d 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->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", "Back

up 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;
}
}

Last edited by:

ebinc: Nov 24, 2002, 5:14 PM