Backing up strategy

I am tired to intervene with routine backups, so I wanted to automate everything. This  is accomplished by running cron jobs with a daemon called “crontab” .

1. each day run the following three programs.

1a: this backs all the web and user related data at /usr/home

#!/bin/sh
BDIR=week`date +%U`
COMPUTER=I-rock                                 # name of this computer
DIRECTORIES=”/home/”                        # directoris to backup
BACKUPDIR=/backup/                          # where to store the backups
TAR=/bin/tar                                # name and locaction of tar

PATH=/usr/local/bin:/usr/bin:/bin
DOW=`date +%a`                          # Day of the week e.g. Mon

# Weekly full backup when it is Sunday
if [ $DOW = “Sun” ]; then
NEWER=””
mkdir $BDIR
$TAR $NEWER -cpf $BACKUPDIR/$COMPUTER-$DOW.tar $DIRECTORIES

# other days, incremental only, only changes since Sun day of the week is backed up.
else
NEWER=”–newer-than”
$TAR $NEWER I-rock-Sun.tar -cpf $BACKUPDIR/$COMPUTER-$DOW.tar $DIRECTORIES
fi

1b: back up mysql database once a day and name it the day of week (*-Mon, *-Tue…etc).

cd /backup/
/usr/local/bin/mysqldump -u root -ppassword–all-databases >current.sql
gzip current.sql
mv current.sql.gz Mysql-`date +%a`.sql.gz

1c:  use rsync once a day to update all the newly backed files to a backup server.

2. weekly:

2a: run this script once a week Sat night, to move a full web backup (Sun), plus the last incremental (Sat), and a mysql dump, plus the named files,  to a directory named for the number of week for that year.

cd /backup/
mkdir week`date +%U` (create a directory called week-23, for 2nd week in June)
mv Mysql-Sat.sql.gz week`date +%U` (move Saturday’s database backup to week24, if it is the 24th week)
mv Named-master-Sat.tar week`date +%U` (move Named db files also)
mv I-rock-Sun.tar  week`date +%U` (move Sunday’s full backup, 6 days ago)
mv I-rock-Sat.tar week`date +%U` (move Saturday, the most recent incremental)

2b: on backup server, restore the databases once a week. Delay it for 5 days on purpose, in case something goes wrong with the database dump. Restore on Saturday, the Monday’s mysql backup.

cd /backup/
rm current.sql                                                                                    #remove last week’s sql file
mv Mysql-Mon.sql.gz current.sql.gz                                              #take monday and rename it
gunzip current.sql.gz                                                                       #unzip it
/usr/local/bin/mysql -h localhost -u root -ppassword <current.sql     #load it to mysql

So in the end, I have a snapshot of each week’s web, named db files, and mysql database saved. It is possible that I will run out of HD space after 30 weeks. so I will need to come up with a script to delete files that are more than 25 weeks old.

I may have to periodically move files to a third server.. just in case a fire in the building destroys both the main and backup servers (I know, this wont happen!).