-
Notifications
You must be signed in to change notification settings - Fork 3
/
xtrabackup.sh
executable file
·244 lines (186 loc) · 8.07 KB
/
xtrabackup.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/bash
die () {
echo -e 1>&2 "$@"
exit 1
}
fail () {
die "...FAILED! See $LOG_FILE for details - aborting.\n"
}
INNOBACKUPEX=$(which innobackupex)
[ -f "$INNOBACKUPEX" ] || die "innobackupex script not found - please ensure xtrabackup is installed before proceeding."
##CONTAINER=$(export | sed -nr "/ENV_MYSQL_DATABASE/{s/^.+ -x (.+)_ENV.+/\1/p;q}")
##DB_PORT=$(export | sed -nr "/-x ${CONTAINER}_PORT_[[:digit:]]+_TCP_PORT/{s/^.+ -x (.+)=.+/\1/p}")
##DB_ADDR="${CONTAINER}_PORT_${!DB_PORT}_TCP_ADDR"
##DB_NAME="${CONTAINER}_ENV_MYSQL_DATABASE"
##DB_PASS="${CONTAINER}_ENV_MYSQL_ROOT_PASSWORD"
CONFIG_FILE=/backups/.xtrabackup.config
if [ -f $CONFIG_FILE ]; then
echo -e "Loading configuration from $CONFIG_FILE."
source $CONFIG_FILE
else
cat << EOF > $CONFIG_FILE
MYSQL_HOST=mysql-host
MYSQL_PORT=3306
MYSQL_USER="$(whoami)"
MYSQL_PASS=
MYSQL_DATA_DIR=/var/lib/mysql/
BACKUPS_DIRECTORY=/backups/mysql-backups
MAX_BACKUP_CHAINS=8
EOF
die "Configuration has been initialised in $CONFIG_FILE. \nPlease make sure all settings are correctly defined/customised - aborting."
fi
[ -d $MYSQL_DATA_DIR ] || die "Please ensure the MYSQL_DATA_DIR setting in the configuration file points to the directory containing the MySQL databases."
[ -n "$MYSQL_USER" -a -n "$MYSQL_PASS" ] || die "Please ensure MySQL username and password are properly set in the configuration file."
FULLS_DIRECTORY=$BACKUPS_DIRECTORY/full
INCREMENTALS_DIRECTORY=$BACKUPS_DIRECTORY/incr
LOGS="/backups/logs"
mkdir -vp $FULLS_DIRECTORY
mkdir -vp $INCREMENTALS_DIRECTORY
mkdir -vp $LOGS
IONICE=$(which ionice)
if [ -n "$IONICE" ]; then
IONICE_COMMAND="$IONICE -c2 -n7"
fi
INNOBACKUPEX_COMMAND="$(which nice) -n 15 $IONICE_COMMAND $INNOBACKUPEX"
RSYNC_COMMAND="$(which nice) -n 15 $IONICE_COMMAND $(which rsync)"
full_backup () {
$INNOBACKUPEX_COMMAND --slave-info --host="$MYSQL_HOST" --port=$MYSQL_PORT --user="$MYSQL_USER" --password="$MYSQL_PASS" "$FULLS_DIRECTORY"
NEW_BACKUP_DIR=$(find $FULLS_DIRECTORY -mindepth 1 -maxdepth 1 -type d -exec ls -dt {} \+ | head -1)
echo $NEW_BACKUP_DIR > $NEW_BACKUP_DIR/backup.chain
}
incremental_backup () {
LAST_BACKUP=${LAST_CHECKPOINTS%/xtrabackup_checkpoints}
$INNOBACKUPEX_COMMAND --slave-info --host="$MYSQL_HOST" --port=$MYSQL_PORT --user="$MYSQL_USER" --password="$MYSQL_PASS" --incremental --incremental-basedir="$LAST_BACKUP" "$INCREMENTALS_DIRECTORY"
NEW_BACKUP_DIR=$(find $INCREMENTALS_DIRECTORY -mindepth 1 -maxdepth 1 -type d -exec ls -dt {} \+ | head -1)
cp $LAST_BACKUP/backup.chain $NEW_BACKUP_DIR/
echo $NEW_BACKUP_DIR >> $NEW_BACKUP_DIR/backup.chain
}
#
# Call before hooks
#
if [ -d "/hooks" ] && ls /hooks/*.before 1> /dev/null 2>&1; then
for hookfile in /hooks/*.before; do
eval $hookfile
echo "Called hook $hookfile"
done
fi
if [ "$1" = "full" ]; then
full_backup
elif [ "$1" = "incr" ]; then
LAST_CHECKPOINTS=$(find $BACKUPS_DIRECTORY -mindepth 3 -maxdepth 3 -type f -name xtrabackup_checkpoints -exec ls -dt {} \+ | head -1)
if [[ -f $LAST_CHECKPOINTS ]]; then
incremental_backup
else
full_backup
fi
elif [ "$1" = "list" ]; then
if [[ -d $FULLS_DIRECTORY ]]; then
BACKUP_CHAINS=$(ls $FULLS_DIRECTORY | wc -l)
else
BACKUP_CHAINS=0
fi
if [[ $BACKUP_CHAINS -gt 0 ]]; then
echo -e "Available backup chains (from oldest to latest):\n"
for FULL_BACKUP in `ls $FULLS_DIRECTORY -tr`; do
let COUNTER=COUNTER+1
echo "Backup chain $COUNTER:"
echo -e "\tFull: $FULL_BACKUP"
if [[ $(ls $INCREMENTALS_DIRECTORY | wc -l) -gt 0 ]]; then
grep -l $FULL_BACKUP $INCREMENTALS_DIRECTORY/**/backup.chain | \
while read INCREMENTAL;
do
BACKUP_DATE=${INCREMENTAL%/backup.chain}
echo -e "\tIncremental: ${BACKUP_DATE##*/}"
done
fi
done
LATEST_BACKUP=$(find $BACKUPS_DIRECTORY -mindepth 2 -maxdepth 2 -type d -exec ls -dt {} \+ | head -1)
[[ "$LATEST_BACKUP" == *full* ]] && IS_FULL=1 || IS_FULL=0
BACKUP_DATE=${LATEST_BACKUP##*/}
if [[ "$LATEST_BACKUP" == *full* ]]
then
echo -e "\nLatest backup available:\n\tFull: $BACKUP_DATE"
else
echo -e "\nLatest backup available:\n\tIncremental: $BACKUP_DATE"
fi
exit 1
else
die "No backup chains available in the backup directory specified in the configuration ($BACKUPS_DIRECTORY)"
fi
elif [ "$1" = "restore" ]; then
([ -n "$2" ] && [ -n "$3" ]) || die "Missing arguments. Please run as: \n\t$0 restore <timestamp> <destination folder>\nTo see the list of the available backups, run:\n\t$0 list"
BACKUP_TIMESTAMP="$2"
DESTINATION="$3"
BACKUP=`find $BACKUPS_DIRECTORY -mindepth 2 -maxdepth 2 -type d -name $BACKUP_TIMESTAMP -exec ls -dt {} \+ | head -1`
LOG_FILE="$LOGS/restore-$BACKUP_TIMESTAMP.log"
echo "" > $LOG_FILE
(mkdir -vp $DESTINATION) || die "Could not access destination folder $3 - aborting"
if [[ -d "$BACKUP" ]]; then
echo -e "!! About to restore MySQL backup taken on $BACKUP_TIMESTAMP to $DESTINATION !!\n"
if [[ "$BACKUP" == *full* ]]; then
echo "- Restore of full backup taken on $BACKUP_TIMESTAMP"
echo "Copying data files to destination..."
$RSYNC_COMMAND --quiet -ah --delete $BACKUP/ $DESTINATION &>> $LOG_FILE || fail
echo -e "...done.\n"
echo "Preparing the destination for use with MySQL..."
$INNOBACKUPEX_COMMAND --apply-log --ibbackup=xtrabackup_51 $DESTINATION &>> $LOG_FILE || fail
echo -e "...done.\n"
else
XTRABACKUP=$(which xtrabackup)
[ -f "$XTRABACKUP" ] || die "xtrabackup executable not found - this is required in order to restore from incrementals. Ensure xtrabackup is installed properly - aborting."
XTRABACKUP_COMMAND="$(which nice) -n 15 $IONICE_COMMAND $XTRABACKUP"
FULL_BACKUP=$(cat $BACKUP/backup.chain | head -1)
echo "- Restore of base backup from $FULL_BACKUP"
echo "Copying data files to destination..."
$RSYNC_COMMAND --quiet -ah --delete $FULL_BACKUP/ $DESTINATION &>> $LOG_FILE || fail
echo -e "...done.\n"
echo "Preparing the base backup in the destination..."
#$XTRABACKUP_COMMAND --prepare --apply-log-only --target-dir=$DESTINATION &>> $LOG_FILE || fail
$INNOBACKUPEX_COMMAND --apply-log --redo-only $DESTINATION &>> $LOG_FILE || fail
echo -e "...done.\n"
for INCREMENTAL in $(cat $BACKUP/backup.chain | tail -n +2); do
echo -e "Applying incremental from $INCREMENTAL...\n"
#$XTRABACKUP_COMMAND --prepare --apply-log-only --target-dir=$DESTINATION --incremental-dir=$INCREMENTAL &>> $LOG_FILE || fail
$INNOBACKUPEX_COMMAND --apply-log --redo-only $DESTINATION --incremental-dir=$INCREMENTAL &>> $LOG_FILE || fail
echo -e "...done.\n"
done
echo "Finalising the destination..."
#$XTRABACKUP_COMMAND --prepare --target-dir=$DESTINATION &>> $LOG_FILE || fail
$INNOBACKUPEX_COMMAND --apply-log $DESTINATION &>> $LOG_FILE || fail
echo -e "...done.\n"
fi
rm $LOG_FILE # no errors, no need to keep it
echo -e "The destination is ready. All you need to do now is:
- ensure the MySQL user owns the destination directory, e.g.: chown -R mysql:mysql $DESTINATION
- stop MySQL server
- replace the content of the MySQL datadir (usually /var/lib/mysql) with the content of $DESTINATION
- start MySQL server again"
else
die "Backup not found. To see the list of the available backups, run: $0 list"
fi
else
die "Backup type not specified. Please run: as $0 [incr|full|list|restore]"
fi
BACKUP_CHAINS=`ls $FULLS_DIRECTORY | wc -l`
if [[ $BACKUP_CHAINS -gt $MAX_BACKUP_CHAINS ]]; then
CHAINS_TO_DELETE=$(expr $BACKUP_CHAINS - $MAX_BACKUP_CHAINS)
for FULL_BACKUP in `ls $FULLS_DIRECTORY -t | tail -n $CHAINS_TO_DELETE`; do
grep -l $FULLS_DIRECTORY/$FULL_BACKUP $INCREMENTALS_DIRECTORY/**/backup.chain | while read incremental; do rm -rf "${incremental%/backup.chain}"; done
$IONICE_COMMAND rm -rf $FULLS_DIRECTORY/$FULL_BACKUP
done
fi
unset MYSQL_USER
unset MYSQL_PASS
#
# Call after hooks
#
if [ -d "/hooks" ] && ls /hooks/*.after 1> /dev/null 2>&1; then
for hookfile in /hooks/*.after; do
echo "===> Calling hook ${hookfile}... "
eval $hookfile
echo "===> Calling hook ${hookfile}... DONE"
done
echo "===> All hooks processed, finished."
else
echo "===> No hooks found, finished."
fi