-
Notifications
You must be signed in to change notification settings - Fork 1
/
back_daily_db_wf.sh
48 lines (42 loc) · 1.32 KB
/
back_daily_db_wf.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
#!/bin/bash
#vars
#before run this script, make sure give it permission
#chmod +x back_daily_db.sh
#sudo apt-get install jq
BACKUP_DIR="/home/ubuntu/backup"
ADMIN_PASSWORD="admin123"
YEAR=$(date +%Y)
MONTH=$(date +%m)
DAY=$(date +%d)
# Create backup dir (-p to avoid warning if already exists)
mkdir -p "$BACKUP_DIR"
db_list=$(python3 - <<END
import erppeek
server_url = 'http://localhost:8069'
client = erppeek.Client(server_url)
databases = client.db.list()
print("\n".join(databases))
END
)
for db_name_ in $db_list; do
databases=$(curl -H "Content-Type: application/json" -d "{}" http://$db_name_.localhost:8069/web/database/list | jq -r '.result[]')
for db_name in $databases; do
BACKUP_NAME="${db_name}_${YEAR}_${MONTH}_${DAY}"
# Create a backup
curl -X POST \
-F "master_pwd=$ADMIN_PASSWORD" \
-F "name=$db_name" \
-F "backup_format=zip" \
-o "$BACKUP_DIR/$BACKUP_NAME.zip" \
http://localhost:8069/web/database/backup
done
done
# backup only hold for latest 10 days
find $BACKUP_DIR -type f -mtime +10 -name "*.zip" -delete
#guide to use
#before run this script, make sure give it permission
#chmod +x back_daily_db.sh
#create a cron job to run this script daily
#crontab -e
#backup at 3:30 UTC time
#30 3 * * * /home/ubuntu/back_daily_db.sh