From 0f9f574bacad9a38a500ca6fc96ad94e00807fc0 Mon Sep 17 00:00:00 2001 From: Jacob Babor Date: Fri, 29 Mar 2024 16:11:34 -0500 Subject: [PATCH] Rework backup script to be more versatile This breaks the restore script but restoration from s3 should be easy enough now --- roles/backup/templates/s3backup.sh | 37 ++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/roles/backup/templates/s3backup.sh b/roles/backup/templates/s3backup.sh index b84c911..da42a06 100644 --- a/roles/backup/templates/s3backup.sh +++ b/roles/backup/templates/s3backup.sh @@ -46,6 +46,23 @@ if command -v ostree > /dev/null 2>&1; then done fi +# Helper functions +backup() { + # Takes a file or directory to backup and backs it up + [ -z "$1" ] && return 1 + + dir="$1" + echo "- $dir" + + nice -n 10 tar {{ backup_s3backup_tar_args }}{{ backup_s3backup_tar_args_extra }} \ + {% for item in backup_s3backup_exclude_list + backup_s3backup_exclude_list_extra %} + --exclude "{{ item }}" \ + {% endfor %} + "$dir" \ + | aws s3 cp --expected-size 274877906944 - \ + "s3://{{ backup_s3_bucket }}/{{ inventory_hostname }}/$dir/$(date "+{{ backup_dateformat }}").tar.gz" +} + # Tar up all items in the backup list, recursively, and pipe them straight # up to S3 if [ -n "${DIRS[*]}" ]; then @@ -57,14 +74,16 @@ if [ -n "${DIRS[*]}" ]; then {% for item in backup_s3backup_exclude_list + backup_s3backup_exclude_list_extra %} echo "- {{ item }}" {% endfor %} - echo "Will upload resultant backup to {{ backup_s3_bucket }}" - nice -n 10 tar {{ backup_s3backup_tar_args }}{{ backup_s3backup_tar_args_extra }} \ - {% for item in backup_s3backup_exclude_list + backup_s3backup_exclude_list_extra %} - --exclude "{{ item }}" \ - {% endfor %} - "${DIRS[@]}" \ - | aws s3 cp --expected-size 274877906944 - \ - "s3://{{ backup_s3_bucket }}/{{ inventory_hostname }}/$(date "+{{ backup_dateformat }}").tar.gz" + echo "Will upload resultant backups to {{ backup_s3_bucket }}" + for dir in "${DIRS[@]}"; do + if [ "$dir" == "/data" ]; then + for datadir in "$dir"/*; do + [ -e "$datadir" ] && backup "$datadir" + done + else + backup "$dir" + fi + done fi # Dump Postgres DBs, if possible @@ -86,6 +105,6 @@ if command -v psql > /dev/null 2>&1; then sudo -u postgres pg_dump "$db" \ | gzip -v9 \ | aws s3 cp - \ - "s3://{{ backup_s3_bucket }}/{{ inventory_hostname }}/$db-$(date "+{{ backup_dateformat }}").pgsql.gz" + "s3://{{ backup_s3_bucket }}/{{ inventory_hostname }}/pgdump/$db/$(date "+{{ backup_dateformat }}").pgsql.gz" done fi