From f02754e21e872c3922a51ccb13802523c761a20e Mon Sep 17 00:00:00 2001 From: ljf Date: Sat, 6 Mar 2021 22:04:36 +0100 Subject: [PATCH] I should commit every day --- conf/backup-with-borg | 36 +++++++++++++++++++++++++++++++++--- scripts/install | 1 + 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/conf/backup-with-borg b/conf/backup-with-borg index 2f09a3b..38f1f00 100644 --- a/conf/backup-with-borg +++ b/conf/backup-with-borg @@ -1,6 +1,14 @@ #!/bin/bash +# We don't stop the script on errors cause we want to backup all data we could backuped +#set -eu + app=$1 +errors="" +current_date=$(date +"%y%m%d_%H%M") +log_file="/var/log/$app/$current_date.log" +err_file="/var/log/$app/$current_date.err" +mkdir -p "/var/log/$app" # Adapt this script to different API 2.x vs 3.x if yunohost -v | grep "version: 2." > /dev/null; then @@ -15,18 +23,26 @@ filter_hooks() { ls /usr/share/yunohost/hooks/backup/ /etc/yunohost/hooks.d/backup/ | grep "\-$1_" | cut -d"-" -f2 | uniq } +fail_if_partially_failed() { + grep Skipped|Error +} + # Backup system part conf conf=$(yunohost app setting $app conf) if [ $conf -eq 1 ] then - yunohost backup create $ignore_apps -n auto_conf --method $app_app --system $(filter_hooks conf) + if ! yunohost backup create $ignore_apps -n auto_conf --method $app_app --system $(filter_hooks conf) 2> $err_file > $log_file ; then + errors="$errors\nconf: Error" + fi fi # Backup system data data=$(yunohost app setting $app data) if [ $data -eq 1 ] then - yunohost backup create $ignore_apps -n auto_data --method $app_app --system $(filter_hooks data) + if ! yunohost backup create $ignore_apps -n auto_data --method $app_app --system $(filter_hooks data) 2> $err_file > $log_file ; then + errors="$errors\ndata: Error" + fi fi # Backup all apps independently @@ -44,6 +60,20 @@ for application in $(ls /etc/yunohost/apps/*/scripts/backup | cut -d / -f 5); do done fi if [ "$backup_app" == "true" ];then - yunohost backup create $ignore_system -n auto_$application --method $app_app --apps $app + if ! yunohost backup create $ignore_system -n auto_$application --method $app_app --apps $app 2> $err_file > $log_file ; then + errors="$errors\$application: Error" + fi fi done +partial_errors="$(cat $log_file | grep -E "Error|Skipped")" +if [ ! -z "$partial_errors" ]; then + errors="$errors\n$partial_errors" +fi + +# Send mail on backup (partially) failed +domain=$(hostname) +if [ ! -z "$errors" ]; then + cat $errors | mail -s "[borg] Backup failed from $domain onto $repo" root +else + cat $log_file | mail -s "[borg] Backup succeed from $domain onto $repo" root +fi diff --git a/scripts/install b/scripts/install index c1552fd..85610c6 100755 --- a/scripts/install +++ b/scripts/install @@ -34,6 +34,7 @@ if [[ $repository == *"@"* ]]; then if [[ $server == *":"* ]]; then server="[$(echo "$server" | cut -d":" -f1)]:$(echo "$server" | cut -d":" -f2)" fi + ssh_user=$(echo "$repository" | cut -d"@" -f1 | cut -d"/" -f2) fi ynh_save_args repository server passphrase on_calendar conf data apps