Well I’ve put enough time and effort into my server configuration, mysql databases, and my web dev files that it was finally time to start backing up. Now I’m not completely crazy, the server does have a 3ware card in it for mirroring, but in the end its still a server in the closet (its not gay) and something bad could happen. So I spent about 2 hours writing a few backup scripts because I’m too poor for rsync and too lazy for amanda.
All you need is a cheap web host like the one I use (about $5 a month) and some time. Here’s what I did:
First create your FTP object so you can reuse for all your different PHP scripts. It will look something like this:
class C_FTP
{
/**
* ftpPut - uploads file to ftp server
* @params host, user, password, destFile, and localFile
* DEV-DATE: 5-11-07 CEN
*/
function ftpPut($host, $user, $password, $destFile, $localFile)
{
$ftp = ftp_connect($host);
ftp_login($ftp, $user, $password);
ftp_put($ftp, $destFile, $localFile, FTP_ASCII))
ftp_close($ftp);
}
}
Next determine what you want to backup. In this case we will just backup some important config files. So create a script to do that:
include_once('C_FTP.php');
exec("tar -cf /tmp/apache2-".date('y-m-d').".tar"." /etc/apache2/");
exec("gzip /tmp/apache2-".date('y-m-d').".tar");
exec("tar -cf /tmp/php5-".date('y-m-d').".tar"." /etc/php5/apache2/php.ini");
exec("gzip /tmp/php5-".date('y-m-d').".tar"."");
exec("tar -cf /tmp/postfix-main-".date('y-m-d').".tar"." /etc/postfix/main.cf");
exec("gzip /tmp/postfix-main-".date('y-m-d').".tar"."");
exec("tar -cf /tmp/webalizer-".date('y-m-d').".tar"." /etc/webalizer/");
exec("gzip /tmp/webalizer-".date('y-m-d').".tar"."");
$localAry[1] = '/tmp/apache2-'.date('y-m-d').'.tar.gz';
$localAry[2] = '/tmp/php5-'.date('y-m-d').'.tar.gz';
$localAry[3] = '/tmp/postfix-main-'.date('y-m-d').'.tar.gz';
$localAry[4] = '/tmp/webalizer-'.date('y-m-d').'.tar.gz';
$destAry[1] = 'backups/apache2-'.date('y-m-d').'.tar.gz';
$destAry[2] = 'backups/php5-'.date('y-m-d').'.tar.gz';
$destAry[3] = 'backups/postfix-main-'.date('y-m-d').'.tar.gz';
$destAry[4] = 'backups/webalizer-'.date('y-m-d').'.tar.gz';
// for some reason word press blows and chops off this for loop, you want your for loop $i integer to start at 0, then use the count() function to get how many elements are in the array, then loop through it incrementing one to $i each time and calling your FTP method to do the work.
for($i=0; $i
C_FTP::ftpPut('your-domain.com', 'username', 'password', $destAry[$i], $localAry[$i]);
}
Remember in these examples I am using PHP as a pure scripting language that is executed by a cronjob so you'll need to include #!/usr/bin/php -q on line 1 of your php files. Next create a cronjob that will execute the script so type crontab -e and determine when you want this to run. Since config files don't change too often once a month should be good, so that would look something like this.
30 5 2 * * /home/username/S_BackupConfigFiles.php
This will run at 5:30 AM on the second day of each month. Remember to save the crontab file. You'll probably want to execute the script manually first and verify everything works okay. And thats a backup system for the poor and lazy, definitely no where near enterprise level and you don't get incrementals or diffs with it, but hey, it works.
Next time I'll show you how to setup "system monitoring - for the poor and lazy." But its actually really cool, you'll see.