Monthly Archives: March 2012

Archive-Backup Your Website

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 :

If it can go wrong, it will go wrong – in the worst possible time

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 🙂

MySQL Archived Backup

I regard backup as a very important matter. Because one of the certainties in life is this :

Computer WILL Fail

However, with so many computers in our care, we need a way to make it automatic. Otherwise, it will simply take too much time.

Thankfully, this is very easy to do in UNIX / Linux computers.

We have several kind of backup scripts. All of them are quite simple, but essential. Nobody should be without them.

Below you can find a backup script we use to backup MySQL database for our clients. Basically, this is how we use it :

  1. Modify the script to archive for the time range that we’d like.
    The script can archive the backup from 1 week to unlimited. Just remark out the time range that you don’t need (by putting the “#” character in the first column of the particular row).
  2. Make sure we can run mysqldump automatically / with no user intervention.
    This can be done easily by creating a file name ~/.my.cnf
  3. Setup the backup to run automatically via cron
  4. Routinely backup the backup to other servers.
    Yes, there is no such thing as “too much backup”.

Without further ado, here is the script : backup-archive-mysql.sh


#!/bin/bash

### Please change the variables below to suit you
targetdir="/home/myuser/mybackup"
targetfile="db-mydb"
targetsuffix=".mysql"

userdb="root"
database="mydb"

#################################
bcp="/bin/cp"
bmysqldump="/usr/bin/mysqldump"

################################## start backup now
### Archive : 1 week
$bmysqldump -u $userdb $database > $targetdir/$targetfile-`date +%A`$targetsuffix

### Archive : 2 weeks
$bcp $targetdir/$targetfile-`date +%A`$targetsuffix $targetdir/$targetfile-last-`date +%A`$targetsuffix
$bmysqldump -u $userdb $database > $targetdir/$targetfile-`date +%A`$targetsuffix

### Archive : 1 month
$bmysqldump -u $userdb $database > $targetdir/$targetfile-`date +%d`$targetsuffix

### Archive : 2 months
$bcp $targetdir/$targetfile-`date +%d`$targetsuffix $targetdir/$targetfile-last-`date +%d`$targetsuffix
$bmysqldump -u $userdb $database > $targetdir/$targetfile-`date +%d`$targetsuffix

### Archive : 1 year
$bmysqldump -u $userdb $database > $targetdir/$targetfile-`date +%m%d`$targetsuffix

### Archive : 2 years
$bcp $targetdir/$targetfile-`date +%m%d`$targetsuffix $targetdir/$targetfile-last-`date +%m%d`$targetsuffix
$bmysqldump -u $userdb $database > $targetdir/$targetfile-`date +%m%d`.mysql

### No Archive
### WARNING: always monitor your free disk space, or the following line may consume them all
$bmysqldump -u $userdb $database > $targetdir/$targetfile-`date +%Y%m%d`$targetsuffix

Here is a sample of ~/.my.cnf; if this file exist, then various MySQL tools & software will be able to run with no intervention.
Of course you’ll need to adjust its user & password :


[client]
user=root
password=mypassword

Hope you’ll find this useful.
I’ll post our other scripts here from time to time as well. Stay tuned.