From e878a5be73c91580d11ed4276cb2ec257de5c05c Mon Sep 17 00:00:00 2001 From: Minigrim0 Date: Sun, 23 Oct 2022 15:43:01 +0200 Subject: [PATCH 1/4] [Upd] Backup script for duplication on second server --- backup_db.sh | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/backup_db.sh b/backup_db.sh index 75f9e4cd..c9aaf5bf 100755 --- a/backup_db.sh +++ b/backup_db.sh @@ -1,3 +1,22 @@ -mkdir -p /home/incubator/dumps/$(date +"%y-%m") +CURRENT_MONTH=`date +%Y%m` +CURRENT_DATE=`date +%Y%m%d` +BACKUP_DIR=/home/$(USER)/dumps + +# Create the backup file +mkdir -p /home/incubator/dumps/$(CURRENT_MONTH) docker-compose -f /home/incubator/docker/apps/incubator/docker-compose.yml exec db /usr/local/bin/pg_dump --dbname="incubator_db" --username="incubator" -f /backup.dump -docker cp incubator_db_1:/backup.dump /home/incubator/dumps/$(date +"%y-%m")/$(date +"%y-%m-%d").dump +docker cp incubator_db_1:/backup.dump /home/incubator/dumps/$(CURRENT_MONTH)/$(CURRENT_DATE).dump + +# Delete old backups (Keep for 90 days, all will be available on the second server) +find /home/incubator/dumps/ -type f -mtime +90 -exec rm {} \; + +# Check if the SECONDARY_SERVER exists +if [ -z ${SECONDARY_SERVER_HOST+x} ]; +then + echo "SECONDARY_SERVER_HOST is unset, can't copy the backup to the secondary server"; + return; +fi + +# Copy the backup file to the secondary server +ssh $(SECONDARY_SERVER_HOST) "mkdir -p /home/incubator/dumps/$(CURRENT_MONTH)" +scp /home/incubator/dumps/$(CURRENT_MONTH)/$(CURRENT_DATE).dump $(SECONDARY_SERVER_HOST):/home/incubator/dumps/$(CURRENT_MONTH)/$(CURRENT_DATE).dump \ No newline at end of file From 6c43946beb55692a80c4e4da218b9efeb4fb7ffe Mon Sep 17 00:00:00 2001 From: Minigrim0 Date: Sun, 23 Oct 2022 16:34:33 +0200 Subject: [PATCH 2/4] [Upd] Save medias too --- backup_db.sh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/backup_db.sh b/backup_db.sh index c9aaf5bf..d173803c 100755 --- a/backup_db.sh +++ b/backup_db.sh @@ -4,8 +4,16 @@ BACKUP_DIR=/home/$(USER)/dumps # Create the backup file mkdir -p /home/incubator/dumps/$(CURRENT_MONTH) + +# Dump the database docker-compose -f /home/incubator/docker/apps/incubator/docker-compose.yml exec db /usr/local/bin/pg_dump --dbname="incubator_db" --username="incubator" -f /backup.dump + +# Compress medias +docker-compose -f /home/incubator/docker/apps/incubator/docker-compose.yml exec nginx /bin/bash -c "tar -czf /backup.tar.gz /home/app/web/media" + +# Copy the backup files from docker docker cp incubator_db_1:/backup.dump /home/incubator/dumps/$(CURRENT_MONTH)/$(CURRENT_DATE).dump +docker cp incubator_nginx_1:/backup.tar.gz /home/incubator/dumps/$(CURRENT_MONTH)/media_$(CURRENT_DATE).tar.gz # Delete old backups (Keep for 90 days, all will be available on the second server) find /home/incubator/dumps/ -type f -mtime +90 -exec rm {} \; @@ -19,4 +27,5 @@ fi # Copy the backup file to the secondary server ssh $(SECONDARY_SERVER_HOST) "mkdir -p /home/incubator/dumps/$(CURRENT_MONTH)" -scp /home/incubator/dumps/$(CURRENT_MONTH)/$(CURRENT_DATE).dump $(SECONDARY_SERVER_HOST):/home/incubator/dumps/$(CURRENT_MONTH)/$(CURRENT_DATE).dump \ No newline at end of file +scp /home/incubator/dumps/$(CURRENT_MONTH)/$(CURRENT_DATE).dump $(SECONDARY_SERVER_HOST):/home/incubator/dumps/$(CURRENT_MONTH)/$(CURRENT_DATE).dump +scp /home/incubator/dumps/$(CURRENT_MONTH)/media_$(CURRENT_DATE).tar.gz $(SECONDARY_SERVER_HOST):/home/incubator/dumps/$(CURRENT_MONTH)/media_$(CURRENT_DATE).tar.gz \ No newline at end of file From 3ece0ef1b85840c5bf7e73657cb715203ef6631a Mon Sep 17 00:00:00 2001 From: Minigrim0 Date: Sun, 23 Oct 2022 17:09:04 +0200 Subject: [PATCH 3/4] [Upd] exit -1 when no secondary server is found --- backup_db.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backup_db.sh b/backup_db.sh index d173803c..cdfb4b53 100755 --- a/backup_db.sh +++ b/backup_db.sh @@ -22,7 +22,7 @@ find /home/incubator/dumps/ -type f -mtime +90 -exec rm {} \; if [ -z ${SECONDARY_SERVER_HOST+x} ]; then echo "SECONDARY_SERVER_HOST is unset, can't copy the backup to the secondary server"; - return; + exit -1; fi # Copy the backup file to the secondary server From e8daad5f2c7a7b9f7e339127676e3faee470bf75 Mon Sep 17 00:00:00 2001 From: Minigrim0 Date: Fri, 17 Feb 2023 12:55:21 +0100 Subject: [PATCH 4/4] [Fix] Newline at the end of the file --- backup_db.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backup_db.sh b/backup_db.sh index cdfb4b53..adad1436 100755 --- a/backup_db.sh +++ b/backup_db.sh @@ -28,4 +28,4 @@ fi # Copy the backup file to the secondary server ssh $(SECONDARY_SERVER_HOST) "mkdir -p /home/incubator/dumps/$(CURRENT_MONTH)" scp /home/incubator/dumps/$(CURRENT_MONTH)/$(CURRENT_DATE).dump $(SECONDARY_SERVER_HOST):/home/incubator/dumps/$(CURRENT_MONTH)/$(CURRENT_DATE).dump -scp /home/incubator/dumps/$(CURRENT_MONTH)/media_$(CURRENT_DATE).tar.gz $(SECONDARY_SERVER_HOST):/home/incubator/dumps/$(CURRENT_MONTH)/media_$(CURRENT_DATE).tar.gz \ No newline at end of file +scp /home/incubator/dumps/$(CURRENT_MONTH)/media_$(CURRENT_DATE).tar.gz $(SECONDARY_SERVER_HOST):/home/incubator/dumps/$(CURRENT_MONTH)/media_$(CURRENT_DATE).tar.gz