From 3904c764784c7a64c647f9545b7061febe53118c Mon Sep 17 00:00:00 2001 From: Awiteb Date: Fri, 3 May 2024 13:22:42 +0300 Subject: [PATCH] Support database backup and restore --- Justfile | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/Justfile b/Justfile index bbfd6d7..c35bdc7 100644 --- a/Justfile +++ b/Justfile @@ -7,19 +7,42 @@ # Stop the instance [aliases: s] @stop: - sudo docker-compose rm -f -s + sudo docker-compose stop # Restart the instance @restart: sudo docker-compose restart # Create Backup file [aliases: b] -@backup backup_name: stop && run +@backup backup_name: #!/usr/bin/env bash - FILES="data postgresdata Justfile docker-compose.yml README.md" + DATABASE_BACKUP_FILE="synapse-postgres-backup-$(date +%d-%m-%Y"_"%H%M%S).sql.gz" + sudo docker-compose exec postgresql bash -c "export PGPASSWORD=somepassword && pg_dump -U synapse synapse" | \ + gzip -9 > $DATABASE_BACKUP_FILE + FILES="data postgresdata Justfile docker-compose.yml README.md $DATABASE_BACKUP_FILE" 7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=32m -ms=on -mhe=on -p {{backup_name}}.7z $FILES + rm -fr $DATABASE_BACKUP_FILE echo "Backup done..." +# Restore Backup file +@restore backup_name: stop && run + #!/usr/bin/env bash + 7z x {{backup_name}}.7z -aoa + DATABASE_BACKUP_FILE=$(ls synapse-postgres-backup-*.sql.gz) + DATABASE_FILE=$(echo $DATABASE_BACKUP_FILE | sed 's/.gz//') + # Run the database to write the sql file to the container + sudo docker-compose up -d postgresql + + gunzip -k $DATABASE_BACKUP_FILE && \ + sudo docker cp $DATABASE_FILE postgresql_synapse:/pg-backup.sql && \ + sudo docker-compose exec postgresql bash -c "export PGPASSWORD=somepassword && dropdb -U synapse synapse --force && createdb -U synapse synapse && psql -U synapse synapse < pg-backup.sql" + rm -fr $DATABASE_FILE + rm -fr $DATABASE_BACKUP_FILE + + # Stop the database container (it will be started by the run command) + sudo docker-compose stop postgresql + echo "Restore done..." + [private] alias r := run @@ -27,4 +50,5 @@ alias r := run alias s := stop [private] alias b := backup - +[private] +alias re := restore \ No newline at end of file