2020-02-23 12:02:10 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2020-11-08 17:42:25 +01:00
|
|
|
###
|
|
|
|
# Fetch information from YNH settings
|
|
|
|
###
|
2021-02-28 16:38:35 +01:00
|
|
|
RESTIC_SERVER=$(yunohost app setting {{ app }} server)
|
|
|
|
RESTIC_SERVER_PORT=$(yunohost app setting {{ app }} port)
|
|
|
|
RESTIC_SERVER_USER=$(yunohost app setting {{ app }} ssh_user)
|
|
|
|
RESTIC_PATH=$(yunohost app setting {{ app }} backup_path)
|
2020-11-08 17:42:25 +01:00
|
|
|
|
2021-02-28 16:38:35 +01:00
|
|
|
RESTIC_PASSWORD="$(yunohost app setting {{ app }} passphrase)"
|
2020-11-14 18:02:45 +01:00
|
|
|
RESTIC_REPOSITORY_BASE=sftp://$RESTIC_SERVER_USER@$RESTIC_SERVER:$RESTIC_SERVER_PORT/$RESTIC_PATH/
|
2020-11-08 17:42:25 +01:00
|
|
|
|
2021-02-28 16:38:35 +01:00
|
|
|
RESTIC_COMMAND=/usr/local/bin/{{ app }}
|
|
|
|
LOGFILE=/var/log/restic_backup_{{ app }}.log
|
|
|
|
ERRFILE=/var/log/restic_backup_{{ app }}.err
|
2020-02-23 12:02:10 +01:00
|
|
|
|
|
|
|
do_need_mount() {
|
|
|
|
work_dir="$1"
|
|
|
|
name="$2"
|
|
|
|
repo="$3"
|
|
|
|
size="$4"
|
|
|
|
description="$5"
|
|
|
|
export RESTIC_PASSWORD
|
|
|
|
export RESTIC_REPOSITORY=${RESTIC_REPOSITORY_BASE}/$name
|
|
|
|
|
|
|
|
# On essaie de lister les snapshots, sinon on initialise le depot
|
2020-03-03 19:24:13 +01:00
|
|
|
$RESTIC_COMMAND list snapshots > >(tee -a $LOGFILE) 2> >(tee -a $ERRFILE >&2) || $RESTIC_COMMAND init > >(tee -a $LOGFILE) 2> >(tee -a $ERRFILE >&2)
|
2020-02-23 12:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
do_backup() {
|
|
|
|
|
|
|
|
work_dir="$1"
|
|
|
|
name="$2"
|
|
|
|
repo="$3"
|
|
|
|
size="$4"
|
|
|
|
description="$5"
|
|
|
|
export RESTIC_PASSWORD
|
|
|
|
export RESTIC_REPOSITORY=${RESTIC_REPOSITORY_BASE}/$name
|
|
|
|
current_date=$(date +"%d_%m_%y_%H:%M")
|
|
|
|
pushd $work_dir
|
2020-03-03 19:24:13 +01:00
|
|
|
$RESTIC_COMMAND backup ./ > >(tee -a $LOGFILE) 2> >(tee -a $ERRFILE >&2)
|
2020-02-25 22:07:47 +01:00
|
|
|
backup_return_code="$?"
|
2020-02-23 12:02:10 +01:00
|
|
|
popd
|
|
|
|
|
|
|
|
# On ne nettoie que si la sauvegarde s'est bien passee
|
2020-03-02 22:11:41 +01:00
|
|
|
if [ "$backup_return_code" -eq "0" ];then
|
2020-03-03 19:24:13 +01:00
|
|
|
$RESTIC_COMMAND forget --keep-daily 7 --keep-weekly 8 --keep-monthly 12 > >(tee -a $LOGFILE) 2> >(tee -a $ERRFILE >&2)
|
2020-02-25 22:07:47 +01:00
|
|
|
else
|
2020-03-03 19:24:13 +01:00
|
|
|
echo "Something went wrong during backup" > >(tee -a $ERRFILE >&2)
|
2020-02-25 22:07:47 +01:00
|
|
|
exit 1
|
2020-02-23 12:02:10 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
work_dir=$2
|
|
|
|
name=$3
|
|
|
|
|
|
|
|
size=$5
|
|
|
|
description=$6
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
need_mount)
|
|
|
|
do_need_mount $work_dir $name $repo $size $description
|
|
|
|
;;
|
|
|
|
backup)
|
|
|
|
do_backup $work_dir $name $repo $size $description
|
|
|
|
;;
|
|
|
|
mount)
|
|
|
|
do_need_mount $work_dir $name $repo $size $description
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "hook called with unknown argument \`$1'" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
exit 0
|