diff options
author | Erich Eckner <git@eckner.net> | 2018-10-09 22:10:22 +0200 |
---|---|---|
committer | Erich Eckner <git@eckner.net> | 2018-10-09 22:10:22 +0200 |
commit | ae97379c98c9d116b0fd062f61796b1652ba74c9 (patch) | |
tree | bf0b540df4002976c4376646ee9347fc48dd2ae9 | |
parent | a7117c08a9a8253f2fcb849199f1334be204ad74 (diff) | |
download | hardlinkedBackups-ae97379c98c9d116b0fd062f61796b1652ba74c9.tar.xz |
removeOldBackups: only remove backups older than one year
-rwxr-xr-x | removeOldBackups.in | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/removeOldBackups.in b/removeOldBackups.in index 67d0828..c1602d2 100755 --- a/removeOldBackups.in +++ b/removeOldBackups.in @@ -7,26 +7,33 @@ usage () { >&2 echo \ -'Usage: removeOldBackups -Remove old backups keeping only one backup per month for all but the present year. +'Usage: removeOldBackups [-n] +Remove backups older than one year, keeping only one backup per month. Options: -#HELPTEXT# #' +#HELPTEXT# # + -n only print what would be removed' [ -n "$1" ] && exit $1 exit 1 } +no_action=false + if [ $# -eq 1 ] then - if [ "$1" == "--help" ] + if [ "x$1" == 'x--help' ] then usage 0 - elif [ "$1" == "--version" ] + elif [ "x$1" == 'x--version' ] then >&2 echo '#VERSION#' exit 0 + elif [ "x$1" == 'x-n' ] + then + no_action=true + else + usage fi - usage elif [ $# -gt 1 ] then usage @@ -53,14 +60,19 @@ backups=$( sort -k5,5 -k4,4 -k3,3 -k2,2 -k1,1 | \ uniq -f2 printf '%s\n' "${backups}" | \ - sed ' - / '"$(date +%Y)"' \S\+$/p - ' + sed "$( + for dist in $(seq 0 20 365); do + printf '/ %s \S\+$/p\n' \ + "$(date '+%m %Y' -d@$(($(date +%s)-24*60*60*${dist})))" + done + )" } | \ cut -d' ' -f1 | \ sort | \ uniq -u | \ while read -r dir; do echo rm -rf --one-file-system "${dir}" - rm -rf --one-file-system "${dir}" + if ! ${no_action}; then + rm -rf --one-file-system "${dir}" + fi done |