From 8d23388e3f9818d0d6bf6b757f2ac1960639026f Mon Sep 17 00:00:00 2001 From: rstech209 <84983033+rstech209@users.noreply.github.com> Date: Thu, 2 Dec 2021 23:32:21 +0100 Subject: [PATCH] Bugs about backup and crashrecovery --- common/profile-sync-daemon.in | 55 +++++++++++++++-------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/common/profile-sync-daemon.in b/common/profile-sync-daemon.in index 8289470b..9e1b147f 100644 --- a/common/profile-sync-daemon.in +++ b/common/profile-sync-daemon.in @@ -283,7 +283,7 @@ ungraceful_state_check() { if [[ -d "$BACKUP" ]]; then if [[ -d $DIR ]]; then - echo "Unexpected state detected: we have $BACKUP, but $DIR already exists. Trying move $DIR to $DIR" "$DIR-old-profile-$NOW" + echo "Unexpected state detected: we have $BACKUP, but $DIR already exists. Trying move $DIR to $DIR-old-profile-$NOW" mv --no-target-directory "$DIR" "$DIR-old-profile-$NOW" fi @@ -295,10 +295,10 @@ ungraceful_state_check() { # at the time the user started psd could be many resync cycles # in the past - BACKUP_TIME=$(stat "$BACKUP" | grep Change | awk '{ print $2,$3 }') - BACK_OVFS_TIME=$(stat "$BACK_OVFS" | grep Change | awk '{ print $2,$3 }') + BACKUP_TIME=$(stat --format=%Z "$BACKUP") + BACK_OVFS_TIME=$(stat --format=%Z "$BACK_OVFS") - [[ $(date -d "$BACK_OVFS_TIME" "+%s") -ge $(date -d "$BACKUP_TIME" "+%s") ]] && + [[ $BACK_OVFS_TIME -ge $BACKUP_TIME ]] && TARGETTOKEEP="$BACK_OVFS" || TARGETTOKEEP="$BACKUP" @@ -329,6 +329,8 @@ ungraceful_state_check() { } cleanup() { + [[ "$#" -eq "0" ]] && keep_count=0 || keep_count="$1" + # cleanup and enforce merged; cleanup need arg (nb backup to keep) local browser for browser in "${BROWSERS[@]}"; do load_env_for "$browser" @@ -338,17 +340,19 @@ cleanup() { local CRASHArr=() while IFS= read -d '' -r backup; do CRASHArr=("${CRASHArr[@]}" "$backup") - done < <(find "${DIR%/*}" -type d -name '*crashrecovery*' -print0 | sort -r -z) - - if [[ ${#CRASHArr[@]} -gt 0 ]]; then - echo -e "${BLD}Deleting ${#CRASHArr[@]} crashrecovery dir(s) for profile ${BLU}$DIR${NRM}" - for backup in "${CRASHArr[@]}"; do - echo -e "${BLD}${RED} $backup${NRM}" - rm -rf "$backup" - done - else - echo -e "${BLD}Found no crashrecovery dirs for: ${BLU}$DIR${NRM}${BLD}${NRM}" + done < <(find "${DIR%/*}" -maxdepth 1 -type d -name "${item##*/}*crashrecovery*" -print0 | sort -r -z) + + remove_count=$(( ${#CRASHArr[@]} - keep_count )) + if [[ $remove_count -le 0 ]]; then + [[ $keep_count -eq 0 ]] && echo -e "${BLD}Found no crashrecovery dirs for: ${BLU}$DIR${NRM}${BLD}${NRM}" + continue fi + + echo -e "${BLD}Deleting ${remove_count} crashrecovery dir(s) for profile ${BLU}$DIR${NRM}" + for backup in "${CRASHArr[@]:$keep_count}"; do + echo -e "${BLD}${RED} $backup${NRM}" + rm -rf "$backup" + done echo done done @@ -543,6 +547,7 @@ do_sync() { echo "USE_SUSPSYNC=\"$USE_SUSPSYNC\"" echo "BROWSERS=\"${BROWSERS[*]}\"" echo "USE_BACKUPS=\"$USE_BACKUPS\"" + echo "BACKUP_LIMIT=\"$BACKUP_LIMIT\"" } >> "$PSDCONFDIR/.psd.conf" chmod 400 "$PSDCONFDIR/.psd.conf" fi @@ -553,22 +558,6 @@ do_sync() { done } -enforce() { - local browser - for browser in "${BROWSERS[@]}"; do - local CRASHArr=() - while IFS= read -d '' -r backup; do - CRASHArr=("${CRASHArr[@]}" "$backup") - done < <(find "${DIR%/*}" -type d -name '*crashrecovery*' -print0 | sort -r -z) - - if [[ ${#CRASHArr[@]} -gt $BACKUP_LIMIT ]]; then - for remove in "${CRASHArr[@]:$BACKUP_LIMIT}"; do - rm -rf "$remove" - done - fi - done -} - do_unsync() { rm -f "$PID_FILE" "$PSDCONFDIR/.psd.conf" @@ -655,7 +644,7 @@ parse() { local CRASHArr=() while IFS= read -d '' -r backup; do CRASHArr=("${CRASHArr[@]}" "$backup") - done < <(find "${DIR%/*}" -type d -name '*crashrecovery*' -print0 | sort -r -z) + done < <(find "${DIR%/*}" -maxdepth 1 -type d -name "${item##*/}*crashrecovery*" -print0 | sort -r -z) # get permissions on profile dir and be smart about it since symlinks are all 777 [[ -f $PID_FILE ]] && TRUEP=$(stat -c %a "$BACKUP") || TRUEP=$(stat -c %a "$DIR") @@ -730,6 +719,7 @@ case "$1" in ungraceful_state_check header parse + cleanup "$BACKUP_LIMIT" ;; c|C|clean|Clean) dep_check @@ -746,6 +736,7 @@ case "$1" in dup_check running_check ungraceful_state_check + cleanup "$BACKUP_LIMIT" [[ $SUSPSYNC -eq 1 ]] && take_inhibit_lock echo -e "${BLD}psd startup check successful${NRM}" fi @@ -770,7 +761,7 @@ case "$1" in running_check ungraceful_state_check do_sync - enforce + cleanup "$BACKUP_LIMIT" fi ;; unsync)