PDA

View Full Version : PHP backup script


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 :)

xarst
March 27 '04, 11:55 AM
Part 2

I've decided to post here a continuation explaining how to edit the crontab and to make it compartible with the vast major hosting companies.

1
First we are going to create the shell script. This isn't needed by some companies, but some do need it. I had to create it on my 1and1 hosting.
Create a file called backup.sh, and open it with notepad or similar.

2
Paste this code to it:
PATH=/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin
export PATH
cd /path/to/the/folder/where/the/script/is
php4 -q filename.php

Explaining:
The first line sets the path to the needed programs for the backup script to work: tar,mv and bunzip2.
The second line exports the path to be available to the backup script.
The third line goes to the folder where the backup script created on the post above is. Change it to the correct path/
The fourth line simply runs the backup script with the PHP parser. Change 'filename.php' to the one you created in the post above.
Now upload this file to your server.

3
Now we are going to set up the crontab file. You need to have SSH access to do this.
When in the SSH prompt, type in:crontab -e
Now type in this:* * * * * sh /pathto/the/shell/script/created/above/backup.sh > /dev/null
See the five stars?
The first one stand for minute(0-59)
The second for hour(0-23)(23 = 11pm)
The third for day of the month(1-31)
The fourth for month of the year(1-12)
And the last for day of the week(0-6)(0 = sunday)
This is the time it will be run. As example, this is what you have to put where the stars are:
0 16 * * 1
This will run the backup script at 4pm every monday.
30 0 * * *
This will run the script every day at 0:30 of midnight)

4
Now we save the crontab, like this:
Hit escape to exit the "insert" mode, and type:
:qThis saves and exit.
Now just sit and relax, as at the hour you have chosen to run the script.

Test if it works by going to the folder where the backup.sh is, and typing in:./backup.sh
If it just takes a while and doesn't say anything, it has worked!

computerwiz5
September 20 '04, 05:12 PM
Hey,

What i need to achieve withour script is to backup my htdocs folder. I want to copy it and reaname it into my root directory (above htdocs) Here is the break up:

WWW -> Backup, Htdocs, temp, etc,

Then i have frm htdocs the folder i want to diplicate and rename to the date and be inserted into my backup directory, in the folder called htdocsbak that is above my htocs and name the folder the date it was backuped on. I want this to happen every 3 days.

Can you help me out?

Thanks,
Danny

Bob_G7
January 1 '05, 02:53 PM
But when I attempt to untar the output after ftping to my PC I get this error using WinRAR.

! C:\Documents and Settings\xxxxx\Desktop\bak_20050101.tar.bz2: Read error in the file C:\Documents and Settings\xxxx\Desktop\bak_20050101.tar.bz2

Here is the PHP file I made:
<?php
$backupdate = date("Ymd");
//Backup date variable. Replace "Ymd" with
//"Ymd_H-i" to include the time.

$backupdir = "/home/www/mydomain.com/";
//Where are the files located?

$files = "*";
//What file to backup? Use a * to backup all the files
//inside the folder entered above.

$backupto = "/home/www/mydomain.com/system_backup/";
//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");
}
?>

Hoping you can tell me what may be wrong.

Thanks in advance,

Bob G.

kenny
October 4 '05, 12:14 PM
Just try to rename the file prior to unzip it.

Enjoy !

nodorizzi
May 3 '07, 12:47 PM
Bringing back an old thread. How can I use this without having it make it a zip file. Like I want it to copy a file each day and just name it to a date and have it copied to a different folder. Kinda like an archive.

leprechaun13
May 7 '07, 09:40 AM
having a problem is there a way to get this to work without passthru?