Skip to content

Instantly share code, notes, and snippets.

@philr
Created December 28, 2015 18:14
Show Gist options
  • Save philr/0bb3cbde716f9a503838 to your computer and use it in GitHub Desktop.
Save philr/0bb3cbde716f9a503838 to your computer and use it in GitHub Desktop.
Run all required rsnapshot intervals based on the date they were last run
#!/bin/bash
CONF_FILE=/etc/rsnapshot.conf
if [ $# -lt 1 ]
then
echo "Usage: $0 interval1:dateformat1 interval2:dateformat2 ..." >&2
echo >&2
echo " Where intervaln is an rsnapshot retain interval and dateformatn is a" >&2
echo " format letter to be passed to date. If the intervaln.0 ctime formatted" >&2
echo " with dateformatn differs to the current date formatted with dateformatn" >&2
echo " the backup will be run." >&2
echo >&2
echo " E.g.: $0 monthly:m weekly:W daily:j" >&2
exit 1
fi
snapshot_root=`awk -F $'\t' '/^snapshot_root\t/ { print $2 }' "$CONF_FILE"`
if [ -z "$snapshot_root" ]
then
echo "Could not find snapshot_root option in $CONF_FILE" >&2
exit 1
fi
now=`date +%s`
for spec in "$@"
do
interval=${spec%:*}
format=${spec#*:*}
current="$snapshot_root/$interval.0"
run=0
if [ ! -d "$current" ]
then
echo "No $interval.0 directory found"
run=1
else
ctime=`stat --format=%Z "$snapshot_root/$interval.0"`
ctime_formatted=`date "-d@$ctime" "+%$format"`
now_formatted=`date "-d@$now" "+%$format"`
if [ "$ctime_formatted" -ne "$now_formatted" ]
then
echo "Formatted change time of $interval.0 ($ctime_formatted) does not match current time ($now_formatted)"
run=1
else
echo "Formatted change time of $interval.0 ($ctime_formatted) matches current time"
fi
fi
if [ $run -eq 1 ]
then
echo "Running rsnapshot $interval"
rsnapshot -c "$CONF_FILE" "$interval"
else
echo "Skipping rsnapshot $interval"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment