summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichał Łyszczek <michal.lyszczek@bofc.pl>2020-12-08 20:50:20 +0100
committerMichał Łyszczek <michal.lyszczek@bofc.pl>2020-12-08 20:50:20 +0100
commitb9c1efadd95f1841b7850f77e458df873eb6f33f (patch)
treedef0d49b97f4bc22417bc423bafb7a68e5007814
parenta17132ec00687fea535700e8511ec2bf8f749855 (diff)
downloadcrons-b9c1efadd95f1841b7850f77e458df873eb6f33f.tar.gz
crons-b9c1efadd95f1841b7850f77e458df873eb6f33f.tar.bz2
crons-b9c1efadd95f1841b7850f77e458df873eb6f33f.zip
clean-old-backups.sh: add new script to clean old backups
Signed-off-by: Michał Łyszczek <michal.lyszczek@bofc.pl>
-rw-r--r--Makefile3
-rwxr-xr-xclean-old-backups.sh60
-rw-r--r--etc/clean-old-backups.conf8
3 files changed, 70 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 6904b24..62c7442 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,8 @@ PREFIX ?= /usr/local
SCRIPTDIR = $(PREFIX)/etc/crons
CONFDIR = /etc/crons.conf.d
-CRONS = portage-backup rootfs-backup smart-monitor disk-usage-monitor
+CRONS = portage-backup rootfs-backup smart-monitor disk-usage-monitor \
+ clean-old-backups
CRONS_SCRIPTS = $(addsuffix .sh, $(CRONS))
CRONS_CONFIGS = $(shell find etc/ -mindepth 1)
diff --git a/clean-old-backups.sh b/clean-old-backups.sh
new file mode 100755
index 0000000..130f2f7
--- /dev/null
+++ b/clean-old-backups.sh
@@ -0,0 +1,60 @@
+#!/bin/bash
+
+#. /etc/crons.conf.d/backup.conf
+#. /etc/crons.conf.d/clean-old-backups.conf
+
+. etc/backup.conf
+. etc/clean-old-backups.conf
+
+MOD=400
+export LANG=en_US
+
+## ==========================================================================
+# Removes unfinished backups. This may happen when backup has been
+# interrupted by power lost or out of memory
+## ==========================================================================
+
+
+remove_unfinished()
+{
+ path=$1
+
+ # when backup is being created it has +w mode, like 640,
+ # finished backups have +w stripped and mode set to
+ # $MOD from backup.conf. If backup process has been
+ # canceled or interupted, such file will not have
+ # proper mode
+ find $path -type f -not -perm $MOD | xargs rm -f
+}
+
+
+## ==========================================================================
+# Functions removes files older than specified by config, but makes sure
+# at least N backups are always there - no matther the date
+## ==========================================================================
+
+
+remove_old()
+{
+ path=$1
+
+ untouchable=$(find $path -type f -not -name ".*" |
+ sort -n |tail -n$((CLEAN_BACKUP_LEAVE_AT_LEAST)))
+ to_delete=$(find $path -type f -not -name ".*" -mtime +$CLEAN_BACKUP_OLDER_THAN |
+ sort -nr)
+
+ # missing space between \n" and " is not by accident
+ # without it empty space is added after new line
+ untouchable_in_delete=$(echo -e "${untouchable}\n""${to_delete}" |
+ sort -n | uniq -d)
+ # basically to_delete set minus untouchable
+ unique=$(echo -e "${untouchable_in_delete}\n""${to_delete}" |
+ sort -n | uniq -u)
+ rm -f $unique
+}
+
+
+for d in $CLEAN_BACKUP_DIRS; do
+ remove_unfinished $d
+ remove_old $d
+done
diff --git a/etc/clean-old-backups.conf b/etc/clean-old-backups.conf
new file mode 100644
index 0000000..0c02cf8
--- /dev/null
+++ b/etc/clean-old-backups.conf
@@ -0,0 +1,8 @@
+# Remove files (backups) that are older than this number of days
+CLEAN_BACKUP_OLDER_THAN=7
+
+# But make sure at least this number of backups are left at all time
+CLEAN_BACKUP_LEAVE_AT_LEAST=7
+
+# List of directories to cleanup
+CLEAN_BACKUP_DIRS="/var/backup/rootfs"