Browse Source
new: [mongo] added regular dump for backup
new: [mongo] added regular dump for backup
Signed-off-by: Valentin Lab <valentin.lab@kalysto.org>0k/dev/master
Valentin Lab
5 years ago
4 changed files with 104 additions and 1 deletions
-
4mongo/build/Dockerfile
-
43mongo/hooks/schedule_command-relation-joined
-
9mongo/metadata.yml
-
49mongo/resources/bin/mongo-backup
@ -0,0 +1,43 @@ |
|||
#!/bin/bash |
|||
|
|||
## When writing relation script, remember: |
|||
## - they should be idempotents |
|||
## - they can be launched while the dockers is already up |
|||
## - they are launched from the host |
|||
## - the target of the link is launched first, and get a chance to ``relation-set`` |
|||
## - both side of the scripts get to use ``relation-get``. |
|||
|
|||
. lib/common |
|||
|
|||
set -e |
|||
|
|||
label=${SERVICE_NAME}-backup |
|||
DST=$CONFIGSTORE/$TARGET_SERVICE_NAME/etc/cron/$label |
|||
schedule=$(relation-get schedule) |
|||
|
|||
if ! echo "$schedule" | egrep '^\s*(([0-9/,*-]+\s+){4,4}[0-9/,*-]+|@[a-z]+)\s*$' >/dev/null 2>&1; then |
|||
err "Unrecognized schedule '$schedule'." |
|||
exit 1 |
|||
fi |
|||
|
|||
exclude_dbs=$(relation-get exclude-dbs 2>/dev/null) || true |
|||
exclude_dbs=$(echo "$exclude_dbs" | shyaml get-values 2>/dev/null | |
|||
nspc) || true |
|||
|
|||
## Warning: 'docker -v' will use HOST directory even if launched from |
|||
## 'cron' container. |
|||
file_put "$DST" <<EOF |
|||
COMPOSE_LAUNCHER_OPTS=$COMPOSE_LAUNCHER_OPTS |
|||
|
|||
$schedule root lock $label -D -p 10 -c "\ |
|||
docker run --rm \ |
|||
-u 0 \ |
|||
-e MONGO_HOST=${SERVICE_NAME} \ |
|||
-e exclude_dbs=\"$exclude_dbs\" \ |
|||
--network ${PROJECT_NAME}_default \ |
|||
-v \"$HOST_CHARM_STORE/${CHARM_REL_PATH#${CHARM_STORE}/}/resources/bin/mongo-backup:/usr/sbin/mongo-backup\" \ |
|||
-v \"$SERVICE_DATASTORE/var/backups/mongo:/var/backups/mongo\" \ |
|||
--entrypoint mongo-backup \ |
|||
\"$DOCKER_BASE_IMAGE\"" 2>&1 | ts '\%F \%T \%Z' >> /var/log/cron/${label}_script.log |
|||
EOF |
|||
chmod +x "$DST" |
@ -0,0 +1,49 @@ |
|||
#!/bin/bash |
|||
|
|||
[ "$MONGO_HOST" ] || { |
|||
echo "You must define \$MONGO_HOST variable." >&2 |
|||
exit 1 |
|||
} |
|||
|
|||
|
|||
mongo_databases() { |
|||
echo "show dbs" | mongo --quiet --host "$MONGO_HOST" | cut -f 1 -d " " |
|||
} |
|||
|
|||
dbs=$(mongo_databases) |
|||
|
|||
BACKUP_ROOT=/var/backups/mongo |
|||
mkdir -p "$BACKUP_ROOT" |
|||
for db in $dbs; do |
|||
if [[ "$db" == "local" || "$db" == "config" ]]; then |
|||
continue |
|||
fi |
|||
for exclude_db in $exclude_dbs; do |
|||
if [ "$exclude_db" == "$db" ]; then |
|||
echo "No backup for database $db as it is listed in 'exclude-dbs'." |
|||
continue 2 |
|||
fi |
|||
done |
|||
|
|||
dst="$BACKUP_ROOT/$db" |
|||
[ -d "$dst.old" ] && rm -rf "$dst.old" |
|||
[ -d "$dst" ] && mv "$dst" "$dst.old" |
|||
mkdir -p "$dst.inprogress" |
|||
(( start = SECONDS )) |
|||
echo "Dumping database $db..." >&2 |
|||
mongodump --host "$MONGO_HOST" --db "$db" --out "$dst.inprogress" && |
|||
mv "$dst.inprogress/$db/"* "$dst.inprogress/" && |
|||
rmdir "$dst.inprogress/$db" |
|||
errlvl="${PIPESTATUS[0]}" |
|||
(( elapsed = SECONDS - start )) |
|||
if [ "$errlvl" != "0" ]; then |
|||
echo " !! Error when dumping database $db." >&2 |
|||
rm -rf "$dst.inprogress" && |
|||
[ -d "$dst.old" ] && rm -rf "$dst.old" |
|||
else |
|||
mv "$dst.inprogress" "$dst" |
|||
[ -d "$dst.old" ] && rm -rf "$dst.old" |
|||
printf " ..dumped %-35s (%12s in %10s)\n" "$db" "$(du -sh "$dst" | cut -f 1)" "${elapsed}s" >&2 |
|||
fi |
|||
done |
|||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue