use f00king functions!

use ALL and EVERY backup (daily.1 to daily.$$MAX_ROTATE) for --link-dest
This commit is contained in:
Vincent Riquer 2011-10-08 03:42:59 +02:00
parent bfa5c980a1
commit caa179efa4

View File

@ -60,8 +60,20 @@ then
fi
###END: generic tool checker
for config in /etc/remote-backup/*/config
while getopts 'e:h' opt
do
case $opt in
e)
EMERG="$OPTARG"
;;
h)
help
;;
esac
done
doBackup() {
config="$1"
if [ ! -e "$config" ]
then
echo "No backups defined, dying." >&2
@ -70,16 +82,7 @@ do
source /etc/remote-backup/defaults.conf
source "$config"
echo "Debut de backup de ${REMOTE_LOCATION} vers ${SNAPSHOT_RW}/${NAME}/ a $(date)"
# ------------- the script itself --------------------------------------
# make sure we're running as root
if [ ! $UID -eq 0 ]
then
echo "Sorry, must be root. Exiting..."
exit
fi
echo "Debut de backup de ${REMOTE_LOCATION} vers ${SNAPSHOT_RW}/${NAME}/ a $(date +"%H:%M, le %d/%m/%Y")"
if [[ "$REMOUNT" == "true" ]]
then
@ -87,8 +90,8 @@ do
$MOUNT -o remount,rw "$MOUNT_DEVICE" "$SNAPSHOT_RW" ;
if (( $? ))
then
echo "snapshot: could not remount $SNAPSHOT_RW readwrite"
exit
echo "snapshot: could not remount $SNAPSHOT_RW readwrite" >&2
return 1
fi
fi
@ -97,8 +100,31 @@ do
mkdir -p "$SNAPSHOT_RW/$NAME"
fi
# rotating snapshots of /home (fixme: this should be more general)
if [[ "$2" == emerg ]]
then
runBackup emerg.$(date +%Y%m%d%H%M)
else
rotateBackup
runBackup daily.0
fi
# now remount the RW snapshot mountpoint as readonly
if [[ "$REMOUNT" == "true" ]]
then
$MOUNT -o remount,ro "$MOUNT_DEVICE" "$SNAPSHOT_RW"
if (( $? ))
then
echo "snapshot: could not remount $SNAPSHOT_RW readonly"
exit
fi
fi
echo "Fin de backup de ${REMOTE_LOCATION} vers ${SNAPSHOT_RW}/${NAME}/ a $(date +"%H:%M, le %d/%m/%Y")"
echo '-----------------------------------------------------------------------'
}
rotateBackup() {
# rotating snapshots
if [ -f "$SNAPSHOT_RW/$NAME/daily.0" ]
then
echo "Previous backup failed, skipping rotation."
@ -120,42 +146,56 @@ do
fi
done
fi
}
runBackup() {
# step 3: rsync from the system, linking to latest snapshot if files are
# identical.
RSYNC_OPTS="-a \
--delete \
--delete-excluded \
--exclude-from=$EXCLUDES \
--bwlimit=$BWLIMIT"
RSYNC_OPTS="-a \
--exclude-from=$EXCLUDES \
--bwlimit=$BWLIMIT"
if [ -d "$SNAPSHOT_RW/$NAME/daily.1/" ]
then
RSYNC_OPTS="$RSYNC_OPTS \
--link-dest=$SNAPSHOT_RW/$NAME/daily.1/"
fi
for((backup_num=0;backup_num <= MAX_ROTATE;backup_num++))
do
if [ -d "$SNAPSHOT_RW/$NAME/daily.$backup_num" ]
then
RSYNC_OPTS="$RSYNC_OPTS \
--link-dest=$SNAPSHOT_RW/$NAME/daily.$backup_num"
fi
done
for old_backup in "$SNAPSHOT_RW/$NAME/emerg".*
do
if [ -d "$old_backup" ]
then
RSYNC_OPTS="$RSYNC_OPTS \
--link-dest=$old_backup"
fi
done
$RSYNC \
$RSYNC_OPTS \
"${REMOTE_LOCATION}" \
"$SNAPSHOT_RW/$NAME/daily.0"
$RSYNC \
$RSYNC_OPTS \
"${REMOTE_LOCATION}" \
"$SNAPSHOT_RW/$NAME/$1"
# step 5: update the mtime of daily.0 to reflect the snapshot time
$TOUCH "$SNAPSHOT_RW/$NAME/daily.0"
$TOUCH "$SNAPSHOT_RW/$NAME/$1"
# and thats it for home.
# and thats it
}
# now remount the RW snapshot mountpoint as readonly
if [[ "$REMOUNT" == "true" ]]
then
$MOUNT -o remount,ro "$MOUNT_DEVICE" "$SNAPSHOT_RW"
if (( $? ))
then
echo "snapshot: could not remount $SNAPSHOT_RW readonly"
exit
fi
fi
# make sure we're running as root
if [ ! $UID -eq 0 ]
then
echo "Sorry, must be root. Exiting..."
exit
fi
echo "Fin de backup de ${REMOTE_LOCATION} vers ${SNAPSHOT_RW}/${NAME}/ a $(date)"
echo '-----------------------------------------------------------------------'
done
if [ -n "$EMERG" ]
then
doBackup /etc/remote-backup/"${EMERG//\//_}"/config emerg
else
for config in /etc/remote-backup/*/config
do
doBackup "$config"
done
fi