diff --git a/check_process b/check_process index 16562cf..9f5f81b 100644 --- a/check_process +++ b/check_process @@ -14,6 +14,7 @@ setup_private=0 setup_public=0 upgrade=1 + upgrade=1 from_commit=c1524dd8e37cc671c01f5b5363901dd6a01e6fc1 backup_restore=1 multi_instance=1 port_already_use=0 diff --git a/conf/backup-with-borg b/conf/backup-with-borg index e202f3a..f153033 100644 --- a/conf/backup-with-borg +++ b/conf/backup-with-borg @@ -9,14 +9,14 @@ current_date=$(date +"%y%m%d_%H%M") log_file="/var/log/${borg_id}/${current_date}.log" err_file="/var/log/${borg_id}/${current_date}.err" mkdir -p "/var/log/${borg_id}" -if [[ -z "$borg_id" ]] +if [ -z "$borg_id" ] then echo "This script expects a borg app id as first argument" >&2 exit 1 fi filter_hooks() { - ls /usr/share/yunohost/hooks/backup/ /etc/yunohost/hooks.d/backup/ | grep "\-$1_" | cut -d"-" -f2 | uniq + sudo ls /usr/share/yunohost/hooks/backup/ /etc/yunohost/hooks.d/backup/ | grep "\-$1_" | cut -d"-" -f2 | uniq 2>> $err_file } fail_if_partially_failed() { @@ -24,52 +24,58 @@ fail_if_partially_failed() { } # Backup system part conf -conf=$(yunohost app setting ${borg_id} conf) -if [ $conf -eq 1 ] +conf=$(sudo yunohost app setting ${borg_id} conf) +if [[ "$conf" = "1" ]] then - if ! yunohost backup create -n auto_conf --method ${borg_id}_app --system $(filter_hooks conf) 2> $err_file > $log_file ; then - errors="$errors\nconf: Error" + if ! sudo yunohost backup create -n auto_conf --method ${borg_id}_app --system $(filter_hooks conf) 2>> $err_file >> $log_file ; then + errors+="\nThe backup miserably failed to backup system configurations." fi fi # Backup system data -data=$(yunohost app setting $app data) -if [ $data -eq 1 ] +data=$(sudo yunohost app setting ${borg_id} data) +if [[ "$data" = "1" ]] then - if ! yunohost backup create -n auto_data --method ${borg_id}_app --system $(filter_hooks data) 2> $err_file > $log_file ; then - errors="$errors\ndata: Error" + if ! sudo yunohost backup create -n auto_data --method ${borg_id}_app --system $(filter_hooks data) 2>> $err_file >> $log_file ; then + errors+="\nThe backup miserably failed to backup system data." fi fi # Backup all apps independently -apps=$(yunohost app setting ${borg_id} apps) -for application in $(ls /etc/yunohost/apps/*/scripts/backup | cut -d / -f 5); do - backup_app=false - if [[ "$apps" = "all" ]]; then - backup_app=true - else - for selected_app in $(echo $apps | tr "," " ");do - if [[ "$selected_app" == "$application" ]]; then - backup_app=true - break - fi - done +apps=$(sudo yunohost app setting ${borg_id} apps | tr -d ' ') +for application in $(sudo ls /etc/yunohost/apps/); do + + if ( [[ "$apps" =~ ^exclude: ]] && grep -wq "$application" <<< "$apps" ) || + ( [[ "$apps" != "all" ]] && [[ ! "$apps" =~ ^exclude: ]] && ! grep -wq "$application" <<< "$apps" ); + then + continue fi - if [ "$backup_app" == "true" ];then - if ! yunohost backup create -n auto_$application --method ${borg_id}_app --apps $application 2> $err_file > $log_file ; then - errors="$errors\$application: Error" - fi + + if sudo test ! -f /etc/yunohost/apps/$application/scripts/backup ; then + errors+="\nWarning: The application $application has no backup script. This app won't be backuped." + continue + fi + + if ! sudo yunohost backup create -n auto_$application --method ${borg_id}_app --apps $application 2>> $err_file >> $log_file ; then + errors+="\nThe backup miserably failed to backup $application application." fi done + +#========================================================= +# SEND MAIL TO NOTIFY SUCCED OR FAILED OPERATIONS +#========================================================= + partial_errors="$(cat $log_file | grep -E "Error|Skipped")" if [ ! -z "$partial_errors" ]; then - errors="$errors\n$partial_errors" + errors+="\nSome backup partially failed:\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 + cat <(echo -e "$errors\n\n\n") "$log_file" "$err_file" | mail -s "[borg] Backup failed from $domain onto $repo" root + exit 1 else cat $log_file | mail -s "[borg] Backup succeed from $domain onto $repo" root + exit 0 fi diff --git a/conf/backup_method b/conf/backup_method index 4cf3c30..0b3c1ed 100644 --- a/conf/backup_method +++ b/conf/backup_method @@ -1,13 +1,15 @@ #!/bin/bash set -e +app="${0#"./05-"}" +app="${app%"_app"}" -BORG_PASSPHRASE='__PASSPHRASE__' -repo="__REPOSITORY__" #$4 +BORG_PASSPHRASE="$(yunohost app setting $app passphrase)" +repo="$(yunohost app setting $app repository)" #$4 if ssh-keygen -F "__SERVER__" >/dev/null ; then - BORG_RSH="ssh -i /root/.ssh/id___APP___ed25519 -oStrictHostKeyChecking=yes " + BORG_RSH="ssh -i /root/.ssh/id_${app}_ed25519 -oStrictHostKeyChecking=yes " else - BORG_RSH="ssh -i /root/.ssh/id___APP___ed25519 -oStrictHostKeyChecking=no " + BORG_RSH="ssh -i /root/.ssh/id_${app}_ed25519 -oStrictHostKeyChecking=no " fi do_need_mount() { diff --git a/conf/sudoer b/conf/sudoer index 7945669..6a11357 100644 --- a/conf/sudoer +++ b/conf/sudoer @@ -1 +1 @@ -__APP__ ALL=(root) /usr/local/bin/backup-with-__APP__, /usr/local/bin/borg +__APP__ ALL=(root) NOPASSWD: /usr/local/bin/backup-with-__APP__, /usr/local/bin/borg, /usr/bin/yunohost backup create *, /usr/bin/yunohost app setting *, /bin/ls, /usr/bin/test diff --git a/manifest.json b/manifest.json index 2e04833..5744207 100644 --- a/manifest.json +++ b/manifest.json @@ -6,7 +6,7 @@ "en": "Backup your server on a host server using Borg.", "fr": "Sauvegardez votre serveur sur un serveur distant avec Borg." }, - "version": "1.1.16~ynh1", + "version": "1.1.16~ynh16", "url": "https://borgbackup.readthedocs.io", "license": "BSD-3-Clause", "maintainer": { @@ -67,9 +67,13 @@ "name": "apps", "type": "string", "ask": { - "en": "Which apps should Borg backup (list separated by comma or 'all')?", - "fr": "Quelles applications doivent être sauvegardées par Borg (liste séparée par virgule ou 'all' ?" + "en": "Which apps should Borg backup ?", + "fr": "Quelles applications doivent être sauvegardées par Borg ?" }, + "help":{ + "en": "App list separated by comma. You can write 'all' to select all apps, even those installed after this borg app. You can also select all apps but some apps by writing 'exclude:' following by an app list separated by comma.", + "fr": "Liste d'applications séparées par des virgules. Vous pouvez écrire 'all' pour sélectionner toutes les apps, même celles installées après cette application borg. Vous pouvez aussi sélectionner toutes les apps sauf certaines en écrivant 'exclude:' suivi d'une liste d'applications séparées par des virgules." + }, "default": "all" }, { diff --git a/scripts/install b/scripts/install index 2be84fd..3253316 100755 --- a/scripts/install +++ b/scripts/install @@ -60,8 +60,12 @@ ynh_system_user_create --username=$app # ACTIVATE BACKUP METHODS #================================================= +mkdir -p /etc/yunohost/hooks.d/backup mkdir -p /etc/yunohost/hooks.d/backup_method mkdir -p /usr/share/yunohost/backup_method +mkdir -p /var/log/${app} +chown -R $app:$app /var/log/${app} +chmod u+w /var/log/${app} #================================================= # SETUP THE BACKUP METHOD @@ -72,6 +76,8 @@ chmod go=--- "/etc/yunohost/hooks.d/backup_method/05-${app}_app" ynh_add_config --template="backup-with-borg" --destination="/usr/local/bin/backup-with-$app" chmod u+x "/usr/local/bin/backup-with-$app" +chown $app:$app "/usr/local/bin/backup-with-$app" + ynh_add_config --template="sudoer" --destination="/etc/sudoers.d/$app" if [ ! -z "$server" ]; then diff --git a/scripts/restore b/scripts/restore index f05ed7c..6b396b2 100755 --- a/scripts/restore +++ b/scripts/restore @@ -37,12 +37,19 @@ install_borg_with_pip mkdir -p /etc/yunohost/hooks.d/backup_method mkdir -p /usr/share/yunohost/backup_method +mkdir -p /etc/yunohost/hooks.d/backup +mkdir -p /var/log/${app} +chown -R $app:$app /var/log/${app} +chmod u+w /var/log/${app} #================================================= # RESTORE FILES #================================================= ynh_restore +chmod go=--- "/etc/yunohost/hooks.d/backup_method/05-${app}_app" +chmod u+x "/usr/local/bin/backup-with-$app" +chown $app:$app "/usr/local/bin/backup-with-$app" #================================================= # ADVERTISE SERVICE IN ADMIN PANEL diff --git a/scripts/upgrade b/scripts/upgrade index e5a36f4..37c8520 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -99,6 +99,7 @@ chmod go=--- "/etc/yunohost/hooks.d/backup_method/05-${app}_app" ynh_add_config --template="backup-with-borg" --destination="/usr/local/bin/backup-with-$app" chmod u+x "/usr/local/bin/backup-with-$app" +chown $app:$app "/usr/local/bin/backup-with-$app" ynh_add_config --template="sudoer" --destination="/etc/sudoers.d/$app" @@ -117,6 +118,11 @@ ynh_add_config --template="systemd.timer" --destination="/etc/systemd/system/$ap systemctl enable $app.timer --quiet systemctl start $app.timer +mkdir -p /etc/yunohost/hooks.d/backup +mkdir -p /var/log/${app} +chown -R $app:$app /var/log/${app} +chmod u+w /var/log/${app} + #================================================= # GENERIC FINALIZATION #=================================================