... | @@ -47,6 +47,47 @@ This will create a postgres backup file that ends with the name of the weekday i |
... | @@ -47,6 +47,47 @@ This will create a postgres backup file that ends with the name of the weekday i |
|
|
|
|
|
The `/path/to/backup/location` is a mount to a container volume the is accessible from outside the container. On there it could even be a mount to an external drive, cloud service. I tested it with StackStorage WebDAV but it should work similar for any file storage solution (e.g. Contabo's S3 😉?).
|
|
The `/path/to/backup/location` is a mount to a container volume the is accessible from outside the container. On there it could even be a mount to an external drive, cloud service. I tested it with StackStorage WebDAV but it should work similar for any file storage solution (e.g. Contabo's S3 😉?).
|
|
|
|
|
|
|
|
A more professional version that uses S3 is this script:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
if [ $# -ne 4 ]; then
|
|
|
|
echo "Usage: $0 <CONTAINER> <USER> <TARGETDIR> <PASSWORD>"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
export CONTAINER=$1
|
|
|
|
export USER=$2
|
|
|
|
export TARGETDIR=$3
|
|
|
|
PW=$4
|
|
|
|
|
|
|
|
export NAME=${CONTAINER}_$(date +'%Y%m%d')
|
|
|
|
export DBFILE=$TARGETDIR/$NAME.db
|
|
|
|
export LOGFILE=$TARGETDIR/$NAME.log
|
|
|
|
export ZIPFILE=$TARGETDIR/$NAME.zip
|
|
|
|
AWS_OPTS="--endpoint-url https://eu2.contabostorage.com"
|
|
|
|
|
|
|
|
#docker ps > $TARGETDIR/containers.txt
|
|
|
|
docker exec $CONTAINER vacuumdb -U $USER --all >$LOGFILE
|
|
|
|
|
|
|
|
echo Creating backup for $CONTAINER/$USER into $DBFILE...
|
|
|
|
docker exec $CONTAINER pg_dump -U $USER -Fc >$DBFILE 2>>$LOGFILE
|
|
|
|
|
|
|
|
echo Zipping into $ZIPFILE...
|
|
|
|
zip -e -P $PW $ZIPFILE $LOGFILE $DBFILE
|
|
|
|
|
|
|
|
echo Upload to S3...
|
|
|
|
aws $AWS_OPTS s3 cp $ZIPFILE s3://backups/$CONTAINER/$NAME.zip
|
|
|
|
```
|
|
|
|
|
|
|
|
Note that S3 requires credentials that can be stored in a file called `/home/devops/.aws/credentials/`. Here is an example content of that file (with fake keys of course):
|
|
|
|
|
|
|
|
```properties
|
|
|
|
[default]
|
|
|
|
aws_access_key_id = 6bd6bd1a6bb0464693d7b898b61acf21
|
|
|
|
aws_secret_access_key = 33c6dba1bd1346aba76238edbb980dfb
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Restore
|
|
## Restore
|
|
|
|
|
|
The restore can be done with command `pg_restore`. For instance
|
|
The restore can be done with command `pg_restore`. For instance
|
... | | ... | |