Created
July 21, 2010 00:44
-
-
Save greyson/483864 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
die() { | |
echo "$*" >&2 | |
exit 1 | |
} | |
# | |
# Determine dump level based on what day it is: | |
# | |
# 0: Yearly (Jan 1) | |
# 1: Monthly (FMAMJJASOND 1) | |
# 2-8: Sunday -> Saturday | |
# 9: UNUSED (user checkpoint) | |
# | |
default_level() { | |
# First get the desired incremental dump | |
DUMP_LEVEL=$(expr `date +%w` + 2) | |
# Monthly backups happen on the first of each month | |
if test `date +%-d` = 1; then | |
DUMP_LEVEL=1 | |
# Yearly backups occur on the first of January. | |
if test `date +%-m` = 1; then | |
DUMP_LEVEL=0 | |
fi | |
fi | |
echo $DUMP_LEVEL | |
} | |
# | |
# For each backup, create the file that will be the gzip'd dump of the | |
# filesystem, change the permissions to disallow reading, and then dump the | |
# filesystem into it with an inline gzip | |
# | |
dump_filesystem() { | |
MOUNTPOINT=$1 | |
DEVICE=`mount | awk '{if($3 == "'${MOUNTPOINT}'") {gsub( /[^\/]*\//, "", $1 ); print $1} }'` | |
DUMP="$path/dump_${DEVICE}.gz" | |
echo "Backup of ${MOUNTPOINT} (${DEVICE})" | |
touch $DUMP.tmp | |
chmod 400 $DUMP.tmp | |
/sbin/dump -${LEVEL}uLf - /dev/${DEVICE} | gzip -c > $DUMP.tmp | |
mv $DUMP.tmp $DUMP | |
} | |
fl=`date '+%Y-%m-%d'` | |
host=`hostname -s` | |
path="/backup/dumpfs/${host}/${fl}" | |
PROG_NAME=`basename "$0"` | |
LEVEL=`default_level` | |
while getopts l: OPTION; do | |
case ${OPTION} in | |
l) | |
LEVEL=${OPTARG} | |
;; | |
\?) | |
echo "Usage ${PROG_NAME} [-l dump_level]" >&2 | |
exit 2 | |
;; | |
esac | |
done | |
# If there is already a backup in this location, abort | |
if [ -f $path/COMPLETE ]; then | |
DONE_LEVEL=`cat $path/LEVEL` | |
die "A level $DONE_LEVEL backup is already present for today." | |
fi | |
echo "Performing a level ${LEVEL} dump" | |
# Make sure something is mounted to the backup directory | |
if { mount | grep -q /backup; }; then | |
echo "Found /backup filesystem." | |
else | |
mount /backup || \ | |
die "No filesystem mounted to /backup" | |
echo "Mounted /backup" | |
fi | |
# Make the directory to store this backup | |
mkdir -p "$path" | |
# Indicate the level of the dump. | |
echo ${LEVEL} > $path/LEVEL | |
# Collect filesystems to be dumped | |
DUMPS=`grep -Ev '^\s*#' /etc/fstab | awk '{ if( $5 > 0 ) { print $5, $2 } }' | sort -n | awk '{print $2}'` | |
for fs in $DUMPS; do | |
dump_filesystem $fs | |
done | |
# Indicate that we successfully completed the dumps | |
touch $path/COMPLETE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment