Support database backup and restore
This commit is contained in:
parent
bba57e4648
commit
3904c76478
1 changed files with 28 additions and 4 deletions
32
Justfile
32
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
|
Loading…
Reference in a new issue