rsnapshot/anacron backup settings

This commit is contained in:
Johann Dreo 2018-03-03 14:07:50 +01:00
commit b88f1f3566
6 changed files with 146 additions and 0 deletions

View file

@ -0,0 +1,7 @@
#!/bin/bash
source <BASE>/rsnapshot_common
# 86400 seconds in a day, retry every 10 minutes for half a day
backup "40000" "daily"

View file

@ -0,0 +1,6 @@
#!/bin/bash
source <BASE>/rsnapshot_common
# 3600 seconds in an hour, retry every 10 minutes for half an hour
backup "1800" "hourly"

View file

@ -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" == "<NOT_DEFINED>" ]] ; then
sed -i "s,<NOT_DEFINED>,${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>,$base,g" $base/hourly/rsnapshot_hourly
sed -i "s,<BASE>,$base,g" $base/daily/rsnapshot_daily
sed -i "s,<BASE>,$base,g" $base/weekly/rsnapshot_weekly
sed -i "s,<BASE>,$base,g" $base/monthly/rsnapshot_monthly
echo "DONE"

View file

@ -0,0 +1,7 @@
#!/bin/bash
source <BASE>/rsnapshot_common
# 2592000 seconds in a month, retry every 10 minutes for 8 days
backup "691200" "monthly"

View file

@ -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="<NOT_DEFINED>"
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
}

View file

@ -0,0 +1,7 @@
#!/bin/bash
source <BASE>/rsnapshot_common
# 604800 seconds in a week, retry every 10 minutes for 4 days
backup "345600" "weekly"