|
6 | 6 | # |
7 | 7 |
|
8 | 8 | # |
9 | | -# Manage remote backup |
| 9 | +# Manage configuration backups. |
10 | 10 | # |
| 11 | +# Enterprise units (type=enterprise) talk to my collect after the |
| 12 | +# migrate-to-my credential rotation. Community units (type=community) |
| 13 | +# keep using the legacy backupd.nethesis.it endpoint with the same |
| 14 | +# URL layout they have always used — backupd still accepts both |
| 15 | +# tenants behind the $TYPE/api/v2/backup/ path. |
| 16 | +# |
| 17 | +# Pipefail so the curl exit status survives the jq stage in `list`; |
| 18 | +# without it a HTTP error on the server would be masked by a successful |
| 19 | +# jq parse and ns.backup would report success to the UI. |
| 20 | +# |
| 21 | + |
| 22 | +set -o pipefail |
11 | 23 |
|
12 | 24 | function exit_error { |
13 | 25 | >&2 echo "[ERROR] $@" |
14 | 26 | exit 1 |
15 | 27 | } |
16 | 28 |
|
17 | 29 | function help { |
18 | | - >&2 echo "Usage: $0 <list|download|upload>" |
| 30 | + >&2 echo "Usage: $0 <list|download|upload|delete>" |
19 | 31 | >&2 echo "Commands:" |
20 | | - >&2 echo " - list: retrieve the list of available backups from remote server" |
21 | | - >&2 echo " - download <file> [output]: download the given backup, if 'output' is empty downloaded file will be named as as 'file'" |
22 | | - >&2 echo " - upload <file>: upload the given backup" |
| 32 | + >&2 echo " - list: fetch the list of backups stored for this system" |
| 33 | + >&2 echo " - download <id> [output]: download the backup <id>; defaults to writing to a file named <id>" |
| 34 | + >&2 echo " - upload <file>: upload a backup file" |
| 35 | + >&2 echo " - delete <id>: remove the backup <id>" |
23 | 36 | } |
24 | 37 |
|
25 | 38 | SYSTEM_ID=$(uci -q get ns-plug.config.system_id) |
26 | 39 | SYSTEM_SECRET=$(uci -q get ns-plug.config.secret) |
27 | 40 | TYPE=$(uci -q get ns-plug.config.type) |
28 | | -URL=$(uci -q get ns-plug.config.backup_url) |
29 | 41 |
|
30 | | -if [ -z "$SYSTEM_ID" ] || [ -z "$SYSTEM_SECRET" ] || [ -z "$URL" ]; then |
31 | | - exit_error "System ID, system secret or backup url not found. Please configure ns-plug." |
| 42 | +if [ -z "$SYSTEM_ID" ] || [ -z "$SYSTEM_SECRET" ]; then |
| 43 | + exit_error "System ID or system secret not found. Please configure ns-plug." |
| 44 | +fi |
| 45 | + |
| 46 | +cmd=${1:-list} |
| 47 | + |
| 48 | +if [ "$TYPE" = "enterprise" ]; then |
| 49 | + /usr/sbin/migrate-to-my |
| 50 | + |
| 51 | + COLLECT_URL=$(uci -q get ns-plug.config.collect_url) |
| 52 | + if [ -z "$COLLECT_URL" ]; then |
| 53 | + exit_error "Collect URL not set. Pre-migration unit — retry later." |
| 54 | + fi |
| 55 | + |
| 56 | + BASE="$COLLECT_URL/backups" |
| 57 | + # --fail-with-body: exit 22 on 4xx/5xx while still writing the body |
| 58 | + # so the caller can inspect the error payload. |
| 59 | + curl_args="--silent --location-trusted --fail-with-body --user $SYSTEM_ID:$SYSTEM_SECRET" |
| 60 | + |
| 61 | + case "$cmd" in |
| 62 | + list) |
| 63 | + # my returns {code, message, data: {backups: [...]}} on |
| 64 | + # success. Unwrap `data` so ns.backup can pass it through as |
| 65 | + # {values: <output>} without double-nesting. Fall back to an |
| 66 | + # empty list on failure. |
| 67 | + response=$(curl $curl_args "$BASE") |
| 68 | + echo "$response" | jq 'if .data and (.data.backups // empty) then .data else {backups: []} end' |
| 69 | + ;; |
| 70 | + download) |
| 71 | + file=$2 |
| 72 | + [ -z "$file" ] && exit_error "No file specified" |
| 73 | + output=${3-$file} |
| 74 | + curl $curl_args -o "$output" "$BASE/$file" |
| 75 | + ;; |
| 76 | + upload) |
| 77 | + file=$2 |
| 78 | + [ -z "$file" ] && exit_error "No file specified" |
| 79 | + curl $curl_args -X POST \ |
| 80 | + -H "Content-Type: application/octet-stream" \ |
| 81 | + -H "X-Filename: $(basename "$file")" \ |
| 82 | + --data-binary "@$file" \ |
| 83 | + "$BASE" |
| 84 | + ;; |
| 85 | + delete) |
| 86 | + file=$2 |
| 87 | + [ -z "$file" ] && exit_error "No file specified" |
| 88 | + curl $curl_args -X DELETE "$BASE/$file" |
| 89 | + ;; |
| 90 | + *) |
| 91 | + help |
| 92 | + ;; |
| 93 | + esac |
| 94 | + |
| 95 | + exit $? |
| 96 | +fi |
| 97 | + |
| 98 | +# Community (legacy): backupd.nethesis.it with the /$TYPE/api/v2/backup/ |
| 99 | +# URL layout. Unchanged from the pre-migration behaviour. |
| 100 | +URL=$(uci -q get ns-plug.config.backup_url) |
| 101 | +if [ -z "$URL" ]; then |
| 102 | + exit_error "Backup URL not set. Please configure ns-plug." |
32 | 103 | fi |
33 | 104 |
|
34 | 105 | curl_args="--silent --location-trusted --user $SYSTEM_ID:$SYSTEM_SECRET" |
35 | 106 | base_url="$URL/$TYPE/api/v2/backup/" |
36 | 107 |
|
37 | | -cmd=${1:-list} |
38 | | - |
39 | 108 | case "$cmd" in |
40 | 109 | list) |
41 | 110 | curl $curl_args $base_url |
42 | 111 | ;; |
43 | 112 | download) |
44 | 113 | file=$2 |
45 | | - if [ -z "$file" ]; then |
46 | | - exit_error "No file specified" |
47 | | - fi |
| 114 | + [ -z "$file" ] && exit_error "No file specified" |
48 | 115 | output=${3-$file} |
49 | 116 | curl $curl_args $base_url$file -J -o "$output" |
50 | | - ;; |
51 | | - upload) |
52 | | - file=$2 |
53 | | - if [ -z "$file" ]; then |
54 | | - exit_error "No file specified" |
55 | | - fi |
56 | | - curl $curl_args $base_url --upload-file $file |
57 | | - ;; |
58 | | - delete) |
59 | | - file=$2 |
60 | | - if [ -z "$file" ]; then |
61 | | - exit_error "No file specified" |
62 | | - fi |
63 | | - curl $curl_args -X DELETE $base_url$file |
64 | | - ;; |
65 | | - |
66 | | - *) |
67 | | - help |
68 | | - ;; |
| 117 | + ;; |
| 118 | + upload) |
| 119 | + file=$2 |
| 120 | + [ -z "$file" ] && exit_error "No file specified" |
| 121 | + curl $curl_args $base_url --upload-file $file |
| 122 | + ;; |
| 123 | + delete) |
| 124 | + file=$2 |
| 125 | + [ -z "$file" ] && exit_error "No file specified" |
| 126 | + curl $curl_args -X DELETE $base_url$file |
| 127 | + ;; |
| 128 | + *) |
| 129 | + help |
| 130 | + ;; |
69 | 131 | esac |
0 commit comments