-
Notifications
You must be signed in to change notification settings - Fork 7
/
operation.sh
executable file
·69 lines (50 loc) · 1.74 KB
/
operation.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
65
66
67
68
#!/bin/bash
"""
Usage: ./operation.sh [backup|deploy]
This script is used to perform operations on the server.
It can be used to backup/restore the database and media files, and to deploy to the server.
Can be automated using CI/CD cron jobs.
"""
#for container name and other variables
source .operation.env
#environment variables are needed for incase you need to backup the database with pg_dumpall
source .env
#this make sure that the backup of same day and are not misplaced
COMMON_BACKUP_SUBDIR="$(date +'%Y%m%d')"
mkdir -p "$BACKUP_DIR/$COMMON_BACKUP_SUBDIR"
backup_database() {
docker exec "$POSTGRES_CONTAINER_NAME" pg_dumpall -U "$POSTGRES_USER" > "$BACKUP_DIR/$COMMON_BACKUP_SUBDIR/$(date +'%Y%m%d%H%M%S')_postgres_backup.backup"
}
backup_media() {
docker exec "$MEDIA_CONTAINER_NAME" tar czf - /media/ > "$BACKUP_DIR/$COMMON_BACKUP_SUBDIR/$(date +'%Y%m%d%H%M%S')_media_backup.tar.gz"
}
# Deploy function
deploy() {
git pull origin master
docker compose down
docker compose -f docker-compose.prod.yml up -d --build
}
# Check command-line argument
if [ "$1" == "backup" ]; then
backup_database
backup_media
echo "Backup of media and database completed."
elif [ "$1" == "backup_media" ]; then
backup_media
echo "Deployment of server completed."
elif [ "$1" == "backup_database" ]; then
backup_database
echo "Backup of database completed."
elif [ "$1" == "deploy" ]; then
deploy
echo "Deployment to server completed."
elif [ "$1" == "backup_and_deploy" ]; then
backup_database
backup_media
deploy
echo "Backup and deployment to server completed."
else
echo "Invalid argument."
echo "Usage: $0 [backup|backup_media|backup_database|deploy|backup_and_deploy]"
exit 1
fi