forked from r0bj/docker-mysqldump_backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·62 lines (54 loc) · 1.96 KB
/
backup.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
#!/bin/bash
prometheus_pushgateway_url=${PUSHGATEWAY_URL:-http://127.0.0.1:9091}
prometheus_job=${PROMETHEUS_JOB:-mysqldump-backup}
override_hostname=${OVERRIDE_HOSTNAME}
auth_user=${BASIC_AUTH_USER}
auth_password=${BASIC_AUTH_PASSWORD}
function write_log {
echo "`date +'%Y%m%d %H%M%S'`: $1"
}
function notify_prometheus {
local success="$1"
local duration="$2"
if [ -n "$auth_user" ] && [ -n "$auth_password" ]; then
curl_opts="-u ${auth_user}:$auth_password"
fi
if [ -n "$prometheus_pushgateway_url" ] && [ -n "$prometheus_job" ]; then
if [ "$success" -eq 1 ]; then
write_log "INFO: notify prometheus: backup success; duration: $duration"
cat <<EOF | curl -v --max-time 60 $curl_opts -s -XPOST --data-binary @- ${prometheus_pushgateway_url}/metrics/job/${prometheus_job}/instance/$hostname
# HELP mysqldump_backup_duration_seconds Duration of mysqldump backup
# TYPE mysqldump_backup_duration_seconds gauge
mysqldump_backup_duration_seconds $duration
# HELP mysqldump_backup_last_success_timestamp_seconds Unixtime mysqldump backup last succeeded
# TYPE mysqldump_backup_last_success_timestamp_seconds gauge
mysqldump_backup_last_success_timestamp_seconds $(date +%s.%7N)
# HELP mysqldump_backup_last_success Success of mysqldump backup
# TYPE mysqldump_backup_last_success gauge
mysqldump_backup_last_success 1
EOF
else
write_log "INFO: notify prometheus: backup failed"
cat <<EOF | curl -v --max-time 60 $curl_opts -s -XPOST --data-binary @- ${prometheus_pushgateway_url}/metrics/job/${prometheus_job}/instance/$hostname
# HELP mysqldump_backup_last_success Success of mysqldump backup
# TYPE mysqldump_backup_last_success gauge
mysqldump_backup_last_success 0
EOF
fi
fi
}
if [ -n "$override_hostname" ]; then
hostname=$override_hostname
else
hostname=$(hostname)
fi
start_timastamp=$(date +'%s')
bash /mysqldump-backup.sh
if [ "$?" -eq 0 ]; then
duration=$(($(date +'%s') - $start_timastamp))
notify_prometheus 1 $duration
exit 0
else
notify_prometheus 0
exit 1
fi