Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup script duplicates files on a second server #382

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions backup_db.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
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)

# 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
docker cp incubator_db_1:/backup.dump /home/incubator/dumps/$(date +"%y-%m")/$(date +"%y-%m-%d").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 {} \;

# 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";
exit -1;
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