This is the #2 article in the backup topics, check out the first one : MySQL Archived Backup
Nowadays, nearly everyone / every company have a website of sorts. What most of us forgot is to back them up, in case of problems.
And as Murphy’s Law stated :
Backup of your data can save you from such occasion.
An archived-backup can help you further – should you need to retrieve certain version of your data, then you can. Because it saves multiple copies of your data, each from certain point of time.
A way to do this is by using tools like the excellent rdiff-backup. In short – it’s like Apple’s Time Machine, but can be easily scripted & automated.
For other cases, you may need to create your own script for it.
Below is a script we use to backup our customer’s website.
It will backup both their MySQL database and the actual website itself.
Note that the backup script will archive for 2 weeks.
If you need diffent archive period, please feel free to modify the script, following the example in this article.
Here’s the script : backup-website.sh
#!/bin/bash
### Please change the variables below to suit you
sourcedir="/home/myuser/web"
targetdir="/home/myuser/mybackup"
targetfile="myname"
datestr="`date +%A`"
# 2 months archive
#datestr="`date +%d`"
tempdir="/tmp/$targetfile-$datestr"
dbuser="root"
dbpass="mypass"
database="mydb"
#################################
bmkdir="/bin/mkdir"
btar="/bin/tar"
bbzip2="/bin/bzip2"
bcp="/bin/cp"
bmysqldump="/usr/bin/mysqldump"
################################## start backup now
### create temporary space
$bmkdir $tempdir
### backup database
$bmysqldump -u $dbuser --password=$dbpass $database > $tempdir/$targetfile-$datestr.mysql
$bbzip2 $tempdir/$targetfile-$datestr.mysql
### backup website
$btar cvzf $tempdir/$targetfile-web-$datestr.tar.gz $sourcedir
### 2 weeks archive
$bcp $targetdir/$targetfile-$datestr.tar $targetdir/$targetfile-last-$datestr.tar
### backup website + mysql database
$btar cvf $targetdir/$targetfile-$datestr.tar $tempdir/*
Enjoy 🙂