#!/bin/sh set -eu RCLONE_CONFIG_PATH="${RCLONE_CONFIG:-/home/node/.config/rclone/rclone.conf}" DB_PATH="${DB_FILE:-/data/data.sqlite}" BACKUP_FILE="promptlib-latest.sqlite" join_rclone_target() { remote="$1" file="$2" case "$remote" in *:|*/) printf '%s%s' "$remote" "$file" ;; *) printf '%s/%s' "$remote" "$file" ;; esac } if [ -n "${RCLONE_CONF:-}" ]; then mkdir -p "$(dirname "$RCLONE_CONFIG_PATH")" printf '%s' "$RCLONE_CONF" > "$RCLONE_CONFIG_PATH" chmod 600 "$RCLONE_CONFIG_PATH" export RCLONE_CONFIG="$RCLONE_CONFIG_PATH" fi if [ -n "${RCLONE_BACKUP_REMOTE:-}" ]; then mkdir -p "$(dirname "$DB_PATH")" if [ ! -s "$DB_PATH" ] || [ "${RCLONE_RESTORE_FORCE:-}" = "1" ]; then if rclone lsf "$RCLONE_BACKUP_REMOTE" --files-only --include "$BACKUP_FILE" 2>/dev/null | grep -qx "$BACKUP_FILE"; then restore_source="$(join_rclone_target "$RCLONE_BACKUP_REMOTE" "$BACKUP_FILE")" echo "[restore] downloading $restore_source to $DB_PATH" rclone copyto "$restore_source" "$DB_PATH" else echo "[restore] no remote backup found at $(join_rclone_target "$RCLONE_BACKUP_REMOTE" "$BACKUP_FILE")" fi else echo "[restore] local database exists, skipping restore" fi fi exec "$@"