Simple linux web-server information backup

Recently had some bad experience moving site to pre-production mode and accidentally (caused by software), all user information had partly lost and broken.

I had very unpleasant conversation with customer, so since that time I always setup backups for a new site.

Usually, when you dealing with ror project, it's very simple to backup everything by using backup_fu.

But we have no ruby and non ruby project, I've reviewed some freeware backup software and found it too difficult for such task. So let's write our own backup scripts manually.

Objectives for backup:
  • source code
  • data from database (mysql)
  • static files
Source backup is usually done by version control, so we'll skip this step.

According to database there is a great utility mysqldump.
First we need to mount another disc and let's mount it to /backups
The script is:

#!/bin/bash
echo Starting mysql backup `date`
mysqldump --complete-insert --skip-extended-insert --user=username --password="password" site > /backups/mysql_dump_`date "+%Y%m%d"`.sql
gzip /backups/mysql_dump_`date "+%Y%m%d"`.sql


Make file executable
host:~# chmod +x backup_mysql.sh


Then you need to setup cron to execute the script once a day or whatever you need.
host:~# crontab -e
0 1 * * * /root/backup_mysql.sh > backup.log

Lets move to static files content backup.
We have a lot of user files and we don't want to backup all static files everyday. (I didn't manage setup incremental backup for backup_fu) So we need some tool for incremental backup. For this task we'll use rdiff-backup utility.

apt-get install rdiff-backup

Create a script file backup_static.sh in /root and make it executable:

#!/bin/bash
echo Starting static files backup `date`
rdiff-backup /var/www/adultsgowild/_files /backups/static
echo Backup finished

chmod +x backup_static.sh

Add task to cron

host:~# crontab -e
0 1 * * * /root/backup_static.sh > backup.log

And that is actually it.