Support database backup and restore

This commit is contained in:
Awiteb 2024-05-03 13:22:42 +03:00
parent bba57e4648
commit 3904c76478
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F

View file

@ -7,19 +7,42 @@
# Stop the instance [aliases: s] # Stop the instance [aliases: s]
@stop: @stop:
sudo docker-compose rm -f -s sudo docker-compose stop
# Restart the instance # Restart the instance
@restart: @restart:
sudo docker-compose restart sudo docker-compose restart
# Create Backup file [aliases: b] # Create Backup file [aliases: b]
@backup backup_name: stop && run @backup backup_name:
#!/usr/bin/env bash #!/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 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..." 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] [private]
alias r := run alias r := run
@ -27,4 +50,5 @@ alias r := run
alias s := stop alias s := stop
[private] [private]
alias b := backup alias b := backup
[private]
alias re := restore