diff --git a/rsnapshot_anacron/daily/rsnapshot_daily b/rsnapshot_anacron/daily/rsnapshot_daily new file mode 100755 index 0000000..991d210 --- /dev/null +++ b/rsnapshot_anacron/daily/rsnapshot_daily @@ -0,0 +1,7 @@ +#!/bin/bash + +source /rsnapshot_common + +# 86400 seconds in a day, retry every 10 minutes for half a day +backup "40000" "daily" + diff --git a/rsnapshot_anacron/hourly/rsnapshot_hourly b/rsnapshot_anacron/hourly/rsnapshot_hourly new file mode 100755 index 0000000..b791a84 --- /dev/null +++ b/rsnapshot_anacron/hourly/rsnapshot_hourly @@ -0,0 +1,6 @@ +#!/bin/bash + +source /rsnapshot_common + +# 3600 seconds in an hour, retry every 10 minutes for half an hour +backup "1800" "hourly" diff --git a/rsnapshot_anacron/install.sh b/rsnapshot_anacron/install.sh new file mode 100644 index 0000000..a544c33 --- /dev/null +++ b/rsnapshot_anacron/install.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +# Immediately exit if any command has a non-zero exit. +# A reference to any variable you haven't previously defined - with the exceptions of $* and $@ - is an error. +# If any command in a pipeline fails, that return code will be used as the return code of the whole pipeline. +set -euo pipefail + +user="$USER" + +mkdir -p $user/scripts +cp -r ../anacron $user/scripts/ +cd $user/scripts/ + +base="$(pwd)" + +# Check installed packages +installed="$(dpkg -l rsnapshot)|grep '^ii\srsnapshot\s'" +if [[ "$sintalled" == "" ]]; then + echo "rsnapshot is not installed" + echo "You should install it and edit /etc/rsnapshot.conf:" + echo "sudo apt install rsnapshot anacron && sudo gvim /etc/rsnapshot.conf" + exit 100 +fi +sudo install anacron + + +snapshot=$(grep "^snapshot_root.*$" /etc/rsnapshot.conf|cut -f 2) + +if [[ $? != 0 || "$snapshot" == "" ]] ; then + echo "No snapshot_root in /etc/rsnapshot.conf" + exit 101 +fi + +tag="=^=rsnapshot_anacron=^=" +if [[ $(grep "$tag" /etc/anacrontab) == "" ]]; then +sudo echo " +########################################################## +# $tag +# périodicité (jours), +# | délai (minutes), +# | | nom de la tache, +# | | | commandes +########################################################## +@daily 10 backup.daily sudo -u $user run-parts $base/daily/ +@weekly 10 backup.weekly sudo -u $user run-parts $base/weekly/ +@monthly 10 backup.monthly sudo -u $user run-parts $base/monthly/ +" >> /etc/anacrontab +fi + +ln -s $base ~/.anacron + +# Test if snapshot dir is set +source $base/rsnapshot_common +if [[ "$SNAPSHOT_ROOT" == "" ]] ; then + sed -i "s,,${snapshot},g" rsnapshot_common +else + if [[ "$SNAPSHOT_ROOT" != "$snapshot" ]] ; then + echo "snapshot_root in /etc/rsnapshot.conf is different than SNAPSHOT_ROOT in rsnapshot_common" + exit 102 + fi +fi + +# Configure absolute path in scripts +sed -i "s,,$base,g" $base/hourly/rsnapshot_hourly +sed -i "s,,$base,g" $base/daily/rsnapshot_daily +sed -i "s,,$base,g" $base/weekly/rsnapshot_weekly +sed -i "s,,$base,g" $base/monthly/rsnapshot_monthly + +echo "DONE" diff --git a/rsnapshot_anacron/monthly/rsnapshot_monthly b/rsnapshot_anacron/monthly/rsnapshot_monthly new file mode 100755 index 0000000..01cbb5c --- /dev/null +++ b/rsnapshot_anacron/monthly/rsnapshot_monthly @@ -0,0 +1,7 @@ +#!/bin/bash + +source /rsnapshot_common + +# 2592000 seconds in a month, retry every 10 minutes for 8 days +backup "691200" "monthly" + diff --git a/rsnapshot_anacron/rsnapshot_common b/rsnapshot_anacron/rsnapshot_common new file mode 100644 index 0000000..e82ee9f --- /dev/null +++ b/rsnapshot_anacron/rsnapshot_common @@ -0,0 +1,50 @@ +# Immediately exit if any command has a non-zero exit. +# A reference to any variable you haven't previously defined - with the exceptions of $* and $@ - is an error. +# If any command in a pipeline fails, that return code will be used as the return code of the whole pipeline. +set -euo pipefail + +# There are already cron scripts installed to automatically run rsnapshot in +# the background on a regular hourly/daily/weekly/monthly schedule. If this +# is on a machine that is shutdown or goes to sleep often, then install +# anacron. +# +ENABLE_CRON=no + +# Also run the hourly job in addition to the daily, weekly, and monthly +#ENABLE_HOURLY=yes + +# Specify the disk where the backups are written to, this is the same place as +# is specified in /etc/rsnapshot.conf as 'snapshot_root'. This is used to +# detect if the disk is mounted or not when rsnapshot runs in a cron job. +# +SNAPSHOT_ROOT="" + +RSNAPSHOT="/usr/bin/rsnapshot -t" + +function backup() +{ + wait="$1" # in seconds + freq="$2" # hourly, daily, weekly or monthly + + #retry every 10 minutes + every=600 + + if [[ "$ENABLE_CRON" == "yes" ]] ; then + exit 1000 + fi + + until=$(( $(date +%s) + $wait)) + while [[ $(date +%s) < $until ]] ; do + test -d "$SNAPSHOT_ROOT" && break + sleep $every + done + + # See ionice(1) + if [ -x /usr/bin/ionice ] && + /usr/bin/ionice -c3 true 2>/dev/null; then + IONICE="/usr/bin/ionice -c3" + fi + + test -d "$SNAPSHOT_ROOT" && \ + nice -n 20 $IONICE $RSNAPSHOT $freq +} diff --git a/rsnapshot_anacron/weekly/rsnapshot_weekly b/rsnapshot_anacron/weekly/rsnapshot_weekly new file mode 100755 index 0000000..171147e --- /dev/null +++ b/rsnapshot_anacron/weekly/rsnapshot_weekly @@ -0,0 +1,7 @@ +#!/bin/bash + +source /rsnapshot_common + +# 604800 seconds in a week, retry every 10 minutes for 4 days +backup "345600" "weekly" +