forked from ferthalangur/mariadb-backup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmariabackup.cron
153 lines (136 loc) · 4.78 KB
/
mariabackup.cron
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
#
# Script to run mariabackup on regular basis from cron.hourly
#
# Assumptions:
# - username and password is available in
# /etc/my.cnf.d/mariabackup.cnf ... putting it on the command line
# is worse for security than putting it in a file owned and readable
# only by the root user.
# Version:
# 1.1.0
# Source:
# https://github.com/ferthalangur/maria_db_backup
# License:
# GPL v.3 - See bottom of file.
###########################################################################
if [ -r /etc/default/mariabackup ]
then
source /etc/default/mariabackup
fi
# Defaults, if not set by the defaults file:
MARIABACKUP_HOUR="${MARIABACKUP_HOUR:-06 12 18}"
MARIABACKUP_PROG="${MARIABACKUP_PROG:-/bin/mariabackup}"
MARIABACKUP_OPTIONS="${MARIABACKUP_OPTIONS:---binloginfo=AUTO}"
MARIABACKUP_DEBUG="${MARIABACKUP_DEBUG:-0}"
MARIABACKUP_BASEDIR="${MARIABACKUP_BASEDIR:-/usr/local/backup/mariabackup}"
MARIABACKUP_CLEAN="${MARIABACKUP_CLEAN_OLD:-0}"
MARIABACKUP_CLEAN_DAYS="${MARIABACKUP_CLEAN_DAYS:-90}"
MARIABACKUP_COMPRESSION="${MARIABACKUP_COMPRESSION:-0}"
MARIABACKUP_STORE_SSH="${MARIABACKUP_STORE_SSH:-0}"
MARIABACKUP_STORE_SSH_BASEDIR="${MARIABACKUP_STORE_SSH_BASEDIR:-/usr/local/backup/mariabackup}"
MARIABACKUP_STORE_SSH_HOST="${MARIABACKUP_STORE_SSH_HOST:-127.0.0.1}"
MARIABACKUP_STORE_SSH_USER="${MARIABACKUP_STORE_SSH_USER:-root}"
function clean_backups()
{
if [ "${MARIABACKUP_CLEAN}" -eq "1" ]
then
clean_cmd="find ${MARIABACKUP_BASEDIR}/* -mtime +${MARIABACKUP_CLEAN_DAYS} -exec rm -rf {} +"
if [ "${MARIABACKUP_DEBUG}" -eq "1" ]
then
echo "DEBUG: Cleaning backups older than ${MARIABACKUP_CLEAN_DAYS} days"
fi
eval ${clean_cmd}
fi
return 0
}
function run_backup ()
{
local targetdir="$1"
local backup_cmd
local filename="backup_database_${datestamp}_${timestamp}"
local ssh_cmd="ssh ${MARIABACKUP_STORE_SSH_USER}@${MARIABACKUP_STORE_SSH_HOST} 'cat - > ${MARIABACKUP_STORE_SSH_BASEDIR}/${filename}"
backup_cmd="${MARIABACKUP_PROG} --backup --target-dir ${targetdir} ${MARIABACKUP_OPTIONS}"
if [ "${MARIABACKUP_STORE_SSH}" -eq "1" ]
then
case "$MARIABACKUP_COMPRESSION" in
"gzip")
backup_cmd="${backup_cmd} --stream=xbstream | gzip | ${ssh_cmd}.gz'"
;;
"bzip2")
backup_cmd="${backup_cmd} --stream=xbstream | bzip2 | ${ssh_cmd}.bz2'"
;;
"7z")
backup_cmd="${backup_cmd} --stream=xbstream | 7z a -si | ${ssh_cmd}.7z'"
;;
*)
backup_cmd="${backup_cmd} --stream=xbstream | ${ssh_cmd}'"
;;
esac
else
case "$MARIABACKUP_COMPRESSION" in
"gzip")
backup_cmd="${backup_cmd} --stream=xbstream | gzip > ${MARIABACKUP_BASEDIR}/${filename}.gz"
;;
"bzip2")
backup_cmd="${backup_cmd} --stream=xbstream | bzip2 > ${MARIABACKUP_BASEDIR}/${filename}.bz2"
;;
"7z")
backup_cmd="${backup_cmd} --stream=xbstream | 7z a -si ${MARIABACKUP_BASEDIR}/${filename}.7z"
;;
esac
fi
mkdir -p "${targetdir}"
if [ "${MARIABACKUP_DEBUG}" -eq "1" ]
then
debug_dump "${backup_cmd}"
fi
eval ${backup_cmd}
return 0
}
function debug_dump ()
{
local command="$1"
echo "DEBUG: Environment variables: "
set | egrep '^MARIABACKUP'
echo "DEBUG: Command to be run: ${command}"
}
if [ ! -x "${MARIABACKUP_PROG}" ]
then
echo "Error: unable to execute ${MARIABACKUP_PROG}"
exit 1
fi
if [ ! -d "${MARIABACKUP_BASEDIR}" ]
then
mkdir -p "${MARIABACKUP_BASEDIR}"
fi
datestamp=$(date "+%Y%m%d")
timestamp=$(date "+%H%M%S")
current_hour=$(date "+%H")
backup_dir="${MARIABACKUP_BASEDIR}/${datestamp}/${timestamp}"
for hour in ${MARIABACKUP_HOUR}
do
if [ "${hour}" -eq "${current_hour}" ]
then
run_backup "${backup_dir}"
clean_backups
exit
fi
done
exit
###########################################################################
# Copyright (C) 2018 Robert Bryan Jenson ... rbj.(at).spotch.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###########################################################################