Skip to content

Instantly share code, notes, and snippets.

@henning
Created June 30, 2012 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henning/3021728 to your computer and use it in GitHub Desktop.
Save henning/3021728 to your computer and use it in GitHub Desktop.
simple snapshot backup bash script
#!/bin/bash
set -e
set -u
DEBUG_NOOP=""
# simple snapshot script for raodwarrior systems to make a snapshot of (nearly) the whole
# filesystem, plus some extras like package lists
BASEDIR=/media/road-backup
NO_SNAPSHOT_BASEDIR=$BASEDIR/mirror-only
SNAPSHOT_BASEDIR=/media/road-backup/snapshots
DATESTAMP=$(date +%Y-%m-%d)
SNAPSHOT_TARGET=${SNAPSHOT_BASEDIR}/$DATESTAMP
LATEST_LINK=$SNAPSHOT_BASEDIR/latest
SIMPLE_COPY_DIRS="
/data/produktion/run
/data/produktion/tmp
/data/media
"
# system stuff that might be enough to have only a single copy from:
#/usr
#/lib
#/sbin
#/bin
#/boot
#/root
#/run
# Excludes: start with / from the source. otherwise all directories anywhere with such a named are omitted
EXCLUDES="
/dev
/home/henning/.gvfs
/home/henning/data
/lost+found
/media
/misc
/mnt
/net
/proc
/selinux
/sys
/tmp
/welt
$SIMPLE_COPY_DIRS
"
EXCLUDE_FILE=$(mktemp --suffix snapshot-script-exclude)
# when using an exclude list from a single line separated by spaces, ensure it gets into a file line by line
echo $EXCLUDES | sed -e 's/ /\n/g' > $EXCLUDE_FILE
echo using exclude list $EXCLUDE_FILE
cd /root
[ ! -d backups ] && mkdir backups
echo make backup of myself
cp $BASEDIR/simple-snapshot.sh $BASEDIR/simple-snapshot.sh.bak
dpkg --get-selections > backups/dpkg-selections_${DATESTAMP}.info
# mysql snapshot if there is a server on the local machine!
if [ -d /var/lib/mysql ]; then
mysqldump -u root -A > backups/mysqldump_${DATESTAMP}
fi
if ! [ -d $SNAPSHOT_TARGET ]; then
mkdir -p $SNAPSHOT_TARGET
fi
$DEBUG_NOOP rsync -a --delete --link-dest $LATEST_LINK/ --exclude-from $EXCLUDE_FILE / $SNAPSHOT_TARGET/
if [ -f $LATEST_LINK ]; then
rm $LATEST_LINK
fi
ln -s $DATESTAMP $LATEST_LINK
## stuff we don't want snapshots for, just a safety copy
if [ ! -d ${NO_SNAPSHOT_BASEDIR} ]; then
echo "we don't have basdir ${NO_SNAPSHOT_BASEDIR} for non-snapshot backups. Work finished."
exit 0
fi
for dir in $SIMPLE_COPY_DIRS; do
echo make copy of $dir
TARGET_DIR=${NO_SNAPSHOT_BASEDIR}/${dir}
if [ ! -d $TARGET_DIR ]; then
$DEBUG_NOOP mkdir -p ${TARGET_DIR}
fi
$DEBUG_NOOP rsync -a --delete --ignore-errors ${dir}/ ${TARGET_DIR}/
done
if [ -d /media/externmedia ]; then
time rsync -a --delete /media/externmedia/ $BASEDIR/externmedia/
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment