forked from nullart/debian-ubuntu-mariadb-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-mysql.sh
executable file
·90 lines (77 loc) · 3.33 KB
/
backup-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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
export LC_ALL=C
database_name="$1"
s3_space_name="$2"
backup_owner="$3"
parent_dir="/backups/mariadb"
defaults_file="/etc/mysql/${database_name}-backup.cnf"
todays_dir="$(date +%F)"
log_file="${parent_dir}/${database_name}/${todays_dir}/backup-progress.log"
now="$(date +%FT%H%MZ)"
processors="$(nproc --all)"
# Use this to echo to standard error
error () {
printf "%s: %s\n" "$(basename "${BASH_SOURCE}")" "${1}" >&2
exit 1
}
trap 'error "An unexpected error occurred."' ERR
sanity_check () {
# Check user running the script
if [ "$USER" != "$backup_owner" ]; then
error "Script can only be run as the \"$backup_owner\" user"
fi
}
set_options () {
# List the innobackupex arguments
innobackupex_args=(
"--defaults-file=${defaults_file}"
"--extra-lsndir=${parent_dir}/${database_name}/${todays_dir}"
"--backup"
"--compress"
"--stream=xbstream"
"--parallel=${processors}"
"--compress-threads=${processors}"
)
backup_type="full"
# Add option to read LSN (log sequence number) if a full backup has been
# taken today.
if grep -q -s "to_lsn" "${parent_dir}/${database_name}/${todays_dir}/xtrabackup_checkpoints"; then
backup_type="incremental"
lsn=$(awk '/to_lsn/ {print $3;}' "${parent_dir}/${database_name}/${todays_dir}/xtrabackup_checkpoints")
innobackupex_args+=( "--incremental-lsn=${lsn}" )
fi
}
take_backup () {
# Make sure today's backup directory is available and take the actual backup
mkdir -p "${parent_dir}/${database_name}/${todays_dir}"
find "${parent_dir}/${database_name}/${todays_dir}" -type f -name "*.incomplete" -delete
mariabackup "${innobackupex_args[@]}" "--target-dir=${parent_dir}/${database_name}/${todays_dir}" > "${parent_dir}/${database_name}/${todays_dir}/${backup_type}-${now}.xbstream.incomplete" 2> "${log_file}"
mv "${parent_dir}/${database_name}/${todays_dir}/${backup_type}-${now}.xbstream.incomplete" "${parent_dir}/${database_name}/${todays_dir}/${backup_type}-${now}.xbstream"
}
upload_backup () {
# Upload the backup file to an S3 compatible provider using s3cmd
if [ -e "${parent_dir}/${database_name}/${todays_dir}/${backup_type}-${now}.xbstream" ]
then
if [ -s "${parent_dir}/${database_name}/${todays_dir}/${backup_type}-${now}.xbstream" ]
then
if [ -r "${parent_dir}/${database_name}/${todays_dir}/${backup_type}-${now}.xbstream" ]
then
s3cmd -c "/etc/mysql/${s3_space_name}.s3cfg" -e put "${parent_dir}/${database_name}/${todays_dir}/${backup_type}-${now}.xbstream" "s3://${s3_space_name}/$HOSTNAME/${database_name}/${todays_dir}/"
else
echo "${parent_dir}/${database_name}/${todays_dir}/${backup_type}-${now}.xbstream is not readable"
fi
else
echo "${parent_dir}/${database_name}/${todays_dir}/${backup_type}-${now}.xbstream is zero in size"
fi
else
echo "${parent_dir}/${database_name}/${todays_dir}/${backup_type}-${now}.xbstream does not exist"
fi
}
sanity_check && set_options && take_backup && upload_backup
# Check success and print message
if tail -1 "${log_file}" | grep -q "completed OK"; then
printf "Backup successful!\n"
printf "Backup created at %s/%s-%s.xbstream\n" "${parent_dir}/${database_name}/${todays_dir}" "${backup_type}" "${now}"
else
error "Backup failure! Check ${log_file} for more information"
fi