Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CodiMD Backup and Restore

How to restore CodiMD

source

  1. Ensure a docker-compose.yml file exists inside a codimd directory.

  2. Put the restore.sh script near it.

  3. Follow the script below:

    docker-compose up -d database  # Notice the "Creating volume" message for uploads. Copy the volume name and use it as `uploads_volume`
    bash restore.sh path_to_backup.7z uploads_volume
    docker-compose up -d
    docker-compose exec -u root -w /home/hackmd/app/public/ codimd bash -c 'chown -vR hackmd:hackmd uploads && chmod -R 644 uploads && chmod 755 uploads'
#!/bin/bash
POSTGRES_USER=hackmd
POSTGRES_DB=hackmd
COMPOSE_DATA_VOLUME=upload-data
backups_dir="${1:?usage: $0 backups_dir}"
backups_dir="$(realpath "$backups_dir")"
compose_project="$(dirname "$(readlink -f "$0")")"
working_dir="/tmp/codimd-$(date +'%FT%H-%M-%S')"
script_dir="$(dirname "$0")"
mkdir -vp "$working_dir"
echo "--- Backuping the database..."
(cd "$compose_project" && /usr/local/bin/docker-compose exec -T database pg_dump $POSTGRES_DB -U $POSTGRES_USER) > "$working_dir/db.sql"
echo "--- Backuping uploads..."
/usr/bin/docker run --rm -v "$(basename "$compose_project")_$COMPOSE_DATA_VOLUME":/data \
-v "$working_dir":/bkp alpine \
sh -c "tar cf /bkp/uploads.tar /data && chown -v $UID:$UID /bkp/uploads.tar"
echo "--- Adding restore assets..."
cp "$script_dir/restore.sh" "$working_dir"
cp "$compose_project/docker-compose.yml" "$working_dir"
cp "$script_dir/README-CodiMD-Restore.md" "$working_dir"
echo "--- Archiving..."
/usr/bin/7zr a "$backups_dir/codimd-$(date +'%FT%H').7z" "$working_dir/*"
rm -rf "$working_dir"
#!/bin/bash
POSTGRES_USER=hackmd
error_msg="usage: $0 backup_path uploads_volume"
backup_path="${1:?$error_msg}"
uploads_volume="${2:?$error_msg}"
backup_path="$(realpath "$backup_path")"
compose_project="$(dirname "$(readlink -f "$0")")"
working_dir="/tmp/codimd-$(date +'%FT%H-%M-%S')"
mkdir -p "$working_dir"
echo "--- Extracting $backup_path to $working_dir..."
7zr x -o"$working_dir" "$backup_path"
echo "--- Restoring the database..."
cat "$working_dir/db.sql" | docker exec -i $(cd "$compose_project" && docker-compose ps -q database) psql -U $POSTGRES_USER
echo "--- Restoring uploads..."
docker run --rm -v "$uploads_volume":/data \
-v "$working_dir":/bkp alpine \
tar xf /bkp/uploads.tar
rm -rf "$working_dir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment