-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase-mysql.sh
70 lines (57 loc) · 2.28 KB
/
database-mysql.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
#!/bin/bash
# -------------------------------------------------------------------------------------
# A backup and restore strategy for MySQL
# -------------------------------------------------------------------------------------
check_command "mysqldump"
check_command "mysql"
#Check if BITBUCKET_BACKUP_DB is a file or directory
if [ "${BITBUCKET_BACKUP_DB: -1}" == '/' ]; then
BITBUCKET_BACKUP_DB_FILE=${BITBUCKET_BACKUP_DB}backup-db.sql
else
BITBUCKET_BACKUP_DB_FILE=${BITBUCKET_BACKUP_DB}/backup-db.sql
fi
# Use -h option if MYSQL_HOST is set
if [ -n ${MYSQL_HOST} ]; then
MYSQL_HOST_CMD="-h ${MYSQL_HOST}"
fi
function prepare_backup_db {
check_config_var "BITBUCKET_DB"
check_config_var "BITBUCKET_BACKUP_DB"
check_config_var "MYSQL_USERNAME"
check_config_var "MYSQL_PASSWORD"
info "Prepared backup of DB ${BITBUCKET_DB} in ${BITBUCKET_BACKUP_DB}"
}
function backup_db {
run mysqldump --no-tablespaces ${MYSQL_HOST_CMD} ${MYSQL_BACKUP_OPTIONS} -u "${MYSQL_USERNAME}" -p"${MYSQL_PASSWORD}" \
--databases "${BITBUCKET_DB}" >"${BITBUCKET_BACKUP_DB_FILE}"
}
function prepare_restore_db {
check_config_var "BITBUCKET_DB"
check_var "BITBUCKET_RESTORE_DB"
check_config_var "MYSQL_USERNAME"
check_config_var "MYSQL_PASSWORD"
info "Prepared restore of ${BITBUCKET_DB}"
}
function restore_db {
BITBUCKET_RESTORE_DB_FILE=${BITBUCKET_RESTORE_DB}/backup-db.sql
run mysql ${MYSQL_HOST_CMD} -u "${MYSQL_USERNAME}" -p"${MYSQL_PASSWORD}" <"${BITBUCKET_RESTORE_DB_FILE}"
}
function cleanup_incomplete_db_backup {
info "Cleaning up DB backup created as part of failed/incomplete backup"
if [ -f "${BITBUCKET_BACKUP_DB_FILE}" ]; then
rm "${BITBUCKET_BACKUP_DB_FILE}"
fi
}
function cleanup_old_db_backups {
# Not required as old backups with this strategy are typically cleaned up in the archiving strategy.
no_op
}
# ----------------------------------------------------------------------------------------------------------------------
# Disaster recovery functions
# ----------------------------------------------------------------------------------------------------------------------
function promote_db {
bail "Disaster recovery is not available with this database strategy"
}
function setup_db_replication {
bail "Disaster recovery is not available with this database strategy"
}