mirror of
https://github.com/YunoHost-Apps/borg_ynh.git
synced 2024-09-03 18:16:05 +02:00
Coding style
This commit is contained in:
parent
75c2765211
commit
97d578d127
1 changed files with 28 additions and 32 deletions
|
@ -1,6 +1,5 @@
|
||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
|
# We don't stop the script on errors cause we want to backup all data we could backed up
|
||||||
# We don't stop the script on errors cause we want to backup all data we could backuped
|
|
||||||
#set -eu
|
#set -eu
|
||||||
|
|
||||||
borg_id=$1
|
borg_id=$1
|
||||||
|
@ -9,42 +8,39 @@ current_date=$(date +"%y%m%d_%H%M")
|
||||||
log_file="/var/log/${borg_id}/${current_date}.log"
|
log_file="/var/log/${borg_id}/${current_date}.log"
|
||||||
err_file="/var/log/${borg_id}/${current_date}.err"
|
err_file="/var/log/${borg_id}/${current_date}.err"
|
||||||
mkdir -p "/var/log/${borg_id}"
|
mkdir -p "/var/log/${borg_id}"
|
||||||
if [ -z "$borg_id" ]
|
if [ -z "$borg_id" ]; then
|
||||||
then
|
|
||||||
echo "This script expects a borg app id as first argument" >&2
|
echo "This script expects a borg app id as first argument" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
filter_hooks() {
|
filter_hooks() {
|
||||||
sudo ls /usr/share/yunohost/hooks/backup/ /etc/yunohost/hooks.d/backup/ | grep "\-$1_" | cut -d"-" -f2 | uniq 2>> $err_file
|
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() {
|
fail_if_partially_failed() {
|
||||||
grep Skipped|Error
|
grep Skipped|Error
|
||||||
}
|
}
|
||||||
sudo yunohost app setting ${borg_id} last_run -v "${current_date}"
|
sudo yunohost app setting "${borg_id}" last_run -v "${current_date}"
|
||||||
sudo yunohost app setting ${borg_id} state -v "ongoing"
|
sudo yunohost app setting "${borg_id}" state -v "ongoing"
|
||||||
|
|
||||||
# Backup system part conf
|
# Backup system part conf
|
||||||
conf=$(sudo yunohost app setting ${borg_id} conf)
|
conf=$(sudo yunohost app setting "${borg_id}" conf)
|
||||||
if [[ "$conf" = "1" ]]
|
if [[ "$conf" = "1" ]]; then
|
||||||
then
|
if ! sudo yunohost backup create -n auto_conf --method "${borg_id}_app" --system $(filter_hooks conf) 2>> "$err_file" >> "$log_file" ; then
|
||||||
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."
|
errors+="\nThe backup miserably failed to backup system configurations."
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Backup system data
|
# Backup system data
|
||||||
data=$(sudo yunohost app setting ${borg_id} data)
|
data=$(sudo yunohost app setting "${borg_id}" data)
|
||||||
if [[ "$data" = "1" ]]
|
if [[ "$data" = "1" ]]; then
|
||||||
then
|
if ! sudo yunohost backup create -n auto_data --method "${borg_id}_app" --system $(filter_hooks data) 2>> "$err_file" >> "$log_file" ; then
|
||||||
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."
|
errors+="\nThe backup miserably failed to backup system data."
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Backup all apps independently
|
# Backup all apps independently
|
||||||
apps=$(sudo yunohost app setting ${borg_id} apps | tr -d ' ')
|
apps=$(sudo yunohost app setting "${borg_id}" apps | tr -d ' ')
|
||||||
for application in $(sudo ls /etc/yunohost/apps/); do
|
for application in $(sudo ls /etc/yunohost/apps/); do
|
||||||
|
|
||||||
if ( [[ "$apps" =~ ^exclude: ]] && grep -wq "$application" <<< "$apps" ) ||
|
if ( [[ "$apps" =~ ^exclude: ]] && grep -wq "$application" <<< "$apps" ) ||
|
||||||
|
@ -53,12 +49,12 @@ for application in $(sudo ls /etc/yunohost/apps/); do
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if sudo test ! -f /etc/yunohost/apps/$application/scripts/backup ; then
|
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."
|
errors+="\nWarning: The application $application has no backup script. This app won't be backuped."
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! sudo yunohost backup create -n auto_$application --method ${borg_id}_app --apps $application 2>> $err_file >> $log_file ; then
|
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."
|
errors+="\nThe backup miserably failed to backup $application application."
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
@ -67,25 +63,25 @@ done
|
||||||
# SEND MAIL TO NOTIFY SUCCED OR FAILED OPERATIONS
|
# SEND MAIL TO NOTIFY SUCCED OR FAILED OPERATIONS
|
||||||
#=========================================================
|
#=========================================================
|
||||||
|
|
||||||
partial_errors="$(cat $log_file | grep -E "Error|Skipped")"
|
partial_errors="$(cat "$log_file" | grep -E "Error|Skipped")"
|
||||||
if [ ! -z "$partial_errors" ]; then
|
if [ -n "$partial_errors" ]; then
|
||||||
errors+="\nSome backup partially failed:\n$partial_errors"
|
errors+="\nSome backup partially failed:\n$partial_errors"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Send mail on backup (partially) failed
|
# Send mail on backup (partially) failed
|
||||||
domain=$(hostname)
|
domain=$(hostname)
|
||||||
repository="$(sudo yunohost app setting ${borg_id} repository)"
|
repository="$(sudo yunohost app setting "${borg_id}" repository)"
|
||||||
mailalert="$(sudo yunohost app setting ${borg_id} mailalert)"
|
mailalert="$(sudo yunohost app setting "${borg_id}" mailalert)"
|
||||||
if [[ ! -z "$errors" ]]; then
|
if [[ -n "$errors" ]]; then
|
||||||
sudo yunohost app setting ${borg_id} state -v "failed"
|
sudo yunohost app setting "${borg_id}" state -v "failed"
|
||||||
else
|
else
|
||||||
sudo yunohost app setting ${borg_id} state -v "successful"
|
sudo yunohost app setting "${borg_id}" state -v "successful"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ! -z "$errors" && $mailalert != "never" ]]; then
|
if [[ -n "$errors" && $mailalert != "never" ]]; then
|
||||||
cat <(echo -e "$errors\n\n\n") "$log_file" "$err_file" | mail -s "[borg] Backup failed from $domain onto $repository" root
|
cat <(echo -e "$errors\n\n\n") "$log_file" "$err_file" | mail -s "[borg] Backup failed from $domain onto $repository" root
|
||||||
exit 1
|
exit 1
|
||||||
elif [ $mailalert == "always" ]; then
|
elif [ "$mailalert" == "always" ]; then
|
||||||
cat $log_file | mail -s "[borg] Backup succeed from $domain onto $repository" root
|
cat "$log_file" | mail -s "[borg] Backup succeed from $domain onto $repository" root
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Add table
Reference in a new issue