[enh] Do not fail on backup and restore if some missing files are not mandatory (#576)

This commit is contained in:
Maniack Crudelis 2018-12-09 21:27:43 +01:00 committed by Alexandre Aubin
parent fa5a226339
commit 08818757cc

View file

@ -10,12 +10,13 @@ CAN_BIND=${CAN_BIND:-1}
# #
# If DEST is ended by a slash it complete this path with the basename of SRC. # If DEST is ended by a slash it complete this path with the basename of SRC.
# #
# usage: ynh_backup src [dest [is_big [arg]]] # usage: ynh_backup src [dest [is_big [not_mandatory [arg]]]]
# | arg: src - file or directory to bind or symlink or copy. it shouldn't be in # | arg: src - file or directory to bind or symlink or copy. it shouldn't be in
# the backup dir. # the backup dir.
# | arg: dest - destination file or directory inside the # | arg: dest - destination file or directory inside the
# backup dir # backup dir
# | arg: is_big - 1 to indicate data are big (mail, video, image ...) # | arg: is_big - 1 to indicate data are big (mail, video, image ...)
# | arg: not_mandatory - 1 to indicate that if the file is missing, the backup can ignore it.
# | arg: arg - Deprecated arg # | arg: arg - Deprecated arg
# #
# example: # example:
@ -46,6 +47,7 @@ ynh_backup() {
local SRC_PATH="$1" local SRC_PATH="$1"
local DEST_PATH="${2:-}" local DEST_PATH="${2:-}"
local IS_BIG="${3:-0}" local IS_BIG="${3:-0}"
local NOT_MANDATORY="${4:-0}"
BACKUP_CORE_ONLY=${BACKUP_CORE_ONLY:-0} BACKUP_CORE_ONLY=${BACKUP_CORE_ONLY:-0}
# If backing up core only (used by ynh_backup_before_upgrade), # If backing up core only (used by ynh_backup_before_upgrade),
@ -60,8 +62,8 @@ ynh_backup() {
# ============================================================================== # ==============================================================================
# Be sure the source path is not empty # Be sure the source path is not empty
[[ -e "${SRC_PATH}" ]] || { [[ -e "${SRC_PATH}" ]] || {
echo "!!! Source path '${SRC_PATH}' does not exist !!!" >&2 if [ "$NOT_MANDATORY" == "0" ]
then
# This is a temporary fix for fail2ban config files missing after the migration to stretch. # This is a temporary fix for fail2ban config files missing after the migration to stretch.
if echo "${SRC_PATH}" | grep --quiet "/etc/fail2ban" if echo "${SRC_PATH}" | grep --quiet "/etc/fail2ban"
then then
@ -69,6 +71,8 @@ ynh_backup() {
echo "The missing file will be replaced by a dummy one for the backup !!!" >&2 echo "The missing file will be replaced by a dummy one for the backup !!!" >&2
else else
return 1 return 1
else
return 0
fi fi
} }
@ -176,12 +180,13 @@ with open(sys.argv[1], 'r') as backup_file:
# Use the registered path in backup_list by ynh_backup to restore the file at # Use the registered path in backup_list by ynh_backup to restore the file at
# the good place. # the good place.
# #
# usage: ynh_restore_file ORIGIN_PATH [ DEST_PATH ] # usage: ynh_restore_file ORIGIN_PATH [ DEST_PATH [NOT_MANDATORY]]
# | arg: ORIGIN_PATH - Path where was located the file or the directory before # | arg: ORIGIN_PATH - Path where was located the file or the directory before
# to be backuped or relative path to $YNH_CWD where it is located in the backup archive # to be backuped or relative path to $YNH_CWD where it is located in the backup archive
# | arg: DEST_PATH - Path where restore the file or the dir, if unspecified, # | arg: DEST_PATH - Path where restore the file or the dir, if unspecified,
# the destination will be ORIGIN_PATH or if the ORIGIN_PATH doesn't exist in # the destination will be ORIGIN_PATH or if the ORIGIN_PATH doesn't exist in
# the archive, the destination will be searched into backup.csv # the archive, the destination will be searched into backup.csv
# | arg: NOT_MANDATORY - 1 to indicate that if the file is missing, the restore process can ignore it.
# #
# If DEST_PATH already exists and is lighter than 500 Mo, a backup will be made in # If DEST_PATH already exists and is lighter than 500 Mo, a backup will be made in
# /home/yunohost.conf/backup/. Otherwise, the existing file is removed. # /home/yunohost.conf/backup/. Otherwise, the existing file is removed.
@ -201,10 +206,16 @@ ynh_restore_file () {
local ARCHIVE_PATH="$YNH_CWD${ORIGIN_PATH}" local ARCHIVE_PATH="$YNH_CWD${ORIGIN_PATH}"
# Default value for DEST_PATH = /$ORIGIN_PATH # Default value for DEST_PATH = /$ORIGIN_PATH
local DEST_PATH="${2:-$ORIGIN_PATH}" local DEST_PATH="${2:-$ORIGIN_PATH}"
local NOT_MANDATORY="${3:-0}"
# If ARCHIVE_PATH doesn't exist, search for a corresponding path in CSV # If ARCHIVE_PATH doesn't exist, search for a corresponding path in CSV
if [ ! -d "$ARCHIVE_PATH" ] && [ ! -f "$ARCHIVE_PATH" ] && [ ! -L "$ARCHIVE_PATH" ]; then if [ ! -d "$ARCHIVE_PATH" ] && [ ! -f "$ARCHIVE_PATH" ] && [ ! -L "$ARCHIVE_PATH" ]; then
if [ "$NOT_MANDATORY" == "0" ]
then
ARCHIVE_PATH="$YNH_BACKUP_DIR/$(_get_archive_path \"$ORIGIN_PATH\")" ARCHIVE_PATH="$YNH_BACKUP_DIR/$(_get_archive_path \"$ORIGIN_PATH\")"
else
return 0
fi
fi fi
# Move the old directory if it already exists # Move the old directory if it already exists