Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add xz support #7

Merged
merged 3 commits into from
Sep 8, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions zabbix-mysql-dump
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
# - Ruslan Ohitin (ruslan-ohitin)
#
# HISTORY
# 0.8.2 (2016-07-28)
# NEW: Option -x to use XZ instead of GZ for compression
# 0.8.1 (2016-07-11)
# ENH: Added Zabbix 3.0.x tables to list (added & tested by Ruslan Ohitin)
#
#
# 0.8.0 (2016-01-22)
# FIX: Only invoke `dig` if available
# ENH: Option -c to use a MySQL config ("options") file (suggested by Daniel Schneller)
Expand All @@ -39,7 +41,7 @@
# NEW: Parsing of commandline arguments implemented
# ENH: Try reverse lookup of IPs and include hostname/IP in filename
# REV: Stop if database password is wrong
#
#
# 0.7.0 (2014-10-02)
# ENH: Complete overhaul to make script work with lots of Zabbix versions
#
Expand Down Expand Up @@ -67,7 +69,7 @@

#
# DEFAULT VALUES
#
#
# DO NOT EDIT THESE VALUES!
# Instead, use command line parameters or a config file to specify options.
#
Expand All @@ -76,6 +78,7 @@ DBHOST="127.0.0.1"
DBNAME="zabbix"
DBUSER="zabbix"
DBPASS=""
XZ="no"
QUIET="no"
REVERSELOOKUP="yes"
GENERATIONSTOKEEP=0
Expand All @@ -87,7 +90,7 @@ if [ -z "$1" ]; then
cat <<EOF
USAGE
$(basename $BASH_SOURCE) [options]

OPTIONS
-h HOST
Hostname/IP of MySQL server.
Expand Down Expand Up @@ -121,6 +124,13 @@ OPTIONS
Uses filename to match.
Default: keep all backups

-x
Compress using xz instead of gz
PLEASE NOTE:
xz compression will take much longer and consume more CPU time
but the resulting backup will be about half the size of the same
sql file compressed using gz. Your mileage may vary.

-n
Skip reverse lookup of IP address for host.

Expand All @@ -135,13 +145,13 @@ EXAMPLES

EOF
exit 1
fi
fi

#
# PARSE COMMAND LINE ARGUMENTS
#
DB_GIVEN=0
while getopts ":h:d:u:p:o:r:c:qn" opt; do
while getopts ":h:d:u:p:o:r:c:xqn" opt; do
case $opt in
h) DBHOST="$OPTARG" ;;
d) DBNAME="$OPTARG"; DB_GIVEN=1 ;;
Expand All @@ -150,6 +160,7 @@ while getopts ":h:d:u:p:o:r:c:qn" opt; do
c) CNFFILE="$OPTARG" ;;
o) DUMPDIR="$OPTARG" ;;
r) GENERATIONSTOKEEP=$(printf '%.0f' "$OPTARG") ;;
x) XZ="yes" ;;
n) REVERSELOOKUP="no" ;;
q) QUIET="yes" ;;
\?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;;
Expand Down Expand Up @@ -235,7 +246,7 @@ fi

#
# READ TABLE LIST from __DATA__ section at the end of this script
# (http://stackoverflow.com/a/3477269/2983301)
# (http://stackoverflow.com/a/3477269/2983301)
#
DATA_TABLES=()
while read line; do
Expand Down Expand Up @@ -306,7 +317,7 @@ while read table; do
if [ $(($i_percent % 2)) -eq 0 ]; then echo -n "."; fi
fi
fi
done <<<"$DB_TABLES"
done <<<"$DB_TABLES"

#
# COMPRESS BACKUP
Expand All @@ -315,17 +326,27 @@ if [ "$QUIET" == "no" ]; then
echo -e "\n"
echo "For the following large tables only the schema (without data) was stored:"
for table in "${PROCESSED_DATA_TABLES[@]}"; do echo " - $table"; done
echo

echo
echo "Compressing backup file..."
fi
gzip -f "$DUMPFILE"
if [ $? -ne 0 ]; then
echo -e "\nERROR: Could not compress backup file, see previous messages" >&2
if [ "$XZ" == "no" ]; then
gzip -f "$DUMPFILE"
if [ $? -ne 0 ]; then
echo -e "\nERROR: Could not compress backup file, see previous messages" >&2
exit 1
fi
fi
if [ "$XZ" == "yes" ]; then
xz "$DUMPFILE"
if [ $? -ne 0 ]; then
echo -e "\nERROR: Could not compress backup file, see previous messages" >&2
exit 1
fi
fi

[ "$QUIET" == "no" ] && echo -e "\nBackup Completed:\n${DUMPFILE}.gz"
[ "$QUIET" == "no" ] && [ "$XZ" == "no" ] && echo -e "\nBackup Completed:\n${DUMPFILE}.gz"
[ "$QUIET" == "no" ] && [ "$XZ" == "yes" ] && echo -e "\nBackup Completed:\n${DUMPFILE}.xz"

#
# ROTATE OLD BACKUPS
Expand Down