We always instruct our clients to backup their data before doing website security scanning – just in case… A fresh backup stored in a safe place could save your day sometime. Recently we have received a couple of queries from clients on how to quickly backup their websites.
Backups could be done in many ways. Various control panels (like Plesk or cPanel) provide their own tools. Your hosting provider may have some other tools readily available for you. However sometimes it’s wise to have your own backup script or other method for backuping a website. Here are some advantages:
- more granular control on what you backup and when you backup
- easy access to backup files – very easy do create a backup and restore it on your own PC (WAMP/LAMP environment)
- easy to restore individual files or the whole website
- no need to ask hosting provider for help on restoring website
In this post I will provide my method of doing backups. I’ve been using this script for many years on a couple of websites. It’s simple, informative, quick and it worked just fine.
One remark: you should have a right to create and modify cron jobs to run this script. If you are not sure look for “cron” or “cron jobs” signs in the control panel or ask your hosting provider.
First you need to know where you are going to store backup files. Usually it’s somewhere inside /private folder. Then you need to know the absolute paths to that folder and your website main folder. Absolute path is a full path inside server directory structure. There are many ways to find an absolute path. An easy method is to put a simple php script inside your website root and access it using web browser. Below are two scripts that could be used for this purpose. Copy the one of them to new file called path.php, uploads that file to the website root and access it using www.yourdomain.com/path.php.
First script:
<?php echo realpath(dirname(__FILE__)); ?>
and the other:
<?php echo getcwd(); ?>
When you know the absolute path you can use the bash scripts provided below for easy backuping and restoring of your websites. We put nicedomain.com as an example in the scripts. What you need to do is (on the backup script):
- change date format if needed – Line 4 (optional)
- change file name – Line 5 (optional)
- change the absolute paths to the folder you want to backup (Line 6) and where you want to store your backups (Line 7)
- change database name (DBNAME), username (USR) and password (PSW) – Line 19
If you are going to use restore script as well, change it accordingly.
Backup script:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # Set the parameters DATE=`date +%Y%m%d` FILE=nicedomain_com__SERVER__$DATE BackupDir="/home/nicedomain/public_html/" UploadDir="/home/nicedomain/private" echo Starting the backup of http://www.nicedomain.com \($DATE\): # Let's go to the folder where the backups will be stored cd $UploadDir # Starting files backup tar czf $FILE.tar.gz -C $BackupDir . echo File backup successful # Start MySQL database backup /usr/bin/mysqldump --opt -u USR -pPSW DBNAME > $UploadDir/$FILE.sql echo MySQL backup successful
Restore script
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # Set the parameters DATE=`date +%Y%m%d` FILE=nicedomain_com echo Starting to restore http://www.nicedomain.com \($DATE\): # Delete current data rm -r /home/nicedomain/public_html/* # Restore files tar -zxf /home/nicedomain/private/$FILE.tar.gz -C /home/nicedomain/public_html/ echo Files restored successfully # Restore MySQL mysql --user="USR" --pass="PSW" --host="localhost" DBNAME < /home/nicedomain/private/$FILE.sql echo MySQL restored successfully
That’s it. Now upload the script to the server and create a cron job pointing to the script, for example:
/var/www/vhosts/nicedomain.com/private/backups/backup.sh
You can schedule the job as you want – one time, daily, weekly, etc. Also when creating cron job make sure you provide your email address. When script finishes it will send an email with all the output (either successful or unsuccessful). This makes the tracking of backups very easy.
Whenever you want to test your hacking skills you need to find some websites to get your hands on. Be careful – don’t play with real sites! Either you should have permission (better – written) of the site owner to do so, or you should find another alternative.