Search:  
Gentoo Wiki

Simple_snapshot_style_backup

This article is part of the Tips & Tricks series.
Terminals / Shells Network X Window System Portage System Filesystems Kernel Other

This is a simple implementation of the snapshot style rsync method of doing backups as described by Mike Rubel at http://www.mikerubel.org/computers/rsync_snapshots/.

Simply mount an external USB hard drive at /mnt/sda1, run this script, then unmount the drive.

Key Advantages:

File: snapshot.sh
 #!/bin/bash
  
 set -e
  
 DEST="/mnt/sda1"
 
 DIRS="/boot /etc /home /root /var/log /usr/portage/packages/All"
 #Or use this line for a full system backup
 #DIRS="/bin /boot /cvs /etc /home /lib /opt /root /sbin /usr /var"

 DATE=`date +%Y%m%d%H%M%S`
  
 initial()
 {
     # in the initial run, we perform a full copy.
     cd ${DEST}
     for DIR in ${DIRS}
     do
         echo "rsync ${DIR}"
         mkdir -p ${DATE}/${DIR}
         rsync --ignore-errors -a ${DIR}/ ${DATE}/${DIR}
     done
 }
  
 backup()
 {
     # in the normal run, we hardlink everything, and only copy changed files.
     cd ${DEST}
     PREV=`/bin/ls -dl current | rev | awk '{print $1}' | rev`
     echo "cp -al"
     cp -al ${PREV} ${DATE}
     for DIR in ${DIRS}
     do
         echo "rsync ${DIR}"
         mkdir -p ${DATE}/${DIR}
         rsync --ignore-errors -a --delete ${DIR}/ ${DATE}/${DIR}
     done
 }
  
 # check privileges
 if test "${USER}" != "root"
 then
     echo "I should be run with root privileges."
     exit 1
 fi
  
 # check mount point
 if test -e ${DEST}/NOT_MOUNTED
 then
     echo "${DEST} isn't mounted."
     exit 1
 fi
  
 # run backup
 if test -L ${DEST}/current
 then
     backup
 else
     echo "initial run.  this may take a while..."
     initial
 fi
  
 # update symlink
 cd ${DEST}
 rm -f current
 ln -s ${DATE} current
 
Retrieved from "http://www.gentoo-wiki.info/Simple_snapshot_style_backup"

Last modified: Thu, 04 Sep 2008 05:58:00 +0000 Hits: 13,391