-
Notifications
You must be signed in to change notification settings - Fork 5
/
zzmysqldump.sh
executable file
·145 lines (98 loc) · 3.98 KB
/
zzmysqldump.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
#!/usr/bin/env bash
## bash-fx
if [ -z $(command -v curl) ]; then sudo apt update && sudo apt install curl -y; fi
if [ -f "/usr/local/turbolab.it/bash-fx/bash-fx.sh" ]; then
source "/usr/local/turbolab.it/bash-fx/bash-fx.sh"
else
source <(curl -s https://raw.githubusercontent.com/TurboLabIt/bash-fx/main/bash-fx.sh)
fi
## bash-fx is ready
fxHeader "📦 zzmysqldump"
fxConfigLoader "$1"
fxTitle "🔌 Connecting..."
if [ ! -z "$MYSQL_PASSWORD" ]; then
MYSQL_PASSWORD_HIDDEN="${MYSQL_PASSWORD:0:2}**...**${MYSQL_PASSWORD: -2}"
fi
echo "👤 User: ##${MYSQL_USER}##"
echo "🔑 Pass: ##${MYSQL_PASSWORD_HIDDEN}##"
echo "🖥️ Host: ##${MYSQL_HOST}##"
echo ""
DATABASES=$(mysql -N -u "${MYSQL_USER}" -p"${MYSQL_PASSWORD}" -h "${MYSQL_HOST}" -e 'show databases')
if [ $? -eq 0 ]; then
fxMessage "$DATABASES"
else
fxCatastrophicError "Database connection failed"
fi
fxTitle "📂 Creating the backup directory..."
MYSQL_BACKUP_DIR="${MYSQL_BACKUP_DIR%/}/"
fxInfo "The backup directory is ##${MYSQL_BACKUP_DIR}##"
mkdir -p "${MYSQL_BACKUP_DIR}"
touch "${MYSQL_BACKUP_DIR}⚠️ WARNING! This folder is auto-cleaned periodically!"
fxTitle "🚫 Applying exclude filter..."
if [ ! -z "$MYSQL_DB_EXCLUDE" ]; then
DATABASES=$(echo "$DATABASES" | egrep -vx "$MYSQL_DB_EXCLUDE")
fxMessage "$DATABASES"
else
fxInfo "No exclude filter defined. I'm going to backup every database"
fi
fxTitle "💚 Applying include filter.."
if [ ! -z "$MYSQL_DB_INCLUDE" ]; then
DATABASES=$(echo "$DATABASES" | egrep -x "$MYSQL_DB_INCLUDE")
fxMessage "$DATABASES"
else
fxInfo "No include filter defined."
fi
## Set and clean the 7zip log for non-blocking mode
fxTitle "💨 Check if 7-Zip should run in non-blocking mode..."
if [ "${SEVENZIP_NON_BLOCKING}" = 1 ]; then
fxMessage "7-Zip NON-BLOCKING mode enabled"
SEVENZIP_NON_BLOCKING_LOGFILE_SUFFIX=_background_7zipping.log
## remove leftovers
rm -f "${MYSQL_BACKUP_DIR}"*${SEVENZIP_NON_BLOCKING_LOGFILE_SUFFIX}
else
fxInfo "7-Zip will run is blocking mode"
fi
fxTitle "🔫 Include/exclude triggers..."
if [ "${MYSQLDUMP_SKIP_TRIGGERS}" = 1 ]; then
fxWarning "Triggers skipping is enabled. Your dump WON'T contain any trigger"
MYSQLDUMP_SKIP_TRIGGERS_CLI_OPTION="--skip-triggers"
else
fxOK "Triggers will be included in your dump"
fi
## Iterate over DBs
for DATABASE in $DATABASES; do
DOWEEK="$(date +'%u')"
## Dump filename
DUMPFILE_FULLPATH=${MYSQL_BACKUP_DIR}${HOSTNAME}_${DATABASE}_${DOWEEK}.sql
fxTitle "📦 mysqldumping ##${DATABASE}##"
fxMessage "$DUMPFILE_FULLPATH"
mysqldump \
-u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -h "$MYSQL_HOST" \
$MYSQLDUMP_OPTIONS $MYSQLDUMP_SKIP_TRIGGERS_CLI_OPTION \
--databases "$DATABASE" > "$DUMPFILE_FULLPATH"
fxTitle "🧪 Optimizing the exported file..."
## autocommit optimization - header
NO_AUTOCOMMIT_TEXT="SET @OLD_AUTOCOMMIT=@@AUTOCOMMIT, AUTOCOMMIT=0;"
sed -i "/Server version/ a ${NO_AUTOCOMMIT_TEXT}" "$DUMPFILE_FULLPATH"
## autocommit optimization - footer
echo "COMMIT;" >> "$DUMPFILE_FULLPATH"
echo "SET AUTOCOMMIT=@OLD_AUTOCOMMIT" >> "$DUMPFILE_FULLPATH"
fxTitle "🗜 7-zipping ##${DATABASE}##"
rm -f "${DUMPFILE_FULLPATH}.7z"
if [ ${SEVENZIP_NON_BLOCKING} == 1 ]; then
bash "${SCRIPT_DIR}7zip-log-to-file.sh" "${SEVENZIP_COMPRESS_OPTIONS}" "${DUMPFILE_FULLPATH}.7z" "${DUMPFILE_FULLPATH}" "${DUMPFILE_FULLPATH}${SEVENZIP_NON_BLOCKING_LOGFILE_SUFFIX}" &
else
7za a ${SEVENZIP_COMPRESS_OPTIONS} "${DUMPFILE_FULLPATH}.7z" "${DUMPFILE_FULLPATH}"
fi
done
fxTitle "Deleting old files..."
if [ -z "$1" ]; then
fxMessage "Current retention: ##${RETENTION_DAYS}## day(s)"
find "${MYSQL_BACKUP_DIR}" -type f -mtime +${RETENTION_DAYS} \( -name "*.sql.7z" -o -name "*.sql" -o -name "*.log" \)
find "${MYSQL_BACKUP_DIR}" -type f -mtime +${RETENTION_DAYS} \( -name "*.sql.7z" -o -name "*.sql" -o -name "*.log" \) -delete
else
fxInfo "Skipping when running with a profile"
fi
fxTitle "Backup file list"
ls -latrh "${MYSQL_BACKUP_DIR}"
fxEndFooter