#!/usr/bin/env bash # # docker-restore.sh - Restore a container from an archive created by docker-backup.sh # # Usage: # ./docker-restore.sh [--start] # # What it does: # 1. Extracts the archive to a temp dir # 2. Restores named volumes / bind mount directories # 3. Recreates the container via recreate.sh (if it was generated, requires jq at backup time) # 4. If a database dump is present, restores it into the running container # 5. With --start, starts the container after recreation (otherwise left stopped for review) # set -uo pipefail ARCHIVE="${1:-}" START_AFTER=0 [[ "${2:-}" == "--start" ]] && START_AFTER=1 [[ -n "$ARCHIVE" && -f "$ARCHIVE" ]] || { echo "Usage: $0 [--start]" >&2; exit 1; } command -v docker >/dev/null 2>&1 || { echo "docker is required" >&2; exit 1; } log() { printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"; } WORK="$(mktemp -d "${TMPDIR:-/tmp}/docker-restore.XXXXXX")" trap 'rm -rf "$WORK"' EXIT tar -xjf "$ARCHIVE" -C "$WORK" CONTAINER_DIR="$(find "$WORK" -mindepth 1 -maxdepth 1 -type d | head -n1)" [[ -n "$CONTAINER_DIR" ]] || { echo "unexpected archive layout" >&2; exit 1; } CONTAINER="$(basename "$CONTAINER_DIR")" log "restoring container '$CONTAINER' from $ARCHIVE" # Restore named volumes if [[ -d "$CONTAINER_DIR/volumes" ]]; then for voldir in "$CONTAINER_DIR/volumes"/*/; do [[ -d "$voldir" ]] || continue volname="$(basename "$voldir")" log "recreating volume '$volname' and restoring its data" docker volume inspect "$volname" >/dev/null 2>&1 || docker volume create "$volname" >/dev/null docker run --rm -v "${volname}:/__dst" -v "${voldir}:/__src:ro" \ alpine:latest sh -c 'cp -a /__src/. /__dst/' done fi # Restore bind mounts (print instructions, do not blindly overwrite host paths) if [[ -d "$CONTAINER_DIR/binds" ]]; then log "bind mount data extracted to: $CONTAINER_DIR/binds" log "copy the relevant subfolders back to their original host paths manually (see inspect.json for original Source paths)" fi # Recreate the container if [[ -x "$CONTAINER_DIR/recreate.sh" ]]; then log "recreating container via recreate.sh" (cd "$CONTAINER_DIR" && ./recreate.sh) else log "no recreate.sh found, use $CONTAINER_DIR/inspect.json to recreate the container manually" fi # Restore database dump, if present if [[ -d "$CONTAINER_DIR/db" ]]; then dumpfile="$(find "$CONTAINER_DIR/db" -type f | head -n1)" if [[ -n "$dumpfile" ]]; then log "found database dump: $dumpfile" log "waiting for container '$CONTAINER' to accept connections before restoring (manual confirmation may be needed)" case "$dumpfile" in *.sql) case "$dumpfile" in *postgres*) log "run: docker exec -i $CONTAINER psql -U postgres < $dumpfile" ;; *) log "run: docker exec -i $CONTAINER sh -c 'mysql -uroot -p\$MYSQL_ROOT_PASSWORD' < $dumpfile" ;; esac ;; *.archive.gz) log "run: docker exec -i $CONTAINER mongorestore --archive --gzip < $dumpfile" ;; *.rdb) log "stop the container, copy $dumpfile to its data dir as dump.rdb, then start it" ;; esac fi fi if [[ "$START_AFTER" -eq 1 ]]; then docker start "$CONTAINER" >/dev/null 2>&1 || true fi log "restore steps completed. Review any manual steps printed above."