Skip to content

Commit af73172

Browse files
committed
Add --dest option to r2-restore.sh for custom restore path
1 parent 639e143 commit af73172

1 file changed

Lines changed: 37 additions & 12 deletions

File tree

deploy/r2-restore.sh

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# ./r2-restore.sh images # Restore all images
99
# ./r2-restore.sh all # Restore both database (latest) and images
1010
# ./r2-restore.sh list # List available backups
11+
# ./r2-restore.sh --dest /tmp/test latest # Restore to custom directory
1112

1213
set -euo pipefail
1314

@@ -19,8 +20,26 @@ if [ -f "$ENV_FILE" ]; then
1920
set +a
2021
fi
2122

23+
# Parse --dest option
24+
CUSTOM_DEST=""
25+
while [[ $# -gt 0 ]]; do
26+
case "$1" in
27+
--dest|-d)
28+
CUSTOM_DEST="$2"
29+
shift 2
30+
;;
31+
*)
32+
break
33+
;;
34+
esac
35+
done
36+
2237
# Configuration
23-
DATA_DIR="/srv/dogbook/data"
38+
if [ -n "$CUSTOM_DEST" ]; then
39+
DATA_DIR="$CUSTOM_DEST"
40+
else
41+
DATA_DIR="/srv/dogbook/data"
42+
fi
2443
DB_FILE="$DATA_DIR/keystone.db"
2544
IMAGES_DIR="$DATA_DIR/images"
2645
TMP_DIR="/tmp/dogbook-restore"
@@ -181,18 +200,20 @@ restore_database() {
181200
cp "$DB_FILE" "$current_backup"
182201
fi
183202

184-
# Stop service, restore, start service
203+
# Stop service, restore, start service (only for production path)
185204
local was_running=false
186-
if stop_service; then
187-
was_running=true
205+
if [ -z "$CUSTOM_DEST" ]; then
206+
if stop_service; then
207+
was_running=true
208+
fi
188209
fi
189210

190-
log "Restoring database..."
211+
log "Restoring database to $DB_FILE..."
191212
mkdir -p "$(dirname "$DB_FILE")"
192213
cp "$restored_db" "$DB_FILE"
193214

194-
# Set correct ownership (assuming dogbook user)
195-
if id "dogbook" &>/dev/null; then
215+
# Set correct ownership (assuming dogbook user, only for production)
216+
if [ -z "$CUSTOM_DEST" ] && id "dogbook" &>/dev/null; then
196217
chown dogbook:dogbook "$DB_FILE"
197218
fi
198219

@@ -209,7 +230,7 @@ restore_database() {
209230

210231
# Restore images
211232
restore_images() {
212-
log "Restoring images from R2..."
233+
log "Restoring images from R2 to $IMAGES_DIR..."
213234

214235
if ! confirm "This will sync all images from R2. Continue?"; then
215236
log "Aborted."
@@ -224,8 +245,8 @@ restore_images() {
224245
--transfers 4 \
225246
--progress
226247

227-
# Set correct ownership
228-
if id "dogbook" &>/dev/null; then
248+
# Set correct ownership (only for production)
249+
if [ -z "$CUSTOM_DEST" ] && id "dogbook" &>/dev/null; then
229250
chown -R dogbook:dogbook "$IMAGES_DIR"
230251
fi
231252

@@ -236,7 +257,10 @@ restore_images() {
236257
usage() {
237258
echo "Dogbook R2 Restore Script"
238259
echo ""
239-
echo "Usage: $0 <command> [options]"
260+
echo "Usage: $0 [--dest <dir>] <command> [options]"
261+
echo ""
262+
echo "Options:"
263+
echo " --dest, -d <dir> Restore to custom directory (default: /srv/dogbook/data)"
240264
echo ""
241265
echo "Commands:"
242266
echo " list List available backups"
@@ -251,9 +275,10 @@ usage() {
251275
echo " $0 db 2024-01-15 # Restore from January 15, 2024"
252276
echo " $0 images # Restore all images"
253277
echo " $0 all # Full restore (database + images)"
278+
echo " $0 --dest /tmp/test latest # Restore to /tmp/test for testing"
254279
echo ""
255280
echo "Notes:"
256-
echo " - The service will be stopped during database restore"
281+
echo " - The service will be stopped during database restore (unless --dest is used)"
257282
echo " - Current database is backed up before restoration"
258283
echo " - Images are synced (existing files not deleted unless missing from backup)"
259284
}

0 commit comments

Comments
 (0)