-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-kopia.sh
executable file
·74 lines (60 loc) · 2.21 KB
/
backup-kopia.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
69
70
71
72
73
74
#!/bin/bash -l
set -o errexit
# If we are not the backup app, let's stop
if [[ "$BACKUP_APP" != "$APP_ID" ]]; then
echo "The backup app is incorrect. Both APP_ID and BACKUP_APP are different." 1>&2
exit 0
fi
# Sure?
if [[ "$BACKUP_PLEASE_MY_LOVELY_SCRIPT" != "true" ]]; then
echo "This app is running without the safety flag BACKUP_PLEASE_MY_LOVELY_SCRIPT set to true. It won't backup."
exit 0
fi
# Do not backup twice in a row, let's be sure of that!
clever login --token $CLEVER_TOKEN --secret $CLEVER_SECRET
clever link --alias _myself $APP_ID
clever env --alias _myself set BACKUP_PLEASE_MY_LOVELY_SCRIPT false
cd $APP_HOME
date=`date '+%Y%m%d%H%M%S'`
# Check if connected repository or try to connect to it
kopia repository status || kopia repository connect s3 \
--bucket=$BACKUP_BUCKET \
--access-key=$CELLAR_ADDON_KEY_ID \
--secret-access-key=$CELLAR_ADDON_KEY_SECRET \
--endpoint=$CELLAR_ADDON_HOST \
--override-hostname=${APP_ID}
SERVICE_EXIT_STATUS=$?
if [ $SERVICE_EXIT_STATUS -ne 0 ];then
echo "Error while connecting to the repository"
exit 1
fi
# Backup MySQL databases
# Format to use: BACKUP_MYSQL(_[0-9]+)?=user:pass:host:port:database:alias
mkdir -p $APP_HOME/db-backups
env | grep BACKUP_MYSQL | while read line; do
IFS=":" read -r user pass host port db alias <<< `echo $line | awk -F= {'print $2'}`
# Dump & Upload
mysqldump -v \
--extended-insert \
--single-transaction \
--quick \
--no-tablespaces \
--column-statistics=0 \
-h$host -P$port -u$user -p$pass $db \
| gzip > $APP_HOME/db-backups/$alias.$db.$date.sql.gz
kopia snapshot create $APP_HOME/db-backups
done;
# Backup filesystems
env | grep BACKUP_PATH | while read -r line; do
read backupPath <<< `echo $line | awk -F= {'print $2'}`
# @TODO: move ignore list in ENV variable
kopia policy set --add-ignore cache ${APP_HOME}/${backupPath}
kopia snapshot create ${APP_HOME}/${backupPath}
done;
# Verifying Validity of Snapshots
kopia snapshot verify
SERVICE_EXIT_STATUS=$?
if [ $SERVICE_EXIT_STATUS -ne 0 ];then
echo "Please check the validity the snapshots and fix them: https://kopia.io/docs/advanced/consistency/#repairing-corruption"
exit 2
fi;