Backup config

Written by Ren West on June 25th, 2004

Now that I’m hosting other peoples data, I decided perhaps I should try and create some better backups of things. This script can be called to backup any directory you want, and will store the backups on a local filesystem. I did it this way because I have 2 drives in my server, but it could be modified easily to send data to a remote host via ssh, or just use an NFS mount.

I got a lot of help from this page, which explains some of what I’ve done below. Feel free to use this and modify it to suit your needs. I assume no responsibility if it doesn’t work for you.

#!/bin/bash

# ———————————————————————-
# Creating rsync backups, then making daily copies of those backups
# ———————————————————————-
unset PATH

ID=/bin/id;
CP=/bin/cp;
MV=/bin/mv;
RM=/bin/rm;
RSYNC=/usr/bin/rsync;
BZIP=/bin/bzip2;
TAR=/bin/tar;

if [ ! $2 ] ; then
echo “Usage: `/bin/basename $0` SOURCE NAME_SET”
exit 1
fi

SOURCE=$1
NAME=$2
DEST=/root/temp/

# make sure we’re running as root
if (( `$ID -u` != 0 )); then { $ECHO “Sorry, must be root. Exiting…”; exit; } fi

# Step 1, rotate older backups, deleting the 6th backup, as its too old to care about
if [ -e ${DEST}/${NAME}.6.tar.bz2 ] ; then
$RM ${DEST}/${NAME}.6.tar.bz2
fi

if [ -e ${DEST}/${NAME}.5.tar.bz2 ] ; then
$MV ${DEST}/${NAME}.5.tar.bz2 ${DEST}/${NAME}.6.tar.bz2
fi

if [ -e ${DEST}/${NAME}.4.tar.bz2 ] ; then
$MV ${DEST}/${NAME}.4.tar.bz2 ${DEST}/${NAME}.5.tar.bz2
fi

if [ -e ${DEST}/${NAME}.3.tar.bz2 ] ; then
$MV ${DEST}/${NAME}.3.tar.bz2 ${DEST}/${NAME}.4.tar.bz2
fi

if [ -e ${DEST}/${NAME}.2.tar.bz2 ] ; then
$MV ${DEST}/${NAME}.2.tar.bz2 ${DEST}/${NAME}.3.tar.bz2
fi

if [ -e ${DEST}/${NAME}.1.tar.bz2 ] ; then
$MV ${DEST}/${NAME}.1.tar.bz2 ${DEST}/${NAME}.2.tar.bz2
fi

# This one is different. To save a little stress on the system being backed up,
# make a copy (cp -a) of the last ${NAME}, instead of moving it. This will allow
# rsync to only write the changes in the filesystem, rather than everything.
# Also, bzip2 the previous ${NAME} to save drive space.
if [ -d ${DEST}/${NAME}.0/ ] ; then
$CP -a ${DEST}/${NAME}.0/ ${DEST}/${NAME}.1/
if ( `$TAR cjf ${DEST}/${NAME}.1.tar.bz2 ${DEST}/${NAME}.1/` ); then { $RM -r ${DEST}/${NAME}.1/; } fi
fi

# Step 2, make a new backup, and call it ${NAME}.0/

$RSYNC –delete -avz ${SOURCE} ${DEST}/${NAME}.0/

Share and Enjoy:
  • Digg
  • Facebook
  • del.icio.us
  • Reddit
  • email
  • StumbleUpon
  • Google Bookmarks
 

Comments are closed.

Page optimized by WP Minify WordPress Plugin