xarst
March 27 '04, 09:47 AM
PHP backup script
by Adam LeVasseur [xarst]
This tutorial explains how to make a backup script that can be run with SSH or a cron job. Most *nix hosting companies let users create cron jobs for his directory. This script can't be run with a browser due to security reasons.
Be sure to have a lot of available space on the server, because a backup can be a lot of megs, even when this script bunzip2 them.
This script has limitations, like that he doesn't remove old backup files.
I'm planning to improve it to have it keep a max of "x" files in the folder where they are located.
You need to specify absolute paths in this script. If you don't the absoulute path, just paste this piece of code in a new file and call it "abspath.php". Upload it to your server. Then access it with a browser, and it will output the absolute path to this file.
<?php
echo getcwd();
?>
Simple, right?
Now we go with the backup script. I don't think I have to explain each part because it is very well commented. (All the lines that begin with two slashes [//] are ignored by the PHP parser and are only for human readibility.
<?php
$backupdate = date("Ymd");
//Backup date variable. Replace "Ymd" with
//"Ymd_H-i" to include the time.
$backupdir = "/the/absolute/path/to/the/folder/";
//Where are the files located?
$files = "*";
//What file to backup? Use a * to backup all the files
//inside the folder entered above.
$backupto = "/the/absolute/path/to/the/folder/";
//Where to store the tarball?
$fileprefix = "bak";
//This is the prefix that will be added before the date:
//(bak_20040326.tar.bz2)
//The underscore _ is added automatically
$tararg = "-cf";
//Here goes the tar arguments. I recommend -cf.
//c is for compressing. f is for outputting
//a file.
$bz2arg = "-z9";
//Here goes the bunzip2 arguments. I recommend -z9.
//z is for creating a archive
//and 9 is for max compression. z is always needed
//Call the function
backupsus();
function backupsus() {
global $backupdate,$backupdir,$backupto,
$fileprefix,$tararg,$bz2arg,$files;
$backupsuscmd = "cd $backupdir;
tar $tararg {$fileprefix}_{$backupdate}.tar $files;
bunzip2 $bz2arg {$fileprefix}_{$backupdate}.tar;
mv {$fileprefix}_{$backupdate}.tar.bz2 $backupto";
passthru ("$backupsuscmd");
}
?>
Paste this code in a file with a 'php' extension and upload it to your server.
If someone asks of how to create a cron job, I will extend the tutorial.
To run this script via SSH, go to the folder where it's located and type in:
php4 -q filename.php
Of course, replace 'filename' with the correct name.
If you need help with something related to this just post :)
by Adam LeVasseur [xarst]
This tutorial explains how to make a backup script that can be run with SSH or a cron job. Most *nix hosting companies let users create cron jobs for his directory. This script can't be run with a browser due to security reasons.
Be sure to have a lot of available space on the server, because a backup can be a lot of megs, even when this script bunzip2 them.
This script has limitations, like that he doesn't remove old backup files.
I'm planning to improve it to have it keep a max of "x" files in the folder where they are located.
You need to specify absolute paths in this script. If you don't the absoulute path, just paste this piece of code in a new file and call it "abspath.php". Upload it to your server. Then access it with a browser, and it will output the absolute path to this file.
<?php
echo getcwd();
?>
Simple, right?
Now we go with the backup script. I don't think I have to explain each part because it is very well commented. (All the lines that begin with two slashes [//] are ignored by the PHP parser and are only for human readibility.
<?php
$backupdate = date("Ymd");
//Backup date variable. Replace "Ymd" with
//"Ymd_H-i" to include the time.
$backupdir = "/the/absolute/path/to/the/folder/";
//Where are the files located?
$files = "*";
//What file to backup? Use a * to backup all the files
//inside the folder entered above.
$backupto = "/the/absolute/path/to/the/folder/";
//Where to store the tarball?
$fileprefix = "bak";
//This is the prefix that will be added before the date:
//(bak_20040326.tar.bz2)
//The underscore _ is added automatically
$tararg = "-cf";
//Here goes the tar arguments. I recommend -cf.
//c is for compressing. f is for outputting
//a file.
$bz2arg = "-z9";
//Here goes the bunzip2 arguments. I recommend -z9.
//z is for creating a archive
//and 9 is for max compression. z is always needed
//Call the function
backupsus();
function backupsus() {
global $backupdate,$backupdir,$backupto,
$fileprefix,$tararg,$bz2arg,$files;
$backupsuscmd = "cd $backupdir;
tar $tararg {$fileprefix}_{$backupdate}.tar $files;
bunzip2 $bz2arg {$fileprefix}_{$backupdate}.tar;
mv {$fileprefix}_{$backupdate}.tar.bz2 $backupto";
passthru ("$backupsuscmd");
}
?>
Paste this code in a file with a 'php' extension and upload it to your server.
If someone asks of how to create a cron job, I will extend the tutorial.
To run this script via SSH, go to the folder where it's located and type in:
php4 -q filename.php
Of course, replace 'filename' with the correct name.
If you need help with something related to this just post :)