Skip to content

Instantly share code, notes, and snippets.

@knopki
Created November 15, 2025 17:10
Show Gist options
  • Select an option

  • Save knopki/556581e13a70c12279cb9b825e56fca9 to your computer and use it in GitHub Desktop.

Select an option

Save knopki/556581e13a70c12279cb9b825e56fca9 to your computer and use it in GitHub Desktop.
Convert kopia snapshots to restic snapshots with help of bubblewrap
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p bash restic kopia bubblewrap jq
set -euo pipefail
export KOPIA_CONF_DIR="$XDG_CONFIG_HOME/kopia"
export KOPIA_CONFIG_PATH="$KOPIA_CONF_DIR/kopia-main-repo-on-wdc3.config"
export KOPIA_PASSWORD="$(cat "$KOPIA_CONFIG_PATH.kopia-password" | base64 --decode)"
export RESTIC_PACK_SIZE=128
export RESTIC_PASSWORD_FILE=/run/secrets/restic-backupman-system-data-repo-password
export RESTIC_REPOSITORY=/run/media/sk/backups3/restic-system-data
export RESTIC_READ_CONCURRENCY=1
export GODEBUG=asyncpreemptoff=1
USERNAME=sk
HOST=alien
TMP_DIR=/tmp/kopia-backup
mkdir -p "$TMP_DIR"
umount "$TMP_DIR" || true
for TARGET in "/home/sk/.mozilla" "/home/sk/.thunderbird" "/home/sk/.zotero"; do
SNAPSHOT_NAME="$USERNAME@$HOST:$TARGET"
restic snapshots --path="$TARGET"
IFS=$'\n'
for line in $(kopia snapshot list "$SNAPSHOT_NAME" --json | jq '.[] | [.startTime, .rootEntry.obj, .source.path] | @sh'); do
DT=$(echo $line | cut -d"'" -f 2 | xargs date +%Y-%m-%d' '%H:%M:%S -d)
SNAP_ID=$(echo $line | cut -d"'" -f 4)
SPATH=$(echo $line | cut -d "'" -f 6)
echo "$DT, $SNAP_ID, $SPATH"
if [ "$TARGET" != "$SPATH" ]; then
echo "$TARGET != $SPATH; skipping"
continue
fi
kopia mount "$SNAP_ID" "$TMP_DIR" &
pid=$!
sleep 1
while [ -z "$(ls -A "$TMP_DIR")" ]; do
echo "Still not mounted..."
sleep 1
done
bwrap --bind / / --bind "$TMP_DIR" "$TARGET" --chdir "$TARGET" \
restic backup \
--no-scan --exclude-caches --ignore-ctime --ignore-inode \
--exclude-file=$HOME/.resticignore \
--time "$DT" --host "$HOST" --tag=imported .
kill $pid
sleep 1
done
done
umount "$TMP_DIR" || true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment