Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
# バックアップスクリプト
# コンテナの外で実行します。docker-composeでmongoコンテナを起動中なのが前提
set -o errexit
SRC_CONTAINER=$1
SRC_VOLUME_DIR=$2
OUTPUT_DIR=$3
WORK_DIR=/tmp/lets-chat-backup
mkdir -p $WORK_DIR/mongodb
mkdir -p $WORK_DIR/uploads
docker run -it --rm -v $WORK_DIR/mongodb:/backup --link $SRC_CONTAINER:mongodb mongo:3.2.6 \
/bin/bash -c 'mongodump --host $MONGODB_PORT_27017_TCP_ADDR -o/backup'
cp -rp $SRC_VOLUME_DIR/uploads $WORK_DIR
# バックアップファイルのオーナーがrootになるので、dockerに変更
# (idコマンドでホスト側のdockerのuid,gidを調べて渡している)
docker run -it --rm -v $WORK_DIR:/backup mongo:3.2.6 \
/bin/bash -c "chown -R `id -u`:`id -g` /backup"
tar cpzf $OUTPUT_DIR/lets-chat_backup_`date +%Y%m%d_%H%M%S`.tgz -C $WORK_DIR .
rm -rf $WORK_DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.