Hello guys and ladies,
Long time no see
. Today I’m going to show you how to make a php script to perform full website backup the script can be executed via http request or via ssh shell or like a cron job .
We all know about the most standard way to add backup for your website maintenance for example using the Cron Jobs in the CPanel. If you already don’t know this – if your hosting provider does support CPanel you should be able to access it via this URL: www.yourwebsite.com/cpanel

After you login into you CPanel and choose ‘Cron jobs’ you can enter there manually script to fully backup your database or file system. For example a script for full file system backup should look like this:
tar -cvpzf my_backup_file.tar /home/your_user/public_html
we could make the script more interesting when instead of ‘my_backup_file’ we include the date and time in the filename:
tar -cvpzf `date +%y-%m-%d_%H-%M`.tar /home/ your_user/public_html
of course you should change `/home/your_user/public_html` to whatever your home directory is. You can find out what your home directory is by executing ‘pwd’ (print working directory) in the ssh shell or creating the file `exec.php` with the code:
<?php echo exec(‘pwd’); ?>
and after that executing the code by opening http://www.yoursite.com/exec.php in your browser.
So we can add this line directly into the Curl command field:

But now I am going to tell you better way to do it, because very often the commands directly typed at the cron’s `Command:` field are ignored because of some stupid quote convention etc.
Full website filesystem backup using php file and cron job:
1. First we are going to create the file backup_filesystem.php and put this code into it :
<?php echo exec(‘cd /home/your_user/backup/files;tar -cvpzf `date +%y-%m-%d_%H-%M`.tar /home/ your_user /public_html’); ?>
2. After that we’ll access the website via the FTP and put the backup_filesystem.php file into the /home/your _user/backup/. Make sure that you create “backup” and “backup/files” directories if they are not already created. Better don’t put backup_filesystem.php file into the /public_html/ folder because we don’t want the users to have direct access to the file.
3. Now we are going back to the CPanel -> Cron Jobs. On the common settings we enter Once a day (so the Command will be executed once a day) and on the Command field we enter:
php ‘the path to backup_filesystem.php’

After that click on the ‘Add new cron job’ button and Voila !! – the Cron Job for backup of your database is ready.
The archive is created using low tar compression, so if needed you can archive your site using stronger bzip2 compression by replacing: “tar –cvpzf filename.tar” with “tar –cjf tarname.bz2”.

The full backup of your website is created in the /backup/files directory and can be downloaded via FTP if necessarily – as you see the filename include the date and time of the moment it has been created, so it won’t be hard for you to guess which is the latest version of the backup if needed.
Thank you for your time, next time I’m going to show you how to perform similar backup, but this time full database backup instead of filesystem backup.
Cheers
June 21st, 2010 at 10:32 am
Very straightforward and useful. Thanks.
July 12th, 2010 at 12:56 am
Come on dude, these facts* and proof* i mean who is posting* lol
July 20th, 2010 at 7:15 am
nice list..really helpful.
August 18th, 2010 at 7:31 pm
Of course, what a great site and advisory posts, Can I add backlink – import your rss feed? Regards, Reader.
August 30th, 2010 at 5:22 pm
I truly love your blog. Excellent work!