mirror of
https://github.com/YunoHost-Apps/restic_ynh.git
synced 2024-09-03 20:16:22 +02:00
feat: check backups on separate schedule
This commit is contained in:
parent
ba3a1d4b1f
commit
255867fb81
11 changed files with 181 additions and 20 deletions
|
@ -10,6 +10,8 @@ data=1
|
|||
app="all"
|
||||
allow_extra_space_use=1
|
||||
on_calendar="Daily"
|
||||
check_on_calendar="*-*-8,15,22 3:15"
|
||||
check_read_data_on_calendar="*-*-1 3:15"
|
||||
; Checks
|
||||
pkg_linter=1
|
||||
setup_sub_dir=0
|
||||
|
|
|
@ -34,16 +34,13 @@ do_backup() {
|
|||
pushd $work_dir
|
||||
$RESTIC_COMMAND backup ./ >> $LOGFILE 2>> $ERRFILE
|
||||
backup_return_code="$?"
|
||||
$RESTIC_COMMAND check >> $LOGFILE 2>> $ERRFILE
|
||||
check_return_code="$?"
|
||||
popd
|
||||
|
||||
# On ne nettoie que si la sauvegarde s'est bien passee
|
||||
if [ "$backup_return_code" -eq "0" ] && [ "$check_return_code" -eq 0 ];then
|
||||
if [ "$backup_return_code" -eq "0" ];then
|
||||
$RESTIC_COMMAND forget --keep-daily 7 --keep-weekly 8 --keep-monthly 12 >> $LOGFILE 2>> $ERRFILE
|
||||
else
|
||||
[ "$backup_return_code" -ne 0 ] && echo "Something went wrong during backup" >> $ERRFILE
|
||||
[ "$check_return_code" -ne 0 ] && echo "Repository check did not return 0" >> $ERRFILE
|
||||
echo "Something went wrong during backup" >> $ERRFILE
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
|
46
conf/check-restic.j2
Normal file
46
conf/check-restic.j2
Normal file
|
@ -0,0 +1,46 @@
|
|||
#!/bin/bash
|
||||
LOCK_FILE=/tmp/{{ app }}_check.lock
|
||||
EXIT_PROPERLY() {
|
||||
echo -e "\e[91m \e[1m" # Shell in light red bold
|
||||
echo -e "!!\n Caught an interruption signal, removing lock file...\n!!"
|
||||
echo -e "\e[22m" # Remove bold
|
||||
|
||||
rm $LOCK_FILE
|
||||
exit 1
|
||||
}
|
||||
trap EXIT_PROPERLY 1 2 3 6 15
|
||||
if [ -f "$LOCK_FILE" ];then
|
||||
echo "Check already launched by process $(grep '.*' $LOCK_FILE), canceling this one" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo $$ > "$LOCK_FILE"
|
||||
|
||||
CHECK_READ_DATA=${1:-0}
|
||||
|
||||
# Check system part conf
|
||||
conf=$(yunohost app setting {{ app }} conf)
|
||||
if [ $conf -eq 1 ];then
|
||||
{{final_path}}/check_method auto_conf ${CHECK_READ_DATA}
|
||||
fi
|
||||
|
||||
# Check system data
|
||||
data=$(yunohost app setting {{ app }} data)
|
||||
if [ $data -eq 1 ];then
|
||||
{{final_path}}/check_method auto_data ${CHECK_READ_DATA}
|
||||
fi
|
||||
|
||||
# Check all apps independently
|
||||
apps=$(yunohost app setting {{ app }} apps)
|
||||
for app in $(yunohost app list --installed -b | grep id: | cut -d: -f2); do
|
||||
check_app=false
|
||||
for selected_app in $(echo $apps | tr "," " ");do
|
||||
if [[ "$selected_app" == "$app" ]] || [ "$apps" = "all" ]; then
|
||||
check_app=true
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ "$check_app" == "true" ];then
|
||||
{{final_path}}/check_method auto_${app} ${CHECK_READ_DATA}
|
||||
fi
|
||||
done
|
||||
rm "$LOCK_FILE"
|
33
conf/check_method.j2
Normal file
33
conf/check_method.j2
Normal file
|
@ -0,0 +1,33 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
RESTIC_PASSWORD="{{ passphrase }}"
|
||||
RESTIC_REPOSITORY_BASE=sftp:{{ server }}:{{ backup_path }}
|
||||
RESTIC_COMMAND=/usr/local/bin/restic
|
||||
|
||||
do_check() {
|
||||
|
||||
local name="$1"
|
||||
local check_read_data="$2"
|
||||
export RESTIC_PASSWORD
|
||||
export RESTIC_REPOSITORY=${RESTIC_REPOSITORY_BASE}/$name
|
||||
LOGFILE=/var/log/restic_check.log
|
||||
ERRFILE=/var/log/restic_check.err
|
||||
current_date=$(date +"%d_%m_%y_%H:%M")
|
||||
echo -e "\n==============\n${current_date}\n==============\n" | tee -a ${LOGFILE} | tee -a ${ERRFILE}
|
||||
if [ "$check_read_data" -eq "1" ];then
|
||||
$RESTIC_COMMAND check --read-data >> $LOGFILE 2>> $ERRFILE
|
||||
else
|
||||
$RESTIC_COMMAND check >> $LOGFILE 2>> $ERRFILE
|
||||
fi
|
||||
check_return_code="$?"
|
||||
return "${check_return_code}"
|
||||
}
|
||||
|
||||
name=$1
|
||||
check_read_data=${2:-0}
|
||||
|
||||
do_check "${name}" "${check_read_data}"
|
||||
|
||||
exit 0
|
13
conf/systemd_check.service
Normal file
13
conf/systemd_check.service
Normal file
|
@ -0,0 +1,13 @@
|
|||
[Unit]
|
||||
Description=Check backup __APP__
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=__FINALPATH__/check-__APP__
|
||||
ExecStartPost=/bin/bash -c 'echo -e "Subject: YunoHost Restic check log on $(hostname)\n$(/bin/journalctl _SYSTEMD_INVOCATION_ID=`systemctl show -p InvocationID --value __APP___check.service`)" | /usr/sbin/sendmail root'
|
||||
User=root
|
||||
Group=root
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
8
conf/systemd_check.timer.j2
Normal file
8
conf/systemd_check.timer.j2
Normal file
|
@ -0,0 +1,8 @@
|
|||
[Unit]
|
||||
Description=Check {{ app }} backup regularly
|
||||
|
||||
[Timer]
|
||||
OnCalendar={{ check_on_calendar }}
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
13
conf/systemd_check_read_data.service
Normal file
13
conf/systemd_check_read_data.service
Normal file
|
@ -0,0 +1,13 @@
|
|||
[Unit]
|
||||
Description=Complete check backup __APP__
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=__FINALPATH__/check-__APP__ "1"
|
||||
ExecStartPost=/bin/bash -c 'echo -e "Subject: YunoHost Restic complete check log on $(hostname)\n$(/bin/journalctl _SYSTEMD_INVOCATION_ID=`systemctl show -p InvocationID --value __APP___check_read_data.service`)" | /usr/sbin/sendmail root'
|
||||
User=root
|
||||
Group=root
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
8
conf/systemd_check_read_data.timer.j2
Normal file
8
conf/systemd_check_read_data.timer.j2
Normal file
|
@ -0,0 +1,8 @@
|
|||
[Unit]
|
||||
Description=Complete check {{ app }} backup regularly
|
||||
|
||||
[Timer]
|
||||
OnCalendar={{ check_read_data_on_calendar }}
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
|
@ -128,6 +128,26 @@
|
|||
},
|
||||
"example": "Daily",
|
||||
"default": "Daily"
|
||||
},
|
||||
{
|
||||
"name": "check_on_calendar",
|
||||
"type": "string",
|
||||
"ask": {
|
||||
"en": "Indicate the backup check frequency (see systemd OnCalendar format)",
|
||||
"fr": "Indiquez la fréquence de vérification de la sauvegarde (voir le format OnCalendar de systemd)"
|
||||
},
|
||||
"example": "Tue *-*-* 00:15:00",
|
||||
"default": "*-*-8,15,22 3:15:00"
|
||||
},
|
||||
{
|
||||
"name": "check_read_data_on_calendar",
|
||||
"type": "string",
|
||||
"ask": {
|
||||
"en": "Indicate the complete backup check frequency (see systemd OnCalendar format)",
|
||||
"fr": "Indiquez la fréquence de vérification complète de la sauvegarde (voir le format OnCalendar de systemd)"
|
||||
},
|
||||
"example": "Tue *-*-* 00:15:00",
|
||||
"default": "*-*-1 1:15:00"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -20,14 +20,15 @@ ynh_abort_if_errors
|
|||
# RETRIEVE ARGUMENTS FROM THE MANIFEST
|
||||
#=================================================
|
||||
export app=$YNH_APP_INSTANCE_NAME
|
||||
export final_path="/opt/yunohost/${app}"
|
||||
|
||||
# Retrieve arguments
|
||||
ynh_export server port ssh_user backup_path passphrase on_calendar conf data apps allow_extra_space_use
|
||||
ynh_export server port ssh_user backup_path passphrase on_calendar check_on_calendar check_read_data_on_calendar conf data apps allow_extra_space_use
|
||||
|
||||
#=================================================
|
||||
# STORE SETTINGS FROM MANIFEST
|
||||
#=================================================
|
||||
ynh_save_args server port ssh_user backup_path passphrase on_calendar conf data apps allow_extra_space_use
|
||||
ynh_save_args server port ssh_user backup_path passphrase on_calendar check_on_calendar check_read_data_on_calendar conf data apps allow_extra_space_use
|
||||
|
||||
#=================================================
|
||||
# INSTALL DEPENDENCIES
|
||||
|
@ -49,19 +50,31 @@ mkdir -p /usr/share/yunohost/backup_method
|
|||
#=================================================
|
||||
ynh_print_info --message="Setting up backup methods"
|
||||
ynh_configure backup_method "/etc/yunohost/hooks.d/backup_method/05-${app}_app"
|
||||
ynh_configure check_method "${final_path}/check_method"
|
||||
|
||||
#=================================================
|
||||
# CONFIGURE CRON
|
||||
#=================================================
|
||||
ynh_print_info --message="Configuring cron"
|
||||
ynh_configure backup-with-restic "/usr/local/bin/backup-with-$app"
|
||||
ynh_configure backup-with-restic-answerbot "/usr/local/bin/backup-with-$app-answerbot"
|
||||
chmod u+x "/usr/local/bin/backup-with-$app"
|
||||
chmod u+x "/usr/local/bin/backup-with-$app-answerbot"
|
||||
ynh_add_systemd_config
|
||||
ynh_configure systemd.timer "/etc/systemd/system/$app.timer"
|
||||
systemctl enable $app.timer
|
||||
systemctl start $app.timer
|
||||
ynh_configure backup-with-restic "/usr/local/bin/backup-with-${app}"
|
||||
ynh_configure backup-with-restic-answerbot "/usr/local/bin/backup-with-${app}-answerbot"
|
||||
ynh_configure check-restic "${final_path}/check-${app}"
|
||||
chmod u+x "/usr/local/bin/backup-with-${app}"
|
||||
chmod u+x "/usr/local/bin/backup-with-${app}-answerbot"
|
||||
chmod u+x "${final_path}/check-${app}"
|
||||
chmod u+x "${final_path}/check_method"
|
||||
ynh_add_systemd_config --service=${app} --template=systemd.service
|
||||
ynh_add_systemd_config --service=${app}_check --template=systemd_check.service
|
||||
ynh_add_systemd_config --service=${app}_check_read_data --template=systemd_check_read_data.service
|
||||
ynh_configure systemd.timer "/etc/systemd/system/${app}.timer"
|
||||
ynh_configure systemd_check.timer "/etc/systemd/system/${app}_check.timer"
|
||||
ynh_configure systemd_check_read_data.timer "/etc/systemd/system/${app}_check_read_data.timer"
|
||||
systemctl enable ${app}.timer
|
||||
systemctl enable ${app}_check.timer
|
||||
systemctl enable ${app}_check_read_data.timer
|
||||
systemctl start ${app}.timer
|
||||
systemctl start ${app}_check.timer
|
||||
systemctl start ${app}_check_read_data.timer
|
||||
|
||||
#=================================================
|
||||
# GENERATE SSH KEY
|
||||
|
|
|
@ -23,12 +23,20 @@ ynh_remove_app_dependencies
|
|||
#=================================================
|
||||
# REMOVE FILES
|
||||
#=================================================
|
||||
systemctl stop $app.timer
|
||||
systemctl disable $app.timer
|
||||
ynh_remove_systemd_config
|
||||
ynh_secure_remove "/etc/systemd/system/$app.timer"
|
||||
ynh_secure_remove "/usr/local/bin/backup-with-$app"
|
||||
systemctl stop ${app}.timer
|
||||
systemctl disable ${app}.timer
|
||||
ynh_remove_systemd_config --service=${app}
|
||||
ynh_remove_systemd_config --service=${app}_check
|
||||
ynh_remove_systemd_config --service=${app}_check_read_data
|
||||
ynh_secure_remove "/etc/systemd/system/${app}.timer"
|
||||
ynh_secure_remove "/etc/systemd/system/${app}_check.timer"
|
||||
ynh_secure_remove "/etc/systemd/system/${app}_check_read_data.timer"
|
||||
ynh_secure_remove "/usr/local/bin/backup-with-${app}"
|
||||
ynh_secure_remove "/usr/local/bin/check-${app}"
|
||||
ynh_secure_remove "/usr/local/bin/check-read-data-${app}"
|
||||
ynh_secure_remove "/etc/yunohost/hooks.d/backup_method/05-${app}_app"
|
||||
ynh_secure_remove "/etc/yunohost/hooks.d/backup_method/05-${app}_check_app"
|
||||
ynh_secure_remove "/etc/yunohost/hooks.d/backup_method/05-${app}_check_read_data_app"
|
||||
|
||||
#=================================================
|
||||
# REMOVE SSH CONFIG
|
||||
|
|
Loading…
Add table
Reference in a new issue