diff options
author | Erich Eckner <git@eckner.net> | 2018-10-09 21:13:54 +0200 |
---|---|---|
committer | Erich Eckner <git@eckner.net> | 2018-10-09 21:13:54 +0200 |
commit | a7117c08a9a8253f2fcb849199f1334be204ad74 (patch) | |
tree | e8a2c03ebe369c2de8db6543f07fa299a5983a54 | |
parent | 376295a42191a005483b54be19493b9e9676b445 (diff) | |
download | hardlinkedBackups-a7117c08a9a8253f2fcb849199f1334be204ad74.tar.xz |
removeOldBackups new
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 6 | ||||
-rwxr-xr-x | removeOldBackups.in | 66 |
3 files changed, 70 insertions, 3 deletions
@@ -2,6 +2,7 @@ backup backupStatistics fastRepair lastBackups +removeOldBackups man.commons *.common *.1 @@ -27,7 +27,7 @@ MANDIR = /usr/share/man VERSION = 1.2.2 -all: man.commons backup backup.1 lastBackups lastBackups.1 backupStatistics backupStatistics.1 +all: man.commons backup backup.1 backupStatistics backupStatistics.1 lastBackups lastBackups.1 removeOldBackups removeOldBackups.1 %: %.in sed " \ @@ -46,8 +46,8 @@ all: man.commons backup backup.1 lastBackups lastBackups.1 backupStatistics back .PHONY: install dist clean install: all - install -D -m0755 -t $(DESTDIR)$(BINDIR) backup backupStatistics lastBackups - install -D -m0644 -t $(DESTDIR)$(MANDIR)/man1 backup.1 lastBackups.1 backupStatistics.1 + install -D -m0755 -t $(DESTDIR)$(BINDIR) backup backupStatistics lastBackups removeOldBackups + install -D -m0644 -t $(DESTDIR)$(MANDIR)/man1 backup.1 backupStatistics.1 lastBackups.1 removeOldBackups.1 install -D -m0644 -t $(DESTDIR)$(ETCDIR) backup.conf clean: diff --git a/removeOldBackups.in b/removeOldBackups.in new file mode 100755 index 0000000..67d0828 --- /dev/null +++ b/removeOldBackups.in @@ -0,0 +1,66 @@ +#!/bin/bash + +# removeOldBackups version #VERSION# + +[ -r "#ETCDIR#/backup.conf" ] && \ + . "#ETCDIR#/backup.conf" + +usage () { + >&2 echo \ +'Usage: removeOldBackups +Remove old backups keeping only one backup per month for all but the present year. + +Options: +#HELPTEXT# #' + [ -n "$1" ] && exit $1 + exit 1 +} + +if [ $# -eq 1 ] +then + if [ "$1" == "--help" ] + then + usage 0 + elif [ "$1" == "--version" ] + then + >&2 echo '#VERSION#' + exit 0 + fi + usage +elif [ $# -gt 1 ] +then + usage +fi + +backups=$( + printf '%s\n' "${backups[@]}" | \ + cut -d' ' -f1 | \ + while read -r dir; do \ + if [ $(ls "$dir" | wc -l) -lt 30 ]; then + continue; + fi + ls -1 "$dir" | \ + sed -n ' + s,^\([0-9]\{4\}\)_\([0-9]\{2\}\)_\([0-9]\{2\}\)$,'"${dir}"'\0 \3 \2 \1 '"${dir}"', + T + p + ' + done +) + +{ + printf '%s\n' "${backups}" | \ + sort -k5,5 -k4,4 -k3,3 -k2,2 -k1,1 | \ + uniq -f2 + printf '%s\n' "${backups}" | \ + sed ' + / '"$(date +%Y)"' \S\+$/p + ' +} | \ + cut -d' ' -f1 | \ + sort | \ + uniq -u | \ + while read -r dir; do + echo rm -rf --one-file-system "${dir}" + rm -rf --one-file-system "${dir}" + done |