-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-prodenv.sh
executable file
·52 lines (41 loc) · 1.19 KB
/
backup-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
#!/bin/bash
export COMPOSE_FILE=production.yaml
BACKUP_DIR="data"
DATE=$(date +"%Y-%m-%d_%H-%M-%S")
FILENAME="backup_$DATE.tar.gz"
TEMP_DIR=$(mktemp -d "/tmp/backup_$DATE.XXXXXX")
echo
echo "***********************************************"
echo "This script BACKUPS 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
fi
echo "Checking if the production environment is running..."
if ! docker compose ps | grep -q "Up"; then
echo "[ERROR] Production environment not running."
exit 1
fi
echo
echo "Exporting the database to a JSON file..."
docker compose exec -T django python manage.py dumpdata --exclude=contenttypes --indent 2 > "$TEMP_DIR/db_backup_$DATE.json"
echo
echo "Copying media files..."
mkdir -p "$TEMP_DIR/media"
docker cp -q "$(docker compose ps -q django):/app/media/." "$TEMP_DIR/media"
echo
echo "Creating the backup file..."
mkdir -p "$BACKUP_DIR"
cd "$BACKUP_DIR" || exit
tar -czf "$FILENAME" -C "$TEMP_DIR" .
echo
echo "Cleaning up..."
rm -rf "$TEMP_DIR"
echo
echo "Backup Created: $FILENAME!"
echo