Initial implementation of Time Machine style backup pruning.
Within 24 hours, all backups are kept. Within 1 month, only the most recent backup for each day is kept. For all previous backups, only the most recent of each month is kept. This is not *quite* the same as Time Machine, but this implementation is a lot easier to do since it is based on string comparisons of the dates and doesn't require any "hard" date logic. Also this commit just 'echo's what will be deleted, and does not actually delete anything yet because I am still testing it.
This commit is contained in:
@@ -31,6 +31,10 @@ trap 'fn_terminate_script' SIGINT
|
||||
# Source and destination information
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
fn_parse_date() {
|
||||
date -d "${1:0:10} ${1:11:2}:${1:13:2}:${1:15:2}" +%s
|
||||
}
|
||||
|
||||
SRC_FOLDER=${1%/}
|
||||
DEST_FOLDER=${2%/}
|
||||
EXCLUSION_FILE=$3
|
||||
@@ -82,6 +86,8 @@ DEST=$DEST_FOLDER/$NOW
|
||||
LAST_TIME=$(ls -1 -- "$DEST_FOLDER" | grep "$BACKUP_FOLDER_PATTERN" | tail -n 1)
|
||||
PREVIOUS_DEST=$DEST_FOLDER/$LAST_TIME
|
||||
INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress
|
||||
KEEP_ALL_DATE=$(date -d '-1 day' +%s)
|
||||
KEEP_DAILIES_DATE=$(date -d '-1 month' +%s)
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Create profile folder if it doesn't exist
|
||||
@@ -140,6 +146,32 @@ while [ "1" ]; do
|
||||
mkdir -p -- "$DEST"
|
||||
fi
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Purge certain old backups before beginning new backup.
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
for date in $(ls -1 -- "$DEST_FOLDER" | grep "$BACKUP_FOLDER_PATTERN" | sort -r); do
|
||||
stamp=$(fn_parse_date $date)
|
||||
|
||||
# Skip if failed to parse date...
|
||||
[ -n "$stamp" ] || continue
|
||||
|
||||
if [ $stamp -ge $KEEP_ALL_DATE ]; then
|
||||
true
|
||||
|
||||
elif [ $stamp -ge $KEEP_DAILIES_DATE ]; then
|
||||
# Delete all but the most recent of each day.
|
||||
[ ${date:8:2} -eq ${prev:8:2} ] && echo rm -rf $DEST_FOLDER/$date
|
||||
|
||||
else
|
||||
# Delete all but the most recent of each month.
|
||||
[ ${date:5:2} -eq ${prev:5:2} ] && echo rm -rf $DEST_FOLDER/$date
|
||||
fi
|
||||
|
||||
prev=$date
|
||||
done
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Start backup
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user