Skip to content

Commit c17ddab

Browse files
keukoAlex-Welsh
authored andcommitted
[mariadb] Fix incremental backup using correct base dir
The original script used --incremental-history-name and compressed full backups as gzip streams. This failed because mariabackup expects an actual decompressed base directory for incremental backups. This patch: - Decompresses the latest full backup into a temporary directory - Uses that as --incremental-basedir - Aligns the backup flow with official Mariabackup documentation: https://mariadb.com/kb/en/incremental-backup-and-restore-with-mariabackup/ This makes the incremental process reliable and fully supported. Closes-Bug: #2111620 Change-Id: I562ce5e54752015863a20bf113bce74a69e02331 (cherry picked from commit bcf37e5)
1 parent 70534ab commit c17ddab

File tree

3 files changed

+89
-47
lines changed

3 files changed

+89
-47
lines changed

docker/mariadb/mariadb-server/backup.sh

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,66 @@ set -o pipefail
66
# Execute a full backup
77
backup_full() {
88
echo "Taking a full backup"
9-
LAST_FULL_DATE=$(date +%d-%m-%Y)
9+
LAST_FULL_DATE=$(date +%d-%m-%Y-%s)
10+
BACKUP_FILE="mysqlbackup-${LAST_FULL_DATE}.qp.xbc.xbs.gz"
11+
BACKUP_PATH="$BACKUP_DIR/full-${LAST_FULL_DATE}"
12+
mkdir -p "$BACKUP_PATH"
13+
1014
mariabackup \
1115
--defaults-file=/etc/mysql/my.cnf \
1216
--backup \
1317
--stream=xbstream \
1418
--history=$LAST_FULL_DATE | gzip > \
15-
$BACKUP_DIR/mysqlbackup-$(date +%d-%m-%Y-%s).qp.xbc.xbs.gz
16-
echo $LAST_FULL_DATE > $BACKUP_DIR/last_full_date
19+
"$BACKUP_PATH/$BACKUP_FILE"
20+
21+
echo "$BACKUP_PATH/$BACKUP_FILE" > "$BACKUP_DIR/last_full_file"
1722
}
1823

1924
# Execute an incremental backup
2025
backup_incremental() {
2126
echo "Taking an incremental backup"
22-
if [ -r $BACKUP_DIR/last_full_date ]; then
23-
LAST_FULL_DATE=$(cat $BACKUP_DIR/last_full_date)
24-
fi
25-
if [ -z $LAST_FULL_DATE ]; then
26-
LAST_FULL_DATE=$(date +%d-%m-%Y)
27+
28+
if [ ! -r "$BACKUP_DIR/last_full_file" ]; then
29+
echo "Error: No full backup file found."
30+
exit 1
2731
fi
32+
33+
FULL_BACKUP_FILE=$(cat "$BACKUP_DIR/last_full_file")
34+
LAST_FULL_DATE=$(basename "$(dirname "$FULL_BACKUP_FILE")" | sed 's/^full-//')
35+
NOW=$(date +%H-%M-%S-%d-%m-%Y)
36+
INCR_DIR="$BACKUP_DIR/incr-${NOW}-since-${LAST_FULL_DATE}"
37+
mkdir -p "$INCR_DIR"
38+
39+
TMP_BASEDIR=$(mktemp -d)
40+
echo "Decompressing full backup to temp dir: $TMP_BASEDIR"
41+
gunzip -c "$FULL_BACKUP_FILE" | mbstream -x -C "$TMP_BASEDIR"
42+
2843
mariabackup \
2944
--defaults-file=/etc/mysql/my.cnf \
3045
--backup \
3146
--stream=xbstream \
32-
--incremental-history-name=$LAST_FULL_DATE \
33-
--history=$LAST_FULL_DATE | gzip > \
34-
$BACKUP_DIR/incremental-$(date +%H)-mysqlbackup-$(date +%d-%m-%Y-%s).qp.xbc.xbs.gz
47+
--incremental-basedir="$TMP_BASEDIR" \
48+
--history="incr-${NOW}" | gzip > \
49+
"$INCR_DIR/incremental-${NOW}-mysqlbackup-${LAST_FULL_DATE}.qp.xbc.xbs.gz"
50+
51+
rm -rf "$TMP_BASEDIR"
3552
}
3653

3754
BACKUP_DIR=/backup/
38-
cd $BACKUP_DIR
55+
cd "$BACKUP_DIR"
3956

40-
if [ -n $BACKUP_TYPE ]; then
41-
case $BACKUP_TYPE in
57+
if [ -n "${BACKUP_TYPE:-}" ]; then
58+
case "$BACKUP_TYPE" in
4259
"full")
43-
backup_full
44-
;;
60+
backup_full
61+
;;
4562
"incremental")
46-
backup_incremental
47-
;;
63+
backup_incremental
64+
;;
4865
*)
49-
echo "Only full or incremental options are supported."
50-
exit 1
51-
;;
66+
echo "Only full or incremental options are supported."
67+
exit 1
68+
;;
5269
esac
5370
else
5471
echo "You need to specify either full or incremental backup options."

