-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrestore-db.sh
More file actions
executable file
·48 lines (38 loc) · 1.27 KB
/
Copy pathrestore-db.sh
File metadata and controls
executable file
·48 lines (38 loc) · 1.27 KB
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
41
42
43
44
45
46
47
48
#!/bin/bash
# Get the directory where the script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd "$SCRIPT_DIR"
# Enable debug output
set -x
# Create data directory if it doesn't exist
mkdir -p data
# Find any existing backups
backup_count=$(ls backups/juiceshop-*.sqlite 2>/dev/null | wc -l)
echo "Found $backup_count backup(s)"
# Check if this is a fresh install (no existing database and no backups)
if [ ! -f "data/juiceshop.sqlite" ] && [ "$backup_count" -eq 0 ]; then
echo "No existing database or backups found - allowing fresh database initialization"
exit 0
fi
# Find the most recent backup
latest_backup=$(ls -t backups/juiceshop-*.sqlite 2>/dev/null | head -n1)
if [ -z "$latest_backup" ]; then
echo "No backup found in backups/ directory"
exit 0
fi
# Stop the application if it's running (you may need to adjust this depending on how you run the app)
if pgrep -f "npm.*start" > /dev/null; then
echo "Stopping application..."
pkill -f "npm.*start"
fi
# Restore the backup
echo "Restoring from $latest_backup..."
cp "$latest_backup" data/juiceshop.sqlite
if [ $? -eq 0 ]; then
echo "Database restored successfully!"
else
echo "Error: Failed to restore database"
exit 1
fi
# Disable debug output
set +x