-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore-prodenv.sh
executable file
·64 lines (51 loc) · 1.53 KB
/
restore-prodenv.sh
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
export COMPOSE_FILE=production.yaml
if [ $# -ne 1 ]; then
echo "Usage: $0 <backup_file>"
exit 1
fi
BACKUP_FILE=$1
TEMP_DIR=$(mktemp -d "/tmp/restore_XXXXXX")
echo
echo "************************************************"
echo "This script RESTORES the production environment!"
echo "************************************************"
echo
if [ -z "${AUTO_DEPLOY:-}" ]; then
read -r -p "Are you sure you want to continue? (yes/no) "
if [ "$REPLY" != "yes" ]; then
exit 0
fi
echo
read -r -p "Are you sure? (type production to continue) "
if [ "$REPLY" != "production" ]; then
exit 0
fi
echo
fi
echo "Checking if the production environment is running..."
if ! docker compose ps | grep -q "Up"; then
echo "[ERROR] The production environment is not running."
exit 1
fi
echo
echo "Extracting the backup file..."
tar -xzf "$BACKUP_FILE" -C "$TEMP_DIR"
echo
echo "Restoring media files..."
docker cp "$TEMP_DIR/media/." "$(docker compose ps -q django):/app/media"
echo
echo "Copying the database backup file to the Django container..."
DB_BACKUP_FILE=$(ls $TEMP_DIR/db_backup_*.json)
docker cp "$DB_BACKUP_FILE" "$(docker compose ps -q django):/tmp/db_backup.json"
echo
echo "Restoring the database from the JSON file..."
docker compose exec -T django python manage.py flush --no-input
docker compose exec -T django python manage.py loaddata /tmp/db_backup.json --exclude=contenttypes
echo
echo "Cleaning up..."
rm -rf "$TEMP_DIR"
docker compose exec -T django rm /tmp/db_backup.json
echo
echo "Restore Completed!"
echo