docker/mariadb/mariadb-server/backup_replica.sh

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,48 @@ cd "${BACKUP_DIR}"
2323
backup_full() {
2424
echo "Taking a full backup"
2525
LAST_FULL_DATE=$(date +%d-%m-%Y-%s)
26+
BACKUP_FILE="backup-full-${LAST_FULL_DATE}.mbs.gz"
27+
BACKUP_PATH="${BACKUP_DIR}/full-${LAST_FULL_DATE}"
28+
mkdir -p "${BACKUP_PATH}"
29+
2630
mariabackup \
2731
--defaults-file="${REPLICA_MY_CNF}" \
2832
--backup \
2933
--stream=mbstream \
30-
--history="${LAST_FULL_DATE}" | gzip > \
31-
"${BACKUP_DIR}/mysqlbackup-${LAST_FULL_DATE}.qp.mbc.mbs.gz" && \
32-
echo "${LAST_FULL_DATE}" > "${BACKUP_DIR}/last_full_date"
34+
--history="${LAST_FULL_DATE}" \
35+
| gzip > "${BACKUP_PATH}/${BACKUP_FILE}"
36+
37+
echo "${BACKUP_PATH}/${BACKUP_FILE}" > "${BACKUP_DIR}/last_full_file"
3338
}
3439

3540
# Execute an incremental backup
3641
backup_incremental() {
37-
if [ -r "${BACKUP_DIR}/last_full_date" ]; then
38-
LAST_FULL_DATE=$(cat "${BACKUP_DIR}/last_full_date")
39-
else
40-
LAST_FULL_DATE=""
41-
fi
42-
if [ ! -z "${LAST_FULL_DATE}" ]; then
43-
echo "Taking an incremental backup"
44-
mariabackup \
45-
--defaults-file="${REPLICA_MY_CNF}" \
46-
--backup \
47-
--stream=mbstream \
48-
--incremental-history-name="${LAST_FULL_DATE}" \
49-
--history="${LAST_FULL_DATE}" | gzip > \
50-
"${BACKUP_DIR}/incremental-$(date +%H)-mysqlbackup-${LAST_FULL_DATE}.qp.mbc.mbs.gz"
51-
else
52-
echo "Error: Full backup don't exist."
42+
if [ ! -r "${BACKUP_DIR}/last_full_file" ]; then
43+
echo "Error: No full backup file found."
5344
exit 1
5445
fi
46+
47+
FULL_BACKUP_FILE=$(cat "${BACKUP_DIR}/last_full_file")
48+
LAST_FULL_DATE=$(basename "$(dirname "${FULL_BACKUP_FILE}")" | sed 's/^full-//')
49+
NOW=$(date +%H-%M-%S-%d-%m-%Y)
50+
INCR_DIR="${BACKUP_DIR}/incr-${NOW}-since-${LAST_FULL_DATE}"
51+
mkdir -p "${INCR_DIR}"
52+
53+
# Temp dir for full base restore
54+
TMP_BASEDIR=$(mktemp -d)
55+
56+
echo "Decompressing full backup to temp dir: ${TMP_BASEDIR}"
57+
gunzip -c "${FULL_BACKUP_FILE}" | mbstream -x -C "${TMP_BASEDIR}"
58+
59+
mariabackup \
60+
--defaults-file="${REPLICA_MY_CNF}" \
61+
--backup \
62+
--stream=mbstream \
63+
--incremental-basedir="${TMP_BASEDIR}" \
64+
--history="incr-${NOW}" \
65+
| gzip > "${INCR_DIR}/backup-incremental-${NOW}.mbs.gz"
66+
67+
rm -rf "${TMP_BASEDIR}"
5568
}
5669

5770
# Retry logic for database queries
@@ -119,15 +132,15 @@ if [ -n "${BACKUP_TYPE}" ]; then
119132
get_and_set_replica_server
120133
case "${BACKUP_TYPE}" in
121134
"full")
122-
backup_full
123-
;;
135+
backup_full
136+
;;
124137
"incremental")
125-
backup_incremental
126-
;;
138+
backup_incremental
139+
;;
127140
*)
128-
echo "Only full or incremental options are supported."
129-
exit 1
130-
;;
141+
echo "Only full or incremental options are supported."
142+
exit 1
143+
;;
131144
esac
132145
else
133146
echo "You need to specify either full or incremental backup options."
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
fixes:
3+
- |
4+
Fixes incremental MariaDB backups by switching to directory-based base backups,
5+
following official Mariabackup recommendations.
6+
`LP#2111620 <https://bugs.launchpad.net/kolla/+bug/2111620>`__
7+
upgrade:
8+
- |
9+
Backup files are now stored in timestamped directories and have new filenames.
10+
This does not affect restore, as Kolla Ansible does not support automated restore;
11+
users perform restores manually. A new full backup is recommended after upgrade
12+
to ensure incremental backups work correctly.

0 commit comments

Comments
 (0)