Last active
September 26, 2017 09:37
-
-
Save roguesherlock/209509d2ddd99e915c67524f0ce7fc30 to your computer and use it in GitHub Desktop.
Backup script for postgres in docker container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
LOG="path/to/logs" | |
START_TIME=$(date +%s) | |
echo "Backup :: ScriptStart -- $(date '+%Y-%m-%d_%H-%M-%S')" >> $LOG | |
USER="postgres" | |
HOST="db" | |
BACKUP_DIR="path/to/backupdir/" | |
SRC_DIR="path/to/src/dir" | |
GZIP="$(which gzip)" | |
NOW=$(date '+%Y-%m-%d_%H-%M-%S') | |
[ ! -d "$BACKUP_DIR" ] && mkdir -p "$BACKUP_DIR" | |
FILE=$BACKUP_DIR/dump-$NOW.gz | |
echo "[INFO] docker exec $HOST pg_dumpall -c -U $USER | $GZIP -9 > $FILE 2>> $LOG" >> $LOG | |
cd $SRC_DIR | |
docker exec $HOST pg_dumpall -c -U $USER | $GZIP -9 > $FILE 2>> $LOG | |
END_TIME=$(date +%s) | |
ELAPSED_TIME=$(expr $END_TIME - $START_TIME) | |
echo "Backup :: ScriptEnd -- $(date '+%Y-%m-%d_%H-%M-%S')" >> $LOG | |
echo "Elapsed Time :: $(date -d 00:00:$ELAPSED_TIME '+%H-%M-%S')" >> $LOG | |
########################################################################### | |
########################### TO RESTORE #################################### | |
# | |
# gzip -dc $FILE | docker exec -i $HOST psql -U $USER | |
# | |
########################################################################### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment