Backup remotely via Perl
Ok, i guess you love that cheap servers only with FTP and HTTP, and minimal PHP4-Timeout and lame CGI and no real possibility but to do a good backup job. Well, Perl was once a dominant language and can do much for you.
The benefits of following backup solutions are as follows:
- Backup is done via calling an URL
- that invokes the mysqldump utility to backup your database to a local director, that is preferable outside the webroot
- the data is then gzipped
- old backups are automatically deleted
- and then backed up via FTP to another server
- and while a second server gets the backup it is more secure than having all on a single server
Put this in you cgi-bin folder and remotely call it via CURL or test it via the browser. Don’t forget tot set chmod 755 for the files! If you backup to a directory in the webroot don’t forget to protect that directory by e.g. setting a up an .htacess-file!
use CGI;
use POSIX qw(strftime);
use Time::Local;
$query = new CGI();
$file = $query->param(‘file’);
$filename = $query->param(‘filename’);
print "Content-Type: text/plain\n\n";
# print "Content-type: application/octet-stream\n";
# print "Content-Disposition: attachment; filename=".$filename."\n";
# print "Pragma: no-cache\n";
# print "bash ".$file;
sub backupMysql
{
my ($host) = $_[0];
my ($username) = $_[1];
my ($password) = $_[2];
my ($database) = $_[3];
my ($title) = $_[4];
$time = time;
$filename = $title."-".$database."-".strftime("%Y%m%d-%H%S", localtime($time));
print "Backup of \"".$title."\" (".$database.")…";
system "mysqldump -e –add-drop-table –add-locks –allow-keywords -h".$host." -u".$username." -p".$password." ".$database." > ../../backup/".$filename.".sql";
# print "GZip ".$filename."…\n";
system "gzip ../../backup/".$filename.".sql";
# print "Unlink ".$filename.".sql…\n";
unlink "../../backup/".$filename.".sql";
print "done.\n";
}
# Delete previous backups
if (-d "../backup/") {
$where = "../backup/";
while (defined($next = <$where/*.sql.gz>)) {
unlink($next);
}
}
backupMysql("<em>host</em>", "<em>username</em>", "<em>password</em>", "<em>database</em>", "<em>mybackupname</em>");
Optional .htaccess-File:
Deny from all
</Files>
Then call the URL by cronjob:
remoteAddress="<em>ftp://user@yourftp.com</em> -p <em>password</em>"
localAddress="<em>/home/Backup/mybackupname</em>"
timeThreshold=15
mirrordirOptions="-v –keep-files –no-chown –no-chmod –num-backups 30 –mtime-threshold $timeThreshold "
echo "Backup to $localAddress — `date +"%a, %C.%B %Y"`"
echo -n "[`date +%H:%M:%S`] Killing runnung instances of mirrordir…"
/usr/bin/killall -9 mirrordir &2>1 > /dev/null
echo "done."
mkdir -p $localAddress/htdocs > /dev/null
echo "[`date +%H:%M:%S`] Backup of relevant databases…"
curl http://www.yourdomain.com/cgi-bin/backup/mysql.cgi
echo "done."
echo -n "[`date +%H:%M:%S`] Backup of remote FTP…"
/usr/bin/mirrordir $mirrordirOptions $remoteAddress $localAddress/htdocs
echo "done."
echo "[`date +%H:%M:%S`] Finished."
If you don’t have mirrordir or curl install it via aptitude install mirrordir curl on your debian box.