common: Configured auto remove of backups

dehydrated
Felix Stupp 5 years ago
parent 32957fc899
commit 102de98479
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

@ -12,6 +12,7 @@ zocker_authorized_keys_url: "https://git.banananet.work/zocker.keys"
update_scripts_directory: "/root/update"
backup_gpg_fingerprint: "73D09948B2392D688A45DC8393E1BD26F6B02FB7"
backups_to_keep: 1
backups_directory: "/backups"
backups_files_directory: "{{ backups_directory }}/files"
backups_mysql_database_directory: "{{ backups_directory }}/mysql_databases"

@ -27,5 +27,14 @@
group: root
mode: "u=rwx,g=rx,o=rx"
loop:
- backup_autoremove.sh
- backup_files.sh
- backup_mysql_database.sh
- name: Configure auto remove older backups
cron:
hour: 1
minute: 55
job: "{{ global_helper_directory }}/backup_autoremove.sh"
name: "Auto remove older backups"
state: present

@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -euxo pipefail;
backupsToKeep={{ backups_to_keep }};
function onlyDatedFiles() {
grep --perl-regexp '/\d+(-\d+)*(\.[^/]+)*$';
}
function getDirName() {
grep --only-matching --perl-regexp '^.+(?=/[^/]+)';
}
find {{ backups_directory | quote }} -type f |
onlyDatedFiles |
getDirName |
sort --unique |
while read dir; do
find "$dir" --type f | onlyDatedFiles | sort --reverse | tail --lines=+$((backupsToKeep + 1)) | xargs rm;
done
Loading…
Cancel
Save