File size: 1,244 Bytes
cfea436
 
 
 
 
91cdb84
cfea436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91cdb84
 
cfea436
 
 
91cdb84
cfea436
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/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 "$@"