Skip to content

Commit

Permalink
feat: 增加备份工具
Browse files Browse the repository at this point in the history
  • Loading branch information
icyleaf committed Jan 14, 2020
1 parent 92c5567 commit 3ceaf89
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .backup-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pgdata*
.git*
.history
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
rootfs/
backups/
docker-compose.yml
.env
54 changes: 54 additions & 0 deletions backup
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash
set -e
# Backup tools
# this script generates a tgz file with the db dump, app uploads
#
# example: ./backup
# example: ./backup /path/to/your/backup/directory

SOURCE_DIR="."
DEST_DIR=${DEST_DIR:-$SOURCE_DIR}

BACKUP_DIR="$DEST_DIR/backups"
TODAY=`date '+%Y%m%d%H%M%S'`
DEST_FILE=$BACKUP_DIR/`hostname`-$TODAY.tar.gz

mkdir -p $BACKUP_DIR

backup_db () {
echo ""
echo "== Backup db =="
docker exec zealot-postgresql su - postgres -c 'pg_dump -O -Fc zealot -f /tmp/db.dump'
docker cp zealot-postgresql:/tmp/db.dump $BACKUP_DIR
}

backup_redis () {
echo ""
echo "== Backup redis =="
docker cp zealot-redis:/data/dump.rdb $BACKUP_DIR/redis.rdb
}

backup_zealot () {
echo ""
echo "== Backup zealot data =="
docker cp zealot-zealot:/app/public/uploads $BACKUP_DIR/uploads
}

compress () {
echo ""
echo "== Compress to .tar.gz file =="
tar cvzf $DEST_FILE -X $SOURCE_DIR/.backup-ignore $SOURCE_DIR
}

cleanup () {
echo
echo "== Cleaning up =="
rm -rf $BACKUP_DIR/uploads $BACKUP_DIR/db.dump $BACKUP_DIR/redis.rdb
}
trap cleanup ERR INT TERM HUP QUIT

backup_db
backup_redis
backup_zealot
compress
cleanup

0 comments on commit 3ceaf89

Please sign in to comment.