-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql-app-consistent-backup.sh
114 lines (96 loc) · 2.97 KB
/
mysql-app-consistent-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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
#
# (C) 2016, CAPSiDE SL. All rights reserved.
# Total or partial distribution, copy or reproduction in any form, or usage
# without written authorization from CAPSiDE is prohibited and will be
# prosecuted.
#
# Main script for PRE/POST actions for MySQL consistent Azure Linux VMs backups
# Default PATH
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/azure"
# Only root can execute this script
USER_ID="$(id -u)"
if [[ ${USER_ID} -ne 0 ]]; then
echo "Only root can execute this script."
exit 1
fi
# Load the custom variables and helper functions
for source_file in /etc/azure/mysql-app-consistent-backup-env.sh /etc/azure/mysql-app-consistent-backup-funcs.sh; do
if [[ -r ${source_file} ]]; then
source ${source_file}
if [[ $? -ne 0 ]]; then
echo "${source_file} could not be loaded."
exit 1
fi
else
echo "${source_file} not found."
exit 1
fi
done
# Folder structure
mkdir -p ${DUMPS_FOLDER} ${LOGS_FOLDER}
touch ${LOG_FILE}
chown -R root:root ${DUMPS_FOLDER} ${LOGS_FOLDER}
chmod -R 700 ${BASE_FOLDER}/*.sh ${DUMPS_FOLDER} ${LOGS_FOLDER}
# From now on the required environment is set up
# Get the server role
server_id="$(egrep "^\s*server-id\s*=\s*[0-9]*\s*$" ${MYSQL_CNF_FILE} | sed -e 's/^.*=\s*\([0-9]*\).*$/\1/')"
role=""
role="master"
#if [[ "${server_id}" -eq 1 ]]; then #MASTER
# role="master"
#elif [[ "${server_id}" -gt 1 ]]; then #SLAVE
# role="slave"
#else
# log_crit "Invalid role."
#fi
# Get the requested action
if [[ $# -lt 1 ]]; then
log_crit "No action provided."
else
case $1 in
pre|Pre|PRE)
action="PRE"
log_info "Starting ${action} actions for role ${role}"
if [[ $(mysql_svc_status) -ne 0 ]]; then
mysql_svc_start
fi
case ${role} in
master)
mysql_set_readonly
mysqldump_dump_all_dbs_and_tables
;;
slave)
mysql_slave_stop
mysql_set_readonly
mysqldump_dump_all_dbs_and_tables
mysql_svc_stop
;;
esac
;;
post|Post|POST)
action="POST"
log_info "Starting ${action} actions for role ${role}"
if [[ $(mysql_svc_status) -ne 0 ]]; then
mysql_svc_start
fi
case ${role} in
master)
mysql_unset_readonly
rm -rf ${DUMPS_FOLDER}/*
;;
slave)
if [[ $(mysql_svc_status) -ne 0 ]]; then
mysql_svc_start
fi
mysql_unset_readonly
mysql_slave_start
;;
esac
;;
*)
log_crit "Invalid action provided."
;;
esac
fi
log_info "${action} for role ${role} finished"