From 371beaf02b709ccb8c9436eda97d97ad75ce5412 Mon Sep 17 00:00:00 2001 From: maxhq Date: Fri, 16 Mar 2012 15:55:21 +0100 Subject: [PATCH 001/122] CHG Backup of Zabbix 1.9.x / 2.0.0 CHG Incorporated mysqldump options suggested by Jonathan Bayer REM unnecessary use of variables (DATEBIN etc) for commands that use to be in $PATH --- zabbix-mysql-backupconf.sh | 67 ++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/zabbix-mysql-backupconf.sh b/zabbix-mysql-backupconf.sh index 4851066..dd1e73d 100644 --- a/zabbix-mysql-backupconf.sh +++ b/zabbix-mysql-backupconf.sh @@ -1,16 +1,22 @@ #!/bin/bash # # zabbix-mysql-backupconf.sh +# v0.4 - 20120302 Incorporated mysqldump options suggested by Jonathan Bayer +# v0.3 - 20120206 Backup of Zabbix 1.9.x / 2.0.0, removed unnecessary use of +# variables (DATEBIN etc) for commands that use to be in $PATH # v0.2 - 20111105 # -# Configuration Backup for Zabbix 1.8 w/MySQL +# Configuration Backup for Zabbix 2.0 w/MySQL # # Author: Ricardo Santos (rsantos at gmail.com) # http://zabbixzone.com # +# modified by Jens Berthold, 2012 +# # Thanks for suggestions from: # - Oleksiy Zagorskyi (zalex) # - Petr Jendrejovsky +# - Jonathan Bayer # # mysql config @@ -19,53 +25,52 @@ DBNAME="zabbix" DBUSER="zabbix" DBPASS="YOURMYSQLPASSWORDHERE" -# some tools -MYSQLDUMP="`which mysqldump`" -GZIP="`which gzip`" -DATEBIN="`which date`" -MKDIRBIN="`which mkdir`" - # target path MAINDIR="/var/lib/zabbix/backupconf" -DUMPDIR="${MAINDIR}/`${DATEBIN} +%Y%m%d%H%M`" -${MKDIRBIN} -p ${DUMPDIR} +DUMPDIR="${MAINDIR}/`date +%Y%m%d-%H%M`" + +mkdir -p "${DUMPDIR}" # configuration tables -CONFTABLES=( actions applications autoreg_host conditions config dchecks dhosts \ -drules dservices escalations expressions functions globalmacro graph_theme \ -graphs graphs_items groups help_items hostmacro hosts hosts_groups \ -hosts_profiles hosts_profiles_ext hosts_templates housekeeper httpstep \ -httpstepitem httptest httptestitem ids images items items_applications \ -maintenances maintenances_groups maintenances_hosts maintenances_windows \ -mappings media media_type node_cksum nodes opconditions operations \ -opmediatypes profiles proxy_autoreg_host proxy_dhistory proxy_history regexps \ -rights screens screens_items scripts service_alarms services services_links \ -services_times sessions slides slideshows sysmaps sysmaps_elements \ -sysmaps_link_triggers sysmaps_links timeperiods trigger_depends triggers \ -user_history users users_groups usrgrp valuemaps ) +CONFTABLES=( actions applications autoreg_host conditions config dchecks \ +dhosts drules dservices escalations expressions functions globalmacro \ +globalvars graph_discovery graph_theme graphs graphs_items groups help_items \ +host_inventory hostmacro hosts hosts_groups hosts_templates housekeeper \ +httpstep httpstepitem httptest httptestitem icon_map icon_mapping ids images \ +interface item_discovery items items_applications maintenances \ +maintenances_groups maintenances_hosts maintenances_windows mappings media \ +media_type node_cksum nodes opcommand opcommand_grp opcommand_hst opconditions \ +operations opgroup opmessage opmessage_grp opmessage_usr optemplate profiles \ +proxy_autoreg_host proxy_dhistory proxy_history regexps rights screens \ +screens_items scripts service_alarms services services_links services_times \ +sessions slides slideshows sysmap_element_url sysmap_url sysmaps \ +sysmaps_elements sysmaps_link_triggers sysmaps_links timeperiods \ +trigger_depends trigger_discovery triggers user_history users users_groups \ +usrgrp valuemaps ) # tables with large data DATATABLES=( acknowledges alerts auditlog_details auditlog events \ history history_log history_str history_str_sync history_sync history_text \ history_uint history_uint_sync trends trends_uint ) +DUMPFILE="${DUMPDIR}/zbx-conf-bkup-`date +%Y%m%d-%H%M`.sql" +>"${DUMPFILE}" + # CONFTABLES for table in ${CONFTABLES[*]}; do - DUMPFILE="${DUMPDIR}/${table}.sql" - echo "Backuping table ${table}" - ${MYSQLDUMP} -R --opt --extended-insert=FALSE \ - -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --tables ${table} >${DUMPFILE} - ${GZIP} -f ${DUMPFILE} + echo "Backuping table ${table}" + mysqldump -R --opt --single-transaction --skip-lock-tables --extended-insert=FALSE \ + -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --tables ${table} >>"${DUMPFILE}" done # DATATABLES for table in ${DATATABLES[*]}; do - DUMPFILE="${DUMPDIR}/${table}.sql" - echo "Backuping schema table ${table}" - ${MYSQLDUMP} -R --opt --no-data \ - -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --tables ${table} >${DUMPFILE} - ${GZIP} -f ${DUMPFILE} + echo "Backuping schema table ${table}" + mysqldump -R --opt --single-transaction --skip-lock-tables --no-data \ + -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --tables ${table} >>"${DUMPFILE}" done +gzip -f "${DUMPFILE}" + echo echo "Backup Completed - ${DUMPDIR}" From e0b97c2bf4c93f3de912bee8238bb75cbf8fb13d Mon Sep 17 00:00:00 2001 From: maxhq Date: Mon, 13 May 2013 23:21:22 +0200 Subject: [PATCH 002/122] Update zabbix-mysql-backupconf.sh --- zabbix-mysql-backupconf.sh | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/zabbix-mysql-backupconf.sh b/zabbix-mysql-backupconf.sh index dd1e73d..7084cf3 100644 --- a/zabbix-mysql-backupconf.sh +++ b/zabbix-mysql-backupconf.sh @@ -9,7 +9,7 @@ # Configuration Backup for Zabbix 2.0 w/MySQL # # Author: Ricardo Santos (rsantos at gmail.com) -# http://zabbixzone.com +# http://zabbixzone.com/zabbix/backuping-only-the-zabbix-configuration/ # # modified by Jens Berthold, 2012 # @@ -19,16 +19,29 @@ # - Jonathan Bayer # +# # mysql config -DBHOST="localhost" -DBNAME="zabbix" +# +DBHOST="1.2.3.4" +DBNAME="zabbixdb" DBUSER="zabbix" -DBPASS="YOURMYSQLPASSWORDHERE" +DBPASS="password" +# # target path -MAINDIR="/var/lib/zabbix/backupconf" -DUMPDIR="${MAINDIR}/`date +%Y%m%d-%H%M`" +# + +#MAINDIR="/var/lib/zabbix/backupconf" +# following will store the backup in a subdirectory of the current directory +MAINDIR="`dirname \"$0\"`" +if [ ! -x /usr/bin/mysqldump ]; then + echo "mysqldump not found." + echo "(with Debian, \"apt-get install mysql-client\" will help)" + exit 1 +fi + +DUMPDIR="${MAINDIR}/`date +%Y%m%d-%H%M`" mkdir -p "${DUMPDIR}" # configuration tables @@ -58,15 +71,15 @@ DUMPFILE="${DUMPDIR}/zbx-conf-bkup-`date +%Y%m%d-%H%M`.sql" # CONFTABLES for table in ${CONFTABLES[*]}; do - echo "Backuping table ${table}" - mysqldump -R --opt --single-transaction --skip-lock-tables --extended-insert=FALSE \ + echo "Backuping configuration table ${table}" + mysqldump --routines --opt --single-transaction --skip-lock-tables --extended-insert=FALSE \ -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --tables ${table} >>"${DUMPFILE}" done # DATATABLES for table in ${DATATABLES[*]}; do - echo "Backuping schema table ${table}" - mysqldump -R --opt --single-transaction --skip-lock-tables --no-data \ + echo "Backuping data table ${table}" + mysqldump --routines --opt --single-transaction --skip-lock-tables --no-data \ -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --tables ${table} >>"${DUMPFILE}" done @@ -74,3 +87,5 @@ gzip -f "${DUMPFILE}" echo echo "Backup Completed - ${DUMPDIR}" +echo "Hit ENTER" +read From 273166cd0f23ca0969c579c527b98e170dd43ae0 Mon Sep 17 00:00:00 2001 From: dre- Date: Mon, 15 Sep 2014 11:36:34 +0200 Subject: [PATCH 003/122] Update zabbix-mysql-backupconf.sh The zabbix database has changed. We use Zabbix 2.2.3 at this time. So I've updated the tables in section "CONSTANTS" --- zabbix-mysql-backupconf.sh | 162 +++++++++++++++++++++++++------------ 1 file changed, 109 insertions(+), 53 deletions(-) diff --git a/zabbix-mysql-backupconf.sh b/zabbix-mysql-backupconf.sh index 7084cf3..57373ea 100644 --- a/zabbix-mysql-backupconf.sh +++ b/zabbix-mysql-backupconf.sh @@ -1,88 +1,144 @@ #!/bin/bash +# NAME +# zabbix-mysql-backupconf.sh - Configuration Backup for Zabbix 2.0 w/MySQL # -# zabbix-mysql-backupconf.sh -# v0.4 - 20120302 Incorporated mysqldump options suggested by Jonathan Bayer -# v0.3 - 20120206 Backup of Zabbix 1.9.x / 2.0.0, removed unnecessary use of -# variables (DATEBIN etc) for commands that use to be in $PATH -# v0.2 - 20111105 +# SYNOPSIS +# This is a MySQL configuration backup script for Zabbix v2.0 oder 2.2. +# It does a full backup of all configuration tables, but only a schema +# backup of large data tables. # -# Configuration Backup for Zabbix 2.0 w/MySQL +# The original script was written by Ricardo Santos and published at +# http://zabbixzone.com/zabbix/backuping-only-the-zabbix-configuration/ +# and +# https://github.com/xsbr/zabbixzone/blob/master/zabbix-mysql-backupconf.sh # -# Author: Ricardo Santos (rsantos at gmail.com) -# http://zabbixzone.com/zabbix/backuping-only-the-zabbix-configuration/ +# Credits for some suggestions concerning the original script to: +# - Ricardo Santos +# - Oleksiy Zagorskyi (zalex) +# - Petr Jendrejovsky +# - Jonathan Bayer +# - Andreas Niedermann (dre-) # -# modified by Jens Berthold, 2012 -# -# Thanks for suggestions from: -# - Oleksiy Zagorskyi (zalex) -# - Petr Jendrejovsky -# - Jonathan Bayer +# HISTORY +# v0.6 - 2014-09-15 Updated the table list for use with zabbix v2.2.3 +# v0.5 - 2013-05-13 Added table list comparison between database and script +# v0.4 - 2012-03-02 Incorporated mysqldump options suggested by Jonathan Bayer +# v0.3 - 2012-02-06 Backup of Zabbix 1.9.x / 2.0.0, removed unnecessary use of +# variables (DATEBIN etc) for commands that use to be in $PATH +# v0.2 - 2011-11-05 # +# AUTHOR +# Jens Berthold (maxhq), 2013 + # -# mysql config +# CONFIGURATION # + +# mysql database DBHOST="1.2.3.4" -DBNAME="zabbixdb" +DBNAME="zabbix" DBUSER="zabbix" DBPASS="password" -# -# target path -# - +# backup target path #MAINDIR="/var/lib/zabbix/backupconf" # following will store the backup in a subdirectory of the current directory MAINDIR="`dirname \"$0\"`" -if [ ! -x /usr/bin/mysqldump ]; then - echo "mysqldump not found." - echo "(with Debian, \"apt-get install mysql-client\" will help)" - exit 1 -fi - -DUMPDIR="${MAINDIR}/`date +%Y%m%d-%H%M`" -mkdir -p "${DUMPDIR}" +# +# CONSTANTS +# # configuration tables -CONFTABLES=( actions applications autoreg_host conditions config dchecks \ -dhosts drules dservices escalations expressions functions globalmacro \ -globalvars graph_discovery graph_theme graphs graphs_items groups help_items \ -host_inventory hostmacro hosts hosts_groups hosts_templates housekeeper \ -httpstep httpstepitem httptest httptestitem icon_map icon_mapping ids images \ -interface item_discovery items items_applications maintenances \ -maintenances_groups maintenances_hosts maintenances_windows mappings media \ -media_type node_cksum nodes opcommand opcommand_grp opcommand_hst opconditions \ -operations opgroup opmessage opmessage_grp opmessage_usr optemplate profiles \ -proxy_autoreg_host proxy_dhistory proxy_history regexps rights screens \ -screens_items scripts service_alarms services services_links services_times \ -sessions slides slideshows sysmap_element_url sysmap_url sysmaps \ -sysmaps_elements sysmaps_link_triggers sysmaps_links timeperiods \ -trigger_depends trigger_discovery triggers user_history users users_groups \ -usrgrp valuemaps ) +CONFTABLES=( actions application_template applications autoreg_host conditions \ +config dbversion dchecks dhosts drules dservices escalations expressions functions \ +globalmacro globalvars graph_discovery graph_theme graphs graphs_items group_discovery \ +group_prototype groups host_discovery host_inventory hostmacro hosts \ +hosts_groups hosts_templates housekeeper httpstep httpstepitem httptest \ +httptestitem icon_map icon_mapping ids images interface interface_discovery \ +item_discovery items items_applications maintenances maintenances_groups \ +maintenances_hosts maintenances_windows mappings media media_type node_cksum \ +nodes opcommand opcommand_grp opcommand_hst opconditions operations opgroup \ +opmessage opmessage_grp opmessage_usr optemplate profiles proxy_autoreg_host \ +proxy_dhistory proxy_history regexps rights screens screens_items scripts \ +service_alarms services services_links services_times sessions slides \ +slideshows sysmap_element_url sysmap_url sysmaps sysmaps_elements \ +sysmaps_link_triggers sysmaps_links timeperiods trigger_depends trigger_discovery \ +triggers user_history users users_groups usrgrp valuemaps ) # tables with large data DATATABLES=( acknowledges alerts auditlog_details auditlog events \ history history_log history_str history_str_sync history_sync history_text \ history_uint history_uint_sync trends trends_uint ) -DUMPFILE="${DUMPDIR}/zbx-conf-bkup-`date +%Y%m%d-%H%M`.sql" +DUMPDIR="${MAINDIR}/`date +%Y%m%d-%H%M`" +DUMPFILE="${DUMPDIR}/zabbix-conf-backup-`date +%Y%m%d-%H%M`.sql" + +# +# CHECKS +# +if [ ! -x /usr/bin/mysqldump ]; then + echo "mysqldump not found." + echo "(with Debian, \"apt-get install mysql-client\" will help)" + exit 1 +fi + +# +# compare table list between script and database +# +FILE_TABLES_LIVE=`mktemp` +FILE_TABLES=`mktemp` + +# Get all current Zabbix tables from databse +mysql -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --batch --silent \ + -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" \ + | sort >> $FILE_TABLES_LIVE + +# Merge CONFTABLES and DATATABLES into one array +allTables=( "${CONFTABLES[@]}" "${DATATABLES[@]}" ) +printf '%s\n' "${allTables[@]}" | sort >> $FILE_TABLES + +difference=`diff --suppress-common-lines $FILE_TABLES $FILE_TABLES_LIVE | grep -v "^\w"` + +if [ ! -z "$difference" ]; then + echo -e "The Zabbix database differs from the configuration in this script." + if [ `echo "$difference" | grep -c "^>"` -gt 0 ]; then + echo -e "\nThese additional tables where found in '$DBNAME' on $DBHOST:" + echo "$difference" | grep "^>" | sed 's/^>/ -/gm' + fi + if [ `echo "$difference" | grep -c "^<"` -gt 0 ]; then + echo -e "\nThese configured tables are missing in '$DBNAME' on $DBHOST:" + echo "$difference" | grep "^<" | sed 's/^"${DUMPFILE}" -# CONFTABLES +# full backup of configuration tables +echo "Full backup of configuration tables:" for table in ${CONFTABLES[*]}; do - echo "Backuping configuration table ${table}" - mysqldump --routines --opt --single-transaction --skip-lock-tables --extended-insert=FALSE \ - -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --tables ${table} >>"${DUMPFILE}" + echo " - ${table}" + mysqldump --routines --opt --single-transaction --skip-lock-tables --extended-insert=FALSE \ + -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --tables ${table} >>"${DUMPFILE}" done - -# DATATABLES +# scheme backup of large data tables +echo "Scheme backup of data tables:" for table in ${DATATABLES[*]}; do - echo "Backuping data table ${table}" - mysqldump --routines --opt --single-transaction --skip-lock-tables --no-data \ - -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --tables ${table} >>"${DUMPFILE}" + echo " - ${table}" + mysqldump --routines --opt --single-transaction --skip-lock-tables --no-data \ + -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --tables ${table} >>"${DUMPFILE}" done +echo "Compressing backup file ${DUMPFILE}..." gzip -f "${DUMPFILE}" echo From 19fdbd2df27be4778b4926cf2a0f147f27af5d7d Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Wed, 17 Sep 2014 13:21:03 +0200 Subject: [PATCH 004/122] Delete zabbix-mysql-autopartitioning.sql I focus on the backup script now --- zabbix-mysql-autopartitioning.sql | 95 ------------------------------- 1 file changed, 95 deletions(-) delete mode 100644 zabbix-mysql-autopartitioning.sql diff --git a/zabbix-mysql-autopartitioning.sql b/zabbix-mysql-autopartitioning.sql deleted file mode 100644 index 137a8ca..0000000 --- a/zabbix-mysql-autopartitioning.sql +++ /dev/null @@ -1,95 +0,0 @@ -/************************************************************** - MySQL Auto Partitioning Procedure for Zabbix 1.8 - http://zabbixzone.com/zabbix/partitioning-tables/ - - Author: Ricardo Santos (rsantos at gmail.com) - Version: 20110518 -**************************************************************/ -DELIMITER // -DROP PROCEDURE IF EXISTS `zabbix`.`create_zabbix_partitions` // -CREATE PROCEDURE `zabbix`.`create_zabbix_partitions` () -BEGIN - CALL zabbix.create_next_partitions("zabbix","history"); - CALL zabbix.create_next_partitions("zabbix","history_log"); - CALL zabbix.create_next_partitions("zabbix","history_str"); - CALL zabbix.create_next_partitions("zabbix","history_text"); - CALL zabbix.create_next_partitions("zabbix","history_uint"); - CALL zabbix.drop_old_partitions("zabbix","history"); - CALL zabbix.drop_old_partitions("zabbix","history_log"); - CALL zabbix.drop_old_partitions("zabbix","history_str"); - CALL zabbix.drop_old_partitions("zabbix","history_text"); - CALL zabbix.drop_old_partitions("zabbix","history_uint"); -END // -DROP PROCEDURE IF EXISTS `zabbix`.`create_next_partitions` // -CREATE PROCEDURE `zabbix`.`create_next_partitions` (SCHEMANAME varchar(64), TABLENAME varchar(64)) -BEGIN - DECLARE NEXTCLOCK timestamp; - DECLARE PARTITIONNAME varchar(16); - DECLARE CLOCK int; - SET @totaldays = 7; - SET @i = 1; - createloop: LOOP - SET NEXTCLOCK = DATE_ADD(NOW(),INTERVAL @i DAY); - SET PARTITIONNAME = DATE_FORMAT( NEXTCLOCK, 'p%Y%m%d' ); - SET CLOCK = UNIX_TIMESTAMP(DATE_FORMAT(DATE_ADD( NEXTCLOCK ,INTERVAL 1 DAY),'%Y-%m-%d 00:00:00')); - CALL zabbix.create_partition( SCHEMANAME, TABLENAME, PARTITIONNAME, CLOCK ); - SET @i=@i+1; - IF @i > @totaldays THEN - LEAVE createloop; - END IF; - END LOOP; -END // -DROP PROCEDURE IF EXISTS `zabbix`.`drop_old_partitions` // -CREATE PROCEDURE `zabbix`.`drop_old_partitions` (SCHEMANAME varchar(64), TABLENAME varchar(64)) -BEGIN - DECLARE OLDCLOCK timestamp; - DECLARE PARTITIONNAME varchar(16); - DECLARE CLOCK int; - SET @mindays = 3; - SET @maxdays = @mindays+4; - SET @i = @maxdays; - droploop: LOOP - SET OLDCLOCK = DATE_SUB(NOW(),INTERVAL @i DAY); - SET PARTITIONNAME = DATE_FORMAT( OLDCLOCK, 'p%Y%m%d' ); - CALL zabbix.drop_partition( SCHEMANAME, TABLENAME, PARTITIONNAME ); - SET @i=@i-1; - IF @i <= @mindays THEN - LEAVE droploop; - END IF; - END LOOP; -END // -DROP PROCEDURE IF EXISTS `zabbix`.`create_partition` // -CREATE PROCEDURE `zabbix`.`create_partition` (SCHEMANAME varchar(64), TABLENAME varchar(64), PARTITIONNAME varchar(64), CLOCK int) -BEGIN - DECLARE RETROWS int; - SELECT COUNT(1) INTO RETROWS - FROM `information_schema`.`partitions` - WHERE `table_schema` = SCHEMANAME AND `table_name` = TABLENAME AND `partition_name` = PARTITIONNAME; - - IF RETROWS = 0 THEN - SELECT CONCAT( "create_partition(", SCHEMANAME, ",", TABLENAME, ",", PARTITIONNAME, ",", CLOCK, ")" ) AS msg; - SET @sql = CONCAT( 'ALTER TABLE `', SCHEMANAME, '`.`', TABLENAME, '`', - ' ADD PARTITION (PARTITION ', PARTITIONNAME, ' VALUES LESS THAN (', CLOCK, '));' ); - PREPARE STMT FROM @sql; - EXECUTE STMT; - DEALLOCATE PREPARE STMT; - END IF; -END // -DROP PROCEDURE IF EXISTS `zabbix`.`drop_partition` // -CREATE PROCEDURE `zabbix`.`drop_partition` (SCHEMANAME varchar(64), TABLENAME varchar(64), PARTITIONNAME varchar(64)) -BEGIN - DECLARE RETROWS int; - SELECT COUNT(1) INTO RETROWS - FROM `information_schema`.`partitions` - WHERE `table_schema` = SCHEMANAME AND `table_name` = TABLENAME AND `partition_name` = PARTITIONNAME; - - IF RETROWS = 1 THEN - SELECT CONCAT( "drop_partition(", SCHEMANAME, ",", TABLENAME, ",", PARTITIONNAME, ")" ) AS msg; - SET @sql = CONCAT( 'ALTER TABLE `', SCHEMANAME, '`.`', TABLENAME, '`', - ' DROP PARTITION ', PARTITIONNAME, ';' ); - PREPARE STMT FROM @sql; - EXECUTE STMT; - DEALLOCATE PREPARE STMT; - END IF; -END // -DELIMITER ; From 3759abff9df3b058bedb4d8a1bbcb2b7d271e159 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Wed, 17 Sep 2014 13:23:49 +0200 Subject: [PATCH 005/122] Update README --- README | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/README b/README index 8292524..487301f 100644 --- a/README +++ b/README @@ -1,7 +1,4 @@ -============================================================= - This is the repository for all scripts from ZabbixZone Blog - http://zabbixzone.com +This is a database backup script for Zabbix 2.2.3 that distincts between configuration and mass data tables. - Fell free to send your contribution, bugs and suggestions - Ricardo Santos (rsantos at gmail.com) -============================================================= +Tables holding configuration data will be fully backuped while for mass data tables only the table schema is stored. +This way you have a small backup of your complete configuration, and in case of disaster "only" use your item history. From ffea98a5fb94dcd26ba8670473d2c15f99f6f67b Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Wed, 17 Sep 2014 15:22:09 +0200 Subject: [PATCH 006/122] REV Ignore Eclipse .project file --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d8fe4fa --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.project From 6de48f25bca2ea5f0c252dd81bdcb3f49991c235 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 18 Sep 2014 22:22:32 +0200 Subject: [PATCH 007/122] NEW Script to load database table names for each Zabbix version from schema definition in Subversion repository --- get-table-list.pl | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100755 get-table-list.pl diff --git a/get-table-list.pl b/get-table-list.pl new file mode 100755 index 0000000..d64dd92 --- /dev/null +++ b/get-table-list.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +use File::Temp qw/ tempfile /; + +my $tables = {}; # for each table, create a list of Zabbix versions that know it +my (undef, $tmpfile) = tempfile(); + +sub stop { + my ($msg) = @_; + print "ERROR: $msg\n"; + unlink $tmpfile; + exit; +} + +# Check for subversion client +my $svn = `which svn`; +stop("No subversion client found") unless $svn; + +# List tags from subversion repo +my @tags = `svn ls 'svn://svn.zabbix.com/tags'`; +chomp @tags; # remove trailing newline +@tags = (map { $_ =~ s/\/$//; $_ } @tags); # remove trailing slash + +# Sort tags as version numbers (http://www.perlmonks.org/?node_id=814026) +@tags = map {$_->[0]} + sort {$a->[1] cmp $b->[1]} + map {[$_, pack "C*", split /\./]} @tags; + +for my $tag (@tags) { + next if $tag < 1.4; # before Zabbix 1.4, schema was stored as pure SQL + + my $schema; + my $subdir; + + printf "%-10s %s", $tag, "Searching schema..."; + # search in subdir /schema (<= 1.9.8) and /src for schema.(sql|tmpl) + for my $sub (qw(schema src)) { + my @files = `svn ls 'svn://svn.zabbix.com/tags/$tag/create/$sub' 2>/dev/null`; + next unless @files; # directory not found? + chomp @files; + ($schema) = grep /^schema\.(sql|tmpl)/, @files; + $subdir = $sub; + last; + } + if (!$schema) { + print "\nNo schema found in tag $tag\n"; + next; + } + print " ($schema) Download... "; + system("svn --force export svn://svn.zabbix.com/tags/$tag/create/$subdir/$schema $tmpfile >/dev/null"); + open my $fh, '<', $tmpfile or stop("Couldn't open temp file: $!"); + while (<$fh>) { + chomp; + next unless m/^TABLE/; + my (undef, $table) = split /\|/; + $tables->{$table} //= []; + push @{$tables->{$table}}, $tag; + } + print " Done\n"; +} + +unlink $tmpfile; + +print "----------------------------------------\n"; +for my $tab (sort keys %$tables) { + printf "%-25s %s - %s\n", $tab, $tables->{$tab}->[0], $tables->{$tab}->[-1]; +} From d93522dbe9b94c5053dde97bb6427dbffb39e8fa Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 19 Sep 2014 00:42:29 +0200 Subject: [PATCH 008/122] FIX Corrected sorting of versions like "1.8.1rc1" (own sort routine) --- get-table-list.pl | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/get-table-list.pl b/get-table-list.pl index d64dd92..b873bee 100755 --- a/get-table-list.pl +++ b/get-table-list.pl @@ -11,6 +11,36 @@ sub stop { exit; } +# sort tags as version numbers +sub cmpver { + my ($a, $b) = @_; + + # split version parts: 3.2.5rc1 --> 3 2 5rc1 + my @a_parts = split /\./, $a; + my @b_parts = split /\./, $b; + + for (my $i=0; $i= scalar(@b_parts); + # split number parts: 5rc1 --> 5 rc 1 + my ($a_num, $a_type, $a_idx) = $a_parts[$i] =~ m/^(\d+)(\D+)?(\d+)?$/; + my ($b_num, $b_type, $b_idx) = $b_parts[$i] =~ m/^(\d+)(\D+)?(\d+)?$/; + my $cmp; + # 4 before 5 + $cmp = $a_num <=> $b_num; return $cmp unless $cmp == 0; + # 5rc1 before 5 + return -1 if $a_type and not $b_type; + return 1 if not $a_type and $b_type; + # a1 before b1 + $cmp = $a_type cmp $b_type; return $cmp unless $cmp == 0; + # rc1 before rc2 + $cmp = $a_idx <=> $b_idx; return $cmp unless $cmp == 0; + } + # equal up to now, but b has more parts + return -1 if scalar(@a_parts) < scalar(@b_parts); + # equal + return 0; +} + # Check for subversion client my $svn = `which svn`; stop("No subversion client found") unless $svn; @@ -19,14 +49,10 @@ sub stop { my @tags = `svn ls 'svn://svn.zabbix.com/tags'`; chomp @tags; # remove trailing newline @tags = (map { $_ =~ s/\/$//; $_ } @tags); # remove trailing slash - -# Sort tags as version numbers (http://www.perlmonks.org/?node_id=814026) -@tags = map {$_->[0]} - sort {$a->[1] cmp $b->[1]} - map {[$_, pack "C*", split /\./]} @tags; +@tags = sort { cmpver($a,$b) } @tags; for my $tag (@tags) { - next if $tag < 1.4; # before Zabbix 1.4, schema was stored as pure SQL + next if cmpver($tag, "1.3.1") < 0; # before Zabbix 1.3.1, schema was stored as pure SQL my $schema; my $subdir; @@ -62,5 +88,5 @@ sub stop { print "----------------------------------------\n"; for my $tab (sort keys %$tables) { - printf "%-25s %s - %s\n", $tab, $tables->{$tab}->[0], $tables->{$tab}->[-1]; + printf "%-25s %-10s - %-10s\n", $tab, $tables->{$tab}->[0], $tables->{$tab}->[-1]; } From ab265e6da4be3d5e21ae2b40b2872d9be4922026 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 19 Sep 2014 01:01:52 +0200 Subject: [PATCH 009/122] DOC Added table headers for output --- get-table-list.pl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/get-table-list.pl b/get-table-list.pl index b873bee..6237503 100755 --- a/get-table-list.pl +++ b/get-table-list.pl @@ -86,7 +86,9 @@ sub cmpver { unlink $tmpfile; -print "----------------------------------------\n"; +print "\n\n"; +print "TABLE FIRST USE LAST USE\n"; +print "-------------------------------------------------\n"; for my $tab (sort keys %$tables) { - printf "%-25s %-10s - %-10s\n", $tab, $tables->{$tab}->[0], $tables->{$tab}->[-1]; + printf "%-25s %-10s - %s\n", $tab, $tables->{$tab}->[0], $tables->{$tab}->[-1]; } From 372bc0232244a9bb8ed578031e9056c5dc0e91dd Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 19 Sep 2014 01:02:29 +0200 Subject: [PATCH 010/122] NEW Added file containing table list up to Zabbix 2.4.0 --- tables.txt | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 tables.txt diff --git a/tables.txt b/tables.txt new file mode 100644 index 0000000..eb68cd9 --- /dev/null +++ b/tables.txt @@ -0,0 +1,120 @@ +This is the recent (2.4.0) result list of show-tables.pl: + +TABLE FIRST USE LAST USE +------------------------------------------------- +acknowledges 1.3.1 - 2.4.0 +actions 1.3.1 - 2.4.0 +alerts 1.3.1 - 2.4.0 +application_template 2.1.0 - 2.4.0 +applications 1.3.1 - 2.4.0 +auditlog 1.3.1 - 2.4.0 +auditlog_details 1.7 - 2.4.0 +autoreg 1.3.1 - 1.3.4 +autoreg_host 1.7 - 2.4.0 +conditions 1.3.1 - 2.4.0 +config 1.3.1 - 2.4.0 +dbversion 2.1.0 - 2.4.0 +dchecks 1.3.4 - 2.4.0 +dhosts 1.3.4 - 2.4.0 +drules 1.3.4 - 2.4.0 +dservices 1.3.4 - 2.4.0 +escalations 1.5.3 - 2.4.0 +events 1.3.1 - 2.4.0 +expressions 1.7 - 2.4.0 +functions 1.3.1 - 2.4.0 +globalmacro 1.7 - 2.4.0 +globalvars 1.9.6 - 2.4.0 +graph_discovery 1.9.0 - 2.4.0 +graph_theme 1.7 - 2.4.0 +graphs 1.3.1 - 2.4.0 +graphs_items 1.3.1 - 2.4.0 +group_discovery 2.1.4 - 2.4.0 +group_prototype 2.1.4 - 2.4.0 +groups 1.3.1 - 2.4.0 +help_items 1.3.1 - 2.1.8 +history 1.3.1 - 2.4.0 +history_log 1.3.1 - 2.4.0 +history_str 1.3.1 - 2.4.0 +history_str_sync 1.3.1 - 2.2.6 +history_sync 1.3.1 - 2.2.6 +history_text 1.3.1 - 2.4.0 +history_uint 1.3.1 - 2.4.0 +history_uint_sync 1.3.1 - 2.2.6 +host_discovery 2.1.4 - 2.4.0 +host_inventory 1.9.6 - 2.4.0 +host_profile 1.9.3 - 1.9.5 +hostmacro 1.7 - 2.4.0 +hosts 1.3.1 - 2.4.0 +hosts_groups 1.3.1 - 2.4.0 +hosts_profiles 1.3.1 - 1.9.2 +hosts_profiles_ext 1.6 - 1.9.2 +hosts_templates 1.3.1 - 2.4.0 +housekeeper 1.3.1 - 2.4.0 +httpstep 1.3.3 - 2.4.0 +httpstepitem 1.3.3 - 2.4.0 +httptest 1.3.3 - 2.4.0 +httptestitem 1.3.3 - 2.4.0 +icon_map 1.9.6 - 2.4.0 +icon_mapping 1.9.6 - 2.4.0 +ids 1.3.3 - 2.4.0 +images 1.3.1 - 2.4.0 +interface 1.9.1 - 2.4.0 +interface_discovery 2.1.4 - 2.4.0 +item_condition 2.3.0 - 2.4.0 +item_discovery 1.9.0 - 2.4.0 +items 1.3.1 - 2.4.0 +items_applications 1.3.1 - 2.4.0 +maintenances 1.7 - 2.4.0 +maintenances_groups 1.7 - 2.4.0 +maintenances_hosts 1.7 - 2.4.0 +maintenances_windows 1.7 - 2.4.0 +mappings 1.3.1 - 2.4.0 +media 1.3.1 - 2.4.0 +media_type 1.3.1 - 2.4.0 +node_cksum 1.3.1 - 2.2.6 +node_configlog 1.3.1 - 1.4.7 +nodes 1.3.1 - 2.2.6 +opcommand 1.9.4 - 2.4.0 +opcommand_grp 1.9.2 - 2.4.0 +opcommand_hst 1.9.2 - 2.4.0 +opconditions 1.5.3 - 2.4.0 +operations 1.3.4 - 2.4.0 +opgroup 1.9.2 - 2.4.0 +opmediatypes 1.7 - 1.8.21 +opmessage 1.9.2 - 2.4.0 +opmessage_grp 1.9.2 - 2.4.0 +opmessage_usr 1.9.2 - 2.4.0 +optemplate 1.9.2 - 2.4.0 +profiles 1.3.1 - 2.4.0 +proxy_autoreg_host 1.7 - 2.4.0 +proxy_dhistory 1.5 - 2.4.0 +proxy_history 1.5.1 - 2.4.0 +regexps 1.7 - 2.4.0 +rights 1.3.1 - 2.4.0 +screens 1.3.1 - 2.4.0 +screens_items 1.3.1 - 2.4.0 +scripts 1.5 - 2.4.0 +service_alarms 1.3.1 - 2.4.0 +services 1.3.1 - 2.4.0 +services_links 1.3.1 - 2.4.0 +services_times 1.3.1 - 2.4.0 +sessions 1.3.1 - 2.4.0 +slides 1.3.4 - 2.4.0 +slideshows 1.3.4 - 2.4.0 +sysmap_element_url 1.9.0 - 2.4.0 +sysmap_url 1.9.0 - 2.4.0 +sysmaps 1.3.1 - 2.4.0 +sysmaps_elements 1.3.1 - 2.4.0 +sysmaps_link_triggers 1.5 - 2.4.0 +sysmaps_links 1.3.1 - 2.4.0 +timeperiods 1.7 - 2.4.0 +trends 1.3.1 - 2.4.0 +trends_uint 1.5 - 2.4.0 +trigger_depends 1.3.1 - 2.4.0 +trigger_discovery 1.9.0 - 2.4.0 +triggers 1.3.1 - 2.4.0 +user_history 1.7 - 2.4.0 +users 1.3.1 - 2.4.0 +users_groups 1.3.1 - 2.4.0 +usrgrp 1.3.1 - 2.4.0 +valuemaps 1.3.1 - 2.4.0 From 5668edb89049854eadddbe177934d0c8d30dbf35 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 19 Sep 2014 01:13:18 +0200 Subject: [PATCH 011/122] DOC Added script description --- get-table-list.pl | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/get-table-list.pl b/get-table-list.pl index 6237503..065df72 100755 --- a/get-table-list.pl +++ b/get-table-list.pl @@ -1,4 +1,23 @@ #!/usr/bin/env perl +# NAME +# get-table-list.pl - List current and historic Zabbix database tables +# +# SYNOPSIS +# This is mainly a helper script for developing the backup script. +# +# It connects to svn://svn.zabbix.com (using Subversion client "svn") and +# fetches the schema definitions of all tagged Zabbix versions beginning +# from 1.3.1 (this takes a while). +# +# It then prints out a list of all tables together with the first and last +# Zabbix version where they were used. +# +# HISTORY +# v0.1 - 2014-09-19 First version +# +# AUTHOR +# Jens Berthold (maxhq), 2014 + use File::Temp qw/ tempfile /; my $tables = {}; # for each table, create a list of Zabbix versions that know it @@ -11,23 +30,23 @@ sub stop { exit; } -# sort tags as version numbers +# sort version numbers correctly sub cmpver { my ($a, $b) = @_; - # split version parts: 3.2.5rc1 --> 3 2 5rc1 + # split version parts: 1.2.3rc1 --> 1 2 3rc1 my @a_parts = split /\./, $a; my @b_parts = split /\./, $b; for (my $i=0; $i= scalar(@b_parts); - # split number parts: 5rc1 --> 5 rc 1 + # split number parts: 3rc1 --> 3 rc 1 my ($a_num, $a_type, $a_idx) = $a_parts[$i] =~ m/^(\d+)(\D+)?(\d+)?$/; my ($b_num, $b_type, $b_idx) = $b_parts[$i] =~ m/^(\d+)(\D+)?(\d+)?$/; my $cmp; - # 4 before 5 + # 3 before 4 $cmp = $a_num <=> $b_num; return $cmp unless $cmp == 0; - # 5rc1 before 5 + # 3rc1 before 3 return -1 if $a_type and not $b_type; return 1 if not $a_type and $b_type; # a1 before b1 @@ -35,7 +54,7 @@ sub cmpver { # rc1 before rc2 $cmp = $a_idx <=> $b_idx; return $cmp unless $cmp == 0; } - # equal up to now, but b has more parts + # 1.2 before 1.2.1 return -1 if scalar(@a_parts) < scalar(@b_parts); # equal return 0; @@ -86,6 +105,9 @@ sub cmpver { unlink $tmpfile; +# +# Print out results +# print "\n\n"; print "TABLE FIRST USE LAST USE\n"; print "-------------------------------------------------\n"; From fd60e6a3bb8e9aa8cf95383ee8830ba10116430e Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 2 Oct 2014 13:05:35 +0200 Subject: [PATCH 012/122] ENH Complete overhaul so that script works with all previous Zabbix versions --- zabbix-mysql-backupconf.sh | 242 ++++++++++++++++++++++++++----------- 1 file changed, 169 insertions(+), 73 deletions(-) mode change 100644 => 100755 zabbix-mysql-backupconf.sh diff --git a/zabbix-mysql-backupconf.sh b/zabbix-mysql-backupconf.sh old mode 100644 new mode 100755 index 57373ea..a9da96f --- a/zabbix-mysql-backupconf.sh +++ b/zabbix-mysql-backupconf.sh @@ -20,6 +20,7 @@ # - Andreas Niedermann (dre-) # # HISTORY +# v0.7 - 2014-10-02 Complete overhaul so that script works with all previous Zabbix versions # v0.6 - 2014-09-15 Updated the table list for use with zabbix v2.2.3 # v0.5 - 2013-05-13 Added table list comparison between database and script # v0.4 - 2012-03-02 Incorporated mysqldump options suggested by Jonathan Bayer @@ -36,10 +37,11 @@ # # mysql database -DBHOST="1.2.3.4" +#DBHOST="10.251.137.201" +DBHOST="10.10.2.31" DBNAME="zabbix" DBUSER="zabbix" -DBPASS="password" +DBPASS="pyerdagIddOdJej:" # backup target path #MAINDIR="/var/lib/zabbix/backupconf" @@ -50,98 +52,192 @@ MAINDIR="`dirname \"$0\"`" # CONSTANTS # -# configuration tables -CONFTABLES=( actions application_template applications autoreg_host conditions \ -config dbversion dchecks dhosts drules dservices escalations expressions functions \ -globalmacro globalvars graph_discovery graph_theme graphs graphs_items group_discovery \ -group_prototype groups host_discovery host_inventory hostmacro hosts \ -hosts_groups hosts_templates housekeeper httpstep httpstepitem httptest \ -httptestitem icon_map icon_mapping ids images interface interface_discovery \ -item_discovery items items_applications maintenances maintenances_groups \ -maintenances_hosts maintenances_windows mappings media media_type node_cksum \ -nodes opcommand opcommand_grp opcommand_hst opconditions operations opgroup \ -opmessage opmessage_grp opmessage_usr optemplate profiles proxy_autoreg_host \ -proxy_dhistory proxy_history regexps rights screens screens_items scripts \ -service_alarms services services_links services_times sessions slides \ -slideshows sysmap_element_url sysmap_url sysmaps sysmaps_elements \ -sysmaps_link_triggers sysmaps_links timeperiods trigger_depends trigger_discovery \ -triggers user_history users users_groups usrgrp valuemaps ) - -# tables with large data -DATATABLES=( acknowledges alerts auditlog_details auditlog events \ -history history_log history_str history_str_sync history_sync history_text \ -history_uint history_uint_sync trends trends_uint ) - +MYSQL_CONN="-h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME}" +MYSQL_BATCH="mysql --batch --silent $MYSQL_CONN" DUMPDIR="${MAINDIR}/`date +%Y%m%d-%H%M`" DUMPFILE="${DUMPDIR}/zabbix-conf-backup-`date +%Y%m%d-%H%M`.sql" # # CHECKS # + if [ ! -x /usr/bin/mysqldump ]; then - echo "mysqldump not found." - echo "(with Debian, \"apt-get install mysql-client\" will help)" - exit 1 + echo "mysqldump not found." + echo "(with Debian, \"apt-get install mysql-client\" will help)" + exit 1 fi # -# compare table list between script and database +# Read table list from __DATA__ section at the end of this script +# (http://stackoverflow.com/a/3477269/2983301) # -FILE_TABLES_LIVE=`mktemp` -FILE_TABLES=`mktemp` - -# Get all current Zabbix tables from databse -mysql -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --batch --silent \ - -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" \ - | sort >> $FILE_TABLES_LIVE - -# Merge CONFTABLES and DATATABLES into one array -allTables=( "${CONFTABLES[@]}" "${DATATABLES[@]}" ) -printf '%s\n' "${allTables[@]}" | sort >> $FILE_TABLES - -difference=`diff --suppress-common-lines $FILE_TABLES $FILE_TABLES_LIVE | grep -v "^\w"` - -if [ ! -z "$difference" ]; then - echo -e "The Zabbix database differs from the configuration in this script." - if [ `echo "$difference" | grep -c "^>"` -gt 0 ]; then - echo -e "\nThese additional tables where found in '$DBNAME' on $DBHOST:" - echo "$difference" | grep "^>" | sed 's/^>/ -/gm' - fi - if [ `echo "$difference" | grep -c "^<"` -gt 0 ]; then - echo -e "\nThese configured tables are missing in '$DBNAME' on $DBHOST:" - echo "$difference" | grep "^<" | sed 's/^"${DUMPFILE}" - -# full backup of configuration tables -echo "Full backup of configuration tables:" -for table in ${CONFTABLES[*]}; do - echo " - ${table}" - mysqldump --routines --opt --single-transaction --skip-lock-tables --extended-insert=FALSE \ - -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --tables ${table} >>"${DUMPFILE}" -done -# scheme backup of large data tables -echo "Scheme backup of data tables:" -for table in ${DATATABLES[*]}; do - echo " - ${table}" - mysqldump --routines --opt --single-transaction --skip-lock-tables --no-data \ - -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --tables ${table} >>"${DUMPFILE}" -done +echo "Starting table backups..." +while read table; do + printf "%s %-25s " "-" "${table}" + if elementIn "$table" "${DATA_TABLES[@]}"; then + echo "only schema" + dump_opt="--no-data" + else + echo "full" + dump_opt="--extended-insert=FALSE" + fi + mysqldump --routines --opt --single-transaction --skip-lock-tables \ + $dump_opt $MYSQL_CONN --tables ${table} >>"${DUMPFILE}" +done < <($MYSQL_BATCH -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" | sort) +echo echo "Compressing backup file ${DUMPFILE}..." gzip -f "${DUMPFILE}" echo echo "Backup Completed - ${DUMPDIR}" -echo "Hit ENTER" -read + +exit + +################################################################################ +# List of all known table names and a flag indicating data (=large) tables +# + +__DATA__ +acknowledges 1.3.1 - 2.4.0 DATA +actions 1.3.1 - 2.4.0 +alerts 1.3.1 - 2.4.0 DATA +application_template 2.1.0 - 2.4.0 +applications 1.3.1 - 2.4.0 +auditlog 1.3.1 - 2.4.0 DATA +auditlog_details 1.7 - 2.4.0 DATA +autoreg 1.3.1 - 1.3.4 +autoreg_host 1.7 - 2.4.0 +conditions 1.3.1 - 2.4.0 +config 1.3.1 - 2.4.0 +dbversion 2.1.0 - 2.4.0 +dchecks 1.3.4 - 2.4.0 +dhosts 1.3.4 - 2.4.0 +drules 1.3.4 - 2.4.0 +dservices 1.3.4 - 2.4.0 +escalations 1.5.3 - 2.4.0 +events 1.3.1 - 2.4.0 DATA +expressions 1.7 - 2.4.0 +functions 1.3.1 - 2.4.0 +globalmacro 1.7 - 2.4.0 +globalvars 1.9.6 - 2.4.0 +graph_discovery 1.9.0 - 2.4.0 +graph_theme 1.7 - 2.4.0 +graphs 1.3.1 - 2.4.0 +graphs_items 1.3.1 - 2.4.0 +group_discovery 2.1.4 - 2.4.0 +group_prototype 2.1.4 - 2.4.0 +groups 1.3.1 - 2.4.0 +help_items 1.3.1 - 2.1.8 +history 1.3.1 - 2.4.0 DATA +history_log 1.3.1 - 2.4.0 DATA +history_str 1.3.1 - 2.4.0 DATA +history_str_sync 1.3.1 - 2.2.6 DATA +history_sync 1.3.1 - 2.2.6 DATA +history_text 1.3.1 - 2.4.0 DATA +history_uint 1.3.1 - 2.4.0 DATA +history_uint_sync 1.3.1 - 2.2.6 DATA +host_discovery 2.1.4 - 2.4.0 +host_inventory 1.9.6 - 2.4.0 +host_profile 1.9.3 - 1.9.5 +hostmacro 1.7 - 2.4.0 +hosts 1.3.1 - 2.4.0 +hosts_groups 1.3.1 - 2.4.0 +hosts_profiles 1.3.1 - 1.9.2 +hosts_profiles_ext 1.6 - 1.9.2 +hosts_templates 1.3.1 - 2.4.0 +housekeeper 1.3.1 - 2.4.0 +httpstep 1.3.3 - 2.4.0 +httpstepitem 1.3.3 - 2.4.0 +httptest 1.3.3 - 2.4.0 +httptestitem 1.3.3 - 2.4.0 +icon_map 1.9.6 - 2.4.0 +icon_mapping 1.9.6 - 2.4.0 +ids 1.3.3 - 2.4.0 +images 1.3.1 - 2.4.0 +interface 1.9.1 - 2.4.0 +interface_discovery 2.1.4 - 2.4.0 +item_condition 2.3.0 - 2.4.0 +item_discovery 1.9.0 - 2.4.0 +items 1.3.1 - 2.4.0 +items_applications 1.3.1 - 2.4.0 +maintenances 1.7 - 2.4.0 +maintenances_groups 1.7 - 2.4.0 +maintenances_hosts 1.7 - 2.4.0 +maintenances_windows 1.7 - 2.4.0 +mappings 1.3.1 - 2.4.0 +media 1.3.1 - 2.4.0 +media_type 1.3.1 - 2.4.0 +node_cksum 1.3.1 - 2.2.6 +node_configlog 1.3.1 - 1.4.7 +nodes 1.3.1 - 2.2.6 +opcommand 1.9.4 - 2.4.0 +opcommand_grp 1.9.2 - 2.4.0 +opcommand_hst 1.9.2 - 2.4.0 +opconditions 1.5.3 - 2.4.0 +operations 1.3.4 - 2.4.0 +opgroup 1.9.2 - 2.4.0 +opmediatypes 1.7 - 1.8.21 +opmessage 1.9.2 - 2.4.0 +opmessage_grp 1.9.2 - 2.4.0 +opmessage_usr 1.9.2 - 2.4.0 +optemplate 1.9.2 - 2.4.0 +profiles 1.3.1 - 2.4.0 +proxy_autoreg_host 1.7 - 2.4.0 +proxy_dhistory 1.5 - 2.4.0 +proxy_history 1.5.1 - 2.4.0 +regexps 1.7 - 2.4.0 +rights 1.3.1 - 2.4.0 +screens 1.3.1 - 2.4.0 +screens_items 1.3.1 - 2.4.0 +scripts 1.5 - 2.4.0 +service_alarms 1.3.1 - 2.4.0 +services 1.3.1 - 2.4.0 +services_links 1.3.1 - 2.4.0 +services_times 1.3.1 - 2.4.0 +sessions 1.3.1 - 2.4.0 +slides 1.3.4 - 2.4.0 +slideshows 1.3.4 - 2.4.0 +sysmap_element_url 1.9.0 - 2.4.0 +sysmap_url 1.9.0 - 2.4.0 +sysmaps 1.3.1 - 2.4.0 +sysmaps_elements 1.3.1 - 2.4.0 +sysmaps_link_triggers 1.5 - 2.4.0 +sysmaps_links 1.3.1 - 2.4.0 +timeperiods 1.7 - 2.4.0 +trends 1.3.1 - 2.4.0 DATA +trends_uint 1.5 - 2.4.0 DATA +trigger_depends 1.3.1 - 2.4.0 +trigger_discovery 1.9.0 - 2.4.0 +triggers 1.3.1 - 2.4.0 +user_history 1.7 - 2.4.0 +users 1.3.1 - 2.4.0 +users_groups 1.3.1 - 2.4.0 +usrgrp 1.3.1 - 2.4.0 +valuemaps 1.3.1 - 2.4.0 From bd56ead90f40e55b124842f859f3f17f53f67b8c Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 2 Oct 2014 13:36:58 +0200 Subject: [PATCH 013/122] REV Show backup progress percentage instead of each table name REV Show list of large data tables after backup --- zabbix-mysql-backupconf.sh | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/zabbix-mysql-backupconf.sh b/zabbix-mysql-backupconf.sh index a9da96f..9fac53e 100755 --- a/zabbix-mysql-backupconf.sh +++ b/zabbix-mysql-backupconf.sh @@ -71,6 +71,7 @@ fi # Read table list from __DATA__ section at the end of this script # (http://stackoverflow.com/a/3477269/2983301) # + DATA_TABLES=() while read line; do table=$(echo "$line" | cut -d" " -f1) @@ -97,26 +98,48 @@ elementIn () { mkdir -p "${DUMPDIR}" +# Read table list from database +DB_TABLES=$($MYSQL_BATCH -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" | sort) +DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) + +PROCESSED_DATA_TABLES=() +i=0 + echo "Starting table backups..." while read table; do - printf "%s %-25s " "-" "${table}" + # large data tables: only store schema if elementIn "$table" "${DATA_TABLES[@]}"; then - echo "only schema" dump_opt="--no-data" + PROCESSED_DATA_TABLES+=($table) + # configuration tables: full dump else - echo "full" dump_opt="--extended-insert=FALSE" fi mysqldump --routines --opt --single-transaction --skip-lock-tables \ $dump_opt $MYSQL_CONN --tables ${table} >>"${DUMPFILE}" -done < <($MYSQL_BATCH -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" | sort) -echo + # show percentage + i=$((i+1)); i_percent=$(($i * 100 / $DB_TABLE_NUM)) + if [ $(($i_percent % 12)) -eq 0 ]; then + echo -n "${i_percent}%" + else + if [ $(($i_percent % 2)) -eq 0 ]; then echo -n "."; fi + fi +done <<<"$DB_TABLES" + +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 "Compressing backup file ${DUMPFILE}..." gzip -f "${DUMPFILE}" +if [ $? -ne 0 ]; then + echo -e "\nERROR: Could not compress backup file, see previous messages" + exit +fi -echo -echo "Backup Completed - ${DUMPDIR}" +echo -e "\nBackup Completed - ${DUMPDIR}" exit From 796d9f5992fc8d1441734a0c2e7f4f0242727571 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 2 Oct 2014 13:36:58 +0200 Subject: [PATCH 014/122] REV Show backup progress percentage instead of each table name REV Show list of large data tables after backup --- zabbix-mysql-backupconf.sh | 42 +++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/zabbix-mysql-backupconf.sh b/zabbix-mysql-backupconf.sh index a9da96f..d6571ec 100755 --- a/zabbix-mysql-backupconf.sh +++ b/zabbix-mysql-backupconf.sh @@ -37,11 +37,10 @@ # # mysql database -#DBHOST="10.251.137.201" -DBHOST="10.10.2.31" +DBHOST="1.2.3.4" DBNAME="zabbix" DBUSER="zabbix" -DBPASS="pyerdagIddOdJej:" +DBPASS="zabbix" # backup target path #MAINDIR="/var/lib/zabbix/backupconf" @@ -71,6 +70,7 @@ fi # Read table list from __DATA__ section at the end of this script # (http://stackoverflow.com/a/3477269/2983301) # + DATA_TABLES=() while read line; do table=$(echo "$line" | cut -d" " -f1) @@ -97,26 +97,48 @@ elementIn () { mkdir -p "${DUMPDIR}" +# Read table list from database +DB_TABLES=$($MYSQL_BATCH -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" | sort) +DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) + +PROCESSED_DATA_TABLES=() +i=0 + echo "Starting table backups..." while read table; do - printf "%s %-25s " "-" "${table}" + # large data tables: only store schema if elementIn "$table" "${DATA_TABLES[@]}"; then - echo "only schema" dump_opt="--no-data" + PROCESSED_DATA_TABLES+=($table) + # configuration tables: full dump else - echo "full" dump_opt="--extended-insert=FALSE" fi mysqldump --routines --opt --single-transaction --skip-lock-tables \ $dump_opt $MYSQL_CONN --tables ${table} >>"${DUMPFILE}" -done < <($MYSQL_BATCH -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" | sort) -echo + # show percentage + i=$((i+1)); i_percent=$(($i * 100 / $DB_TABLE_NUM)) + if [ $(($i_percent % 12)) -eq 0 ]; then + echo -n "${i_percent}%" + else + if [ $(($i_percent % 2)) -eq 0 ]; then echo -n "."; fi + fi +done <<<"$DB_TABLES" + +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 "Compressing backup file ${DUMPFILE}..." gzip -f "${DUMPFILE}" +if [ $? -ne 0 ]; then + echo -e "\nERROR: Could not compress backup file, see previous messages" + exit +fi -echo -echo "Backup Completed - ${DUMPDIR}" +echo -e "\nBackup Completed - ${DUMPDIR}" exit From aa225d70fce78580d574048f64ad36e9869d3ef5 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 2 Oct 2014 13:43:11 +0200 Subject: [PATCH 015/122] REV Removed spacing and changed password --- zabbix-mysql-backupconf.sh | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/zabbix-mysql-backupconf.sh b/zabbix-mysql-backupconf.sh index d6571ec..98aec6d 100755 --- a/zabbix-mysql-backupconf.sh +++ b/zabbix-mysql-backupconf.sh @@ -35,12 +35,11 @@ # # CONFIGURATION # - # mysql database DBHOST="1.2.3.4" DBNAME="zabbix" DBUSER="zabbix" -DBPASS="zabbix" +DBPASS="password" # backup target path #MAINDIR="/var/lib/zabbix/backupconf" @@ -50,7 +49,6 @@ MAINDIR="`dirname \"$0\"`" # # CONSTANTS # - MYSQL_CONN="-h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME}" MYSQL_BATCH="mysql --batch --silent $MYSQL_CONN" DUMPDIR="${MAINDIR}/`date +%Y%m%d-%H%M`" @@ -59,7 +57,6 @@ DUMPFILE="${DUMPDIR}/zabbix-conf-backup-`date +%Y%m%d-%H%M`.sql" # # CHECKS # - if [ ! -x /usr/bin/mysqldump ]; then echo "mysqldump not found." echo "(with Debian, \"apt-get install mysql-client\" will help)" @@ -70,7 +67,6 @@ fi # Read table list from __DATA__ section at the end of this script # (http://stackoverflow.com/a/3477269/2983301) # - DATA_TABLES=() while read line; do table=$(echo "$line" | cut -d" " -f1) @@ -87,7 +83,6 @@ fi # # BACKUP # - # Returns TRUE if argument 1 is part of the given array (remaining arguments) elementIn () { local e From 1d23e1d27648fb793bd9cbc88a06fdf1e0dbb852 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 2 Oct 2014 13:44:54 +0200 Subject: [PATCH 016/122] REV Rearranged some code --- zabbix-mysql-backupconf.sh | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/zabbix-mysql-backupconf.sh b/zabbix-mysql-backupconf.sh index 98aec6d..2a5d72d 100755 --- a/zabbix-mysql-backupconf.sh +++ b/zabbix-mysql-backupconf.sh @@ -29,7 +29,7 @@ # v0.2 - 2011-11-05 # # AUTHOR -# Jens Berthold (maxhq), 2013 +# Jens Berthold (maxhq), 2014 # @@ -54,6 +54,17 @@ MYSQL_BATCH="mysql --batch --silent $MYSQL_CONN" DUMPDIR="${MAINDIR}/`date +%Y%m%d-%H%M`" DUMPFILE="${DUMPDIR}/zabbix-conf-backup-`date +%Y%m%d-%H%M`.sql" +# +# FUNCTIONS +# + +# Returns TRUE if argument 1 is part of the given array (remaining arguments) +elementIn () { + local e + for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done + return 1 +} + # # CHECKS # @@ -64,7 +75,7 @@ if [ ! -x /usr/bin/mysqldump ]; then fi # -# Read table list from __DATA__ section at the end of this script +# READ TABLE LIST from __DATA__ section at the end of this script # (http://stackoverflow.com/a/3477269/2983301) # DATA_TABLES=() @@ -83,13 +94,6 @@ fi # # BACKUP # -# Returns TRUE if argument 1 is part of the given array (remaining arguments) -elementIn () { - local e - for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done - return 1 -} - mkdir -p "${DUMPDIR}" # Read table list from database From b9578ab33e579909bf18051afc05cc143cf36332 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 2 Oct 2014 13:45:43 +0200 Subject: [PATCH 017/122] REV Removed two spaces in output of get-table-list.pl --- get-table-list.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/get-table-list.pl b/get-table-list.pl index 065df72..4fe583e 100755 --- a/get-table-list.pl +++ b/get-table-list.pl @@ -112,5 +112,5 @@ sub cmpver { print "TABLE FIRST USE LAST USE\n"; print "-------------------------------------------------\n"; for my $tab (sort keys %$tables) { - printf "%-25s %-10s - %s\n", $tab, $tables->{$tab}->[0], $tables->{$tab}->[-1]; + printf "%-25s %-8s - %s\n", $tab, $tables->{$tab}->[0], $tables->{$tab}->[-1]; } From 8e2bbcb0afe0210035384be366b4cfc6ff6ceeee Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 2 Oct 2014 13:52:57 +0200 Subject: [PATCH 018/122] DOC Updated README.txt REV Removed table.txt as table list is now in the script --- README | 12 +++++- tables.txt | 120 ----------------------------------------------------- 2 files changed, 11 insertions(+), 121 deletions(-) delete mode 100644 tables.txt diff --git a/README b/README index 487301f..7457130 100644 --- a/README +++ b/README @@ -1,4 +1,14 @@ -This is a database backup script for Zabbix 2.2.3 that distincts between configuration and mass data tables. +This is a database backup script for all Zabbix versions up to 2.4.0. +It distincts between configuration and mass data tables. Tables holding configuration data will be fully backuped while for mass data tables only the table schema is stored. + This way you have a small backup of your complete configuration, and in case of disaster "only" use your item history. + +---------- + +zabbix-mysql-backupconf.sh + Zabbix backup script for MySQL databases + +get-table-list.pl + Shows all database tables ever used (since 1.3.1) by querying Zabbix Subversion repository diff --git a/tables.txt b/tables.txt deleted file mode 100644 index eb68cd9..0000000 --- a/tables.txt +++ /dev/null @@ -1,120 +0,0 @@ -This is the recent (2.4.0) result list of show-tables.pl: - -TABLE FIRST USE LAST USE -------------------------------------------------- -acknowledges 1.3.1 - 2.4.0 -actions 1.3.1 - 2.4.0 -alerts 1.3.1 - 2.4.0 -application_template 2.1.0 - 2.4.0 -applications 1.3.1 - 2.4.0 -auditlog 1.3.1 - 2.4.0 -auditlog_details 1.7 - 2.4.0 -autoreg 1.3.1 - 1.3.4 -autoreg_host 1.7 - 2.4.0 -conditions 1.3.1 - 2.4.0 -config 1.3.1 - 2.4.0 -dbversion 2.1.0 - 2.4.0 -dchecks 1.3.4 - 2.4.0 -dhosts 1.3.4 - 2.4.0 -drules 1.3.4 - 2.4.0 -dservices 1.3.4 - 2.4.0 -escalations 1.5.3 - 2.4.0 -events 1.3.1 - 2.4.0 -expressions 1.7 - 2.4.0 -functions 1.3.1 - 2.4.0 -globalmacro 1.7 - 2.4.0 -globalvars 1.9.6 - 2.4.0 -graph_discovery 1.9.0 - 2.4.0 -graph_theme 1.7 - 2.4.0 -graphs 1.3.1 - 2.4.0 -graphs_items 1.3.1 - 2.4.0 -group_discovery 2.1.4 - 2.4.0 -group_prototype 2.1.4 - 2.4.0 -groups 1.3.1 - 2.4.0 -help_items 1.3.1 - 2.1.8 -history 1.3.1 - 2.4.0 -history_log 1.3.1 - 2.4.0 -history_str 1.3.1 - 2.4.0 -history_str_sync 1.3.1 - 2.2.6 -history_sync 1.3.1 - 2.2.6 -history_text 1.3.1 - 2.4.0 -history_uint 1.3.1 - 2.4.0 -history_uint_sync 1.3.1 - 2.2.6 -host_discovery 2.1.4 - 2.4.0 -host_inventory 1.9.6 - 2.4.0 -host_profile 1.9.3 - 1.9.5 -hostmacro 1.7 - 2.4.0 -hosts 1.3.1 - 2.4.0 -hosts_groups 1.3.1 - 2.4.0 -hosts_profiles 1.3.1 - 1.9.2 -hosts_profiles_ext 1.6 - 1.9.2 -hosts_templates 1.3.1 - 2.4.0 -housekeeper 1.3.1 - 2.4.0 -httpstep 1.3.3 - 2.4.0 -httpstepitem 1.3.3 - 2.4.0 -httptest 1.3.3 - 2.4.0 -httptestitem 1.3.3 - 2.4.0 -icon_map 1.9.6 - 2.4.0 -icon_mapping 1.9.6 - 2.4.0 -ids 1.3.3 - 2.4.0 -images 1.3.1 - 2.4.0 -interface 1.9.1 - 2.4.0 -interface_discovery 2.1.4 - 2.4.0 -item_condition 2.3.0 - 2.4.0 -item_discovery 1.9.0 - 2.4.0 -items 1.3.1 - 2.4.0 -items_applications 1.3.1 - 2.4.0 -maintenances 1.7 - 2.4.0 -maintenances_groups 1.7 - 2.4.0 -maintenances_hosts 1.7 - 2.4.0 -maintenances_windows 1.7 - 2.4.0 -mappings 1.3.1 - 2.4.0 -media 1.3.1 - 2.4.0 -media_type 1.3.1 - 2.4.0 -node_cksum 1.3.1 - 2.2.6 -node_configlog 1.3.1 - 1.4.7 -nodes 1.3.1 - 2.2.6 -opcommand 1.9.4 - 2.4.0 -opcommand_grp 1.9.2 - 2.4.0 -opcommand_hst 1.9.2 - 2.4.0 -opconditions 1.5.3 - 2.4.0 -operations 1.3.4 - 2.4.0 -opgroup 1.9.2 - 2.4.0 -opmediatypes 1.7 - 1.8.21 -opmessage 1.9.2 - 2.4.0 -opmessage_grp 1.9.2 - 2.4.0 -opmessage_usr 1.9.2 - 2.4.0 -optemplate 1.9.2 - 2.4.0 -profiles 1.3.1 - 2.4.0 -proxy_autoreg_host 1.7 - 2.4.0 -proxy_dhistory 1.5 - 2.4.0 -proxy_history 1.5.1 - 2.4.0 -regexps 1.7 - 2.4.0 -rights 1.3.1 - 2.4.0 -screens 1.3.1 - 2.4.0 -screens_items 1.3.1 - 2.4.0 -scripts 1.5 - 2.4.0 -service_alarms 1.3.1 - 2.4.0 -services 1.3.1 - 2.4.0 -services_links 1.3.1 - 2.4.0 -services_times 1.3.1 - 2.4.0 -sessions 1.3.1 - 2.4.0 -slides 1.3.4 - 2.4.0 -slideshows 1.3.4 - 2.4.0 -sysmap_element_url 1.9.0 - 2.4.0 -sysmap_url 1.9.0 - 2.4.0 -sysmaps 1.3.1 - 2.4.0 -sysmaps_elements 1.3.1 - 2.4.0 -sysmaps_link_triggers 1.5 - 2.4.0 -sysmaps_links 1.3.1 - 2.4.0 -timeperiods 1.7 - 2.4.0 -trends 1.3.1 - 2.4.0 -trends_uint 1.5 - 2.4.0 -trigger_depends 1.3.1 - 2.4.0 -trigger_discovery 1.9.0 - 2.4.0 -triggers 1.3.1 - 2.4.0 -user_history 1.7 - 2.4.0 -users 1.3.1 - 2.4.0 -users_groups 1.3.1 - 2.4.0 -usrgrp 1.3.1 - 2.4.0 -valuemaps 1.3.1 - 2.4.0 From c2d92caa7b04e8d395ed5a854c0ac03f3890ee33 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 2 Oct 2014 16:01:28 +0200 Subject: [PATCH 019/122] DOC Updated README (reference to Github wiki) --- README | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/README b/README index 7457130..32e7fbf 100644 --- a/README +++ b/README @@ -1,14 +1,3 @@ -This is a database backup script for all Zabbix versions up to 2.4.0. -It distincts between configuration and mass data tables. +This is a database backup script for all Zabbix versions from 1.3.1 up to 2.4.0. -Tables holding configuration data will be fully backuped while for mass data tables only the table schema is stored. - -This way you have a small backup of your complete configuration, and in case of disaster "only" use your item history. - ----------- - -zabbix-mysql-backupconf.sh - Zabbix backup script for MySQL databases - -get-table-list.pl - Shows all database tables ever used (since 1.3.1) by querying Zabbix Subversion repository +For more informations, see the Wiki (https://github.com/maxhq/zabbix-backup/wiki). \ No newline at end of file From 08bc7e304a6c8cc0ad65a5a6daee3154a3904f4b Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 2 Oct 2014 16:01:54 +0200 Subject: [PATCH 020/122] DOC +"MySQL" --- README | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README b/README index 32e7fbf..e8e7707 100644 --- a/README +++ b/README @@ -1,3 +1,3 @@ -This is a database backup script for all Zabbix versions from 1.3.1 up to 2.4.0. +This is a MySQL database backup script for all Zabbix versions from 1.3.1 up to 2.4.0. For more informations, see the Wiki (https://github.com/maxhq/zabbix-backup/wiki). \ No newline at end of file From a41feda0e81c293ddbd55ad0409e71f6fba13034 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 2 Oct 2014 17:18:52 +0200 Subject: [PATCH 021/122] DOC Added ".0" to version number in script --- zabbix-mysql-backupconf.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/zabbix-mysql-backupconf.sh b/zabbix-mysql-backupconf.sh index 2a5d72d..48c2550 100755 --- a/zabbix-mysql-backupconf.sh +++ b/zabbix-mysql-backupconf.sh @@ -20,12 +20,12 @@ # - Andreas Niedermann (dre-) # # HISTORY -# v0.7 - 2014-10-02 Complete overhaul so that script works with all previous Zabbix versions -# v0.6 - 2014-09-15 Updated the table list for use with zabbix v2.2.3 -# v0.5 - 2013-05-13 Added table list comparison between database and script -# v0.4 - 2012-03-02 Incorporated mysqldump options suggested by Jonathan Bayer -# v0.3 - 2012-02-06 Backup of Zabbix 1.9.x / 2.0.0, removed unnecessary use of -# variables (DATEBIN etc) for commands that use to be in $PATH +# v0.7.0 - 2014-10-02 Complete overhaul so that script works with all previous Zabbix versions +# v0.6 - 2014-09-15 Updated the table list for use with zabbix v2.2.3 +# v0.5 - 2013-05-13 Added table list comparison between database and script +# v0.4 - 2012-03-02 Incorporated mysqldump options suggested by Jonathan Bayer +# v0.3 - 2012-02-06 Backup of Zabbix 1.9.x / 2.0.0, removed unnecessary use of +# variables (DATEBIN etc) for commands that use to be in $PATH # v0.2 - 2011-11-05 # # AUTHOR From 9b11a37fb38f4ab01fdfce35969354f77f7c5f56 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Tue, 27 Jan 2015 22:05:06 +0100 Subject: [PATCH 022/122] NEW Parsing of commandline arguments implemented --- zabbix-mysql-backupconf.sh | 127 ++++++++++++++++++++++++++++--------- 1 file changed, 97 insertions(+), 30 deletions(-) diff --git a/zabbix-mysql-backupconf.sh b/zabbix-mysql-backupconf.sh index 48c2550..caaacb7 100755 --- a/zabbix-mysql-backupconf.sh +++ b/zabbix-mysql-backupconf.sh @@ -3,14 +3,12 @@ # zabbix-mysql-backupconf.sh - Configuration Backup for Zabbix 2.0 w/MySQL # # SYNOPSIS -# This is a MySQL configuration backup script for Zabbix v2.0 oder 2.2. +# This is a MySQL configuration backup script for Zabbix 2.0, 2.2 or 2.4. # It does a full backup of all configuration tables, but only a schema # backup of large data tables. # -# The original script was written by Ricardo Santos and published at -# http://zabbixzone.com/zabbix/backuping-only-the-zabbix-configuration/ -# and -# https://github.com/xsbr/zabbixzone/blob/master/zabbix-mysql-backupconf.sh +# The script is based on a script by Ricardo Santos +# (http://zabbixzone.com/zabbix/backuping-only-the-zabbix-configuration/) # # Credits for some suggestions concerning the original script to: # - Ricardo Santos @@ -20,39 +18,107 @@ # - Andreas Niedermann (dre-) # # HISTORY -# v0.7.0 - 2014-10-02 Complete overhaul so that script works with all previous Zabbix versions -# v0.6 - 2014-09-15 Updated the table list for use with zabbix v2.2.3 -# v0.5 - 2013-05-13 Added table list comparison between database and script -# v0.4 - 2012-03-02 Incorporated mysqldump options suggested by Jonathan Bayer -# v0.3 - 2012-02-06 Backup of Zabbix 1.9.x / 2.0.0, removed unnecessary use of -# variables (DATEBIN etc) for commands that use to be in $PATH -# v0.2 - 2011-11-05 +# 0.7.1 (2015-01-27) +# NEW: Parsing of commandline arguments implemented +# +# 0.7.0 (2014-10-02) +# ENH: Complete overhaul to make script work with lots of Zabbix versions +# +# 0.6.0 (2014-09-15) +# REV: Updated the table list for use with zabbix v2.2.3 +# +# 0.5.0 (2013-05-13) +# NEW: Added table list comparison between database and script +# +# 0.4.0 (2012-03-02) +# REV: Incorporated mysqldump options suggested by Jonathan Bayer +# +# 0.3.0 (2012-02-06) +# ENH: Backup of Zabbix 1.9.x / 2.0.0, removed unnecessary use of +# variables (DATEBIN etc) for commands that use to be in $PATH +# +# 0.2.0 (2011-11-05) # # AUTHOR -# Jens Berthold (maxhq), 2014 +# Jens Berthold (maxhq), 2015 # -# CONFIGURATION +# DEFAULT VALUES # -# mysql database -DBHOST="1.2.3.4" +# following will store the backup in a subdirectory of the current directory +DUMPDIR="$(dirname "$(realpath -s "$0")")" +DBHOST="127.0.0.1" DBNAME="zabbix" DBUSER="zabbix" -DBPASS="password" +DBPASS="" -# backup target path -#MAINDIR="/var/lib/zabbix/backupconf" -# following will store the backup in a subdirectory of the current directory -MAINDIR="`dirname \"$0\"`" +# +# SHOW HELP +# +if [ -z "$1" ]; then + cat <&2; exit 1 ;; + :) echo "Option -$OPTARG requires an argument" >&2; exit 1 ;; + esac +done + +if [ "$DBPASS" = "" ]; then + echo "No password given" >&2 + exit 1 +fi + +if [ "$DBPASS" = "-" ]; then + read -s -p "Enter MySQL password for user '$DBUSER' (input will be hidden): " DBPASS + echo "" +fi # # CONSTANTS # MYSQL_CONN="-h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME}" MYSQL_BATCH="mysql --batch --silent $MYSQL_CONN" -DUMPDIR="${MAINDIR}/`date +%Y%m%d-%H%M`" -DUMPFILE="${DUMPDIR}/zabbix-conf-backup-`date +%Y%m%d-%H%M`.sql" + +DUMPFILEBASE="zabbix_$(date +%Y%m%d-%H%M).sql" +DUMPFILE="${DUMPDIR}/${DUMPFILEBASE}" + +# +# CONFIG DUMP +# +cat <&2 + echo "(with Debian, \"apt-get install mysql-client\" will help)" >&2 exit 1 fi @@ -87,7 +153,7 @@ done < <(sed '0,/^__DATA__$/d' "$0" | tr -s " ") # paranoid check if [ ${#DATA_TABLES[@]} -lt 5 ]; then - echo "ERROR: The number of large data tables configurd in this script is less than 5." + echo "ERROR: The number of large data tables configured in this script is less than 5." >&2 exit 1 fi @@ -97,6 +163,7 @@ fi mkdir -p "${DUMPDIR}" # Read table list from database +echo "Fetching list of existing tables..." DB_TABLES=$($MYSQL_BATCH -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" | sort) DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) @@ -130,14 +197,14 @@ 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 "Compressing backup file ${DUMPFILE}..." +echo "Compressing backup file..." gzip -f "${DUMPFILE}" if [ $? -ne 0 ]; then - echo -e "\nERROR: Could not compress backup file, see previous messages" - exit + echo -e "\nERROR: Could not compress backup file, see previous messages" >&2 + exit 1 fi -echo -e "\nBackup Completed - ${DUMPDIR}" +echo -e "\nBackup Completed:\n${DUMPFILE}.gz" exit From 139e0365ea3fe84634fcef3de57ccd8f7ab6db8c Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Tue, 27 Jan 2015 22:06:48 +0100 Subject: [PATCH 023/122] REV Stop if database password is wrong --- zabbix-mysql-backupconf.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/zabbix-mysql-backupconf.sh b/zabbix-mysql-backupconf.sh index caaacb7..bb083d4 100755 --- a/zabbix-mysql-backupconf.sh +++ b/zabbix-mysql-backupconf.sh @@ -20,6 +20,7 @@ # HISTORY # 0.7.1 (2015-01-27) # NEW: Parsing of commandline arguments implemented +# 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 @@ -164,7 +165,9 @@ mkdir -p "${DUMPDIR}" # Read table list from database echo "Fetching list of existing tables..." -DB_TABLES=$($MYSQL_BATCH -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" | sort) +DB_TABLES=$($MYSQL_BATCH -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" 2>&1) +if [ $? -ne 0 ]; then echo -e "ERROR while trying to access database:\n$DB_TABLES" 2>&1; exit 1; fi +DB_TABLES=$(echo "$DB_TABLES" | sort) DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) PROCESSED_DATA_TABLES=() From cf69f0105c9721a53971097a7b3c5f0a1556efba Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Tue, 27 Jan 2015 22:07:41 +0100 Subject: [PATCH 024/122] ENH Try reverse lookup of IPs and include hostname/IP in filename --- zabbix-mysql-backupconf.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/zabbix-mysql-backupconf.sh b/zabbix-mysql-backupconf.sh index bb083d4..b37d874 100755 --- a/zabbix-mysql-backupconf.sh +++ b/zabbix-mysql-backupconf.sh @@ -20,6 +20,7 @@ # HISTORY # 0.7.1 (2015-01-27) # 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) @@ -108,7 +109,12 @@ fi MYSQL_CONN="-h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME}" MYSQL_BATCH="mysql --batch --silent $MYSQL_CONN" -DUMPFILEBASE="zabbix_$(date +%Y%m%d-%H%M).sql" +# Try resolving a given host ip +DBHOSTNAME="$DBHOST" +newHostname=$(dig +noall +answer -x $DBHOST | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') +test \! -z "$newHostname" && DBHOSTNAME="$newHostname" + +DUMPFILEBASE="zabbix_${DBHOSTNAME}_$(date +%Y%m%d-%H%M).sql" DUMPFILE="${DUMPDIR}/${DUMPFILEBASE}" # From 7e880955e589c82a8c9205c71e1b3d8865ca97e9 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Tue, 27 Jan 2015 22:08:54 +0100 Subject: [PATCH 025/122] REV Renamed script to zabbix-mysql-dump --- zabbix-mysql-backupconf.sh => zabbix-mysql-dump | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename zabbix-mysql-backupconf.sh => zabbix-mysql-dump (100%) diff --git a/zabbix-mysql-backupconf.sh b/zabbix-mysql-dump similarity index 100% rename from zabbix-mysql-backupconf.sh rename to zabbix-mysql-dump From 9614a42852cea165ccde8162c271579be37ca4e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C8=99u=20Moldovan?= Date: Tue, 7 Apr 2015 16:55:42 +0300 Subject: [PATCH 026/122] Bash-related fixes. --- zabbix-mysql-dump | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index b37d874..171eb65 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -1,4 +1,5 @@ -#!/bin/bash +#!/usr/bin/env bash +# # NAME # zabbix-mysql-backupconf.sh - Configuration Backup for Zabbix 2.0 w/MySQL # @@ -16,8 +17,12 @@ # - Petr Jendrejovsky # - Jonathan Bayer # - Andreas Niedermann (dre-) +# - Mișu Moldovan # # HISTORY +# 0.7.2 (2015-04-07) +# ENH: Bash related fixes. +# # 0.7.1 (2015-01-27) # NEW: Parsing of commandline arguments implemented # ENH: Try reverse lookup of IPs and include hostname/IP in filename @@ -49,7 +54,7 @@ # DEFAULT VALUES # # following will store the backup in a subdirectory of the current directory -DUMPDIR="$(dirname "$(realpath -s "$0")")" +DUMPDIR="$(dirname "$(realpath -s "$BASH_SOURCE")")" DBHOST="127.0.0.1" DBNAME="zabbix" DBUSER="zabbix" @@ -61,7 +66,7 @@ DBPASS="" if [ -z "$1" ]; then cat < Date: Fri, 22 Jan 2016 12:59:50 +0100 Subject: [PATCH 027/122] CHG Default output directory is now $PWD instead of script dir Thank you @endersonmaia, @dschneller and @dumol for pointing out the strangeness of the former solution ;-) --- zabbix-mysql-dump | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 171eb65..eae47fc 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -17,7 +17,7 @@ # - Petr Jendrejovsky # - Jonathan Bayer # - Andreas Niedermann (dre-) -# - Mișu Moldovan +# - Mișu Moldovan (dumol) # # HISTORY # 0.7.2 (2015-04-07) @@ -53,8 +53,9 @@ # # DEFAULT VALUES # -# following will store the backup in a subdirectory of the current directory -DUMPDIR="$(dirname "$(realpath -s "$BASH_SOURCE")")" +# store backup below current directory +DUMPDIR="$PWD" +# DB settings DBHOST="127.0.0.1" DBNAME="zabbix" DBUSER="zabbix" @@ -73,7 +74,7 @@ OPTIONS -d database - Zabbix database name (default: $DBNAME) -u user - MySQL user to access Zabbix database (default: $DBUSER) -p password - MySQL user password (specify "-" for a prompt) - -o outputdir - output directory for the MySQL dump file + -o outputdir - output directory for MySQL dump (default: $DUMPDIR) EXAMPLE From cb689980e4d9fb6a89ed1f0ca9bc4c15ec19b909 Mon Sep 17 00:00:00 2001 From: Daniel Schneller Date: Thu, 12 Mar 2015 14:31:46 +0100 Subject: [PATCH 028/122] ENH Add quiet mode. IP reverse lookup optional. * Adds a -q switch to suppress all output in case of regular successful runs. This makes the script much more suitable for unattended runs via cron where any other output would usually cause a notification mail. * Adds a -n switch to optionally skip the reverse IP address lookup. --- zabbix-mysql-dump | 70 +++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index eae47fc..d699033 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -60,6 +60,8 @@ DBHOST="127.0.0.1" DBNAME="zabbix" DBUSER="zabbix" DBPASS="" +QUIET="no" +REVERSELOOKUP="yes" # # SHOW HELP @@ -76,7 +78,8 @@ OPTIONS -p password - MySQL user password (specify "-" for a prompt) -o outputdir - output directory for MySQL dump (default: $DUMPDIR) - + -q - No output on succesful backup. For crontab use. + -n - Skip reverse lookup of IP address for host. EXAMPLE $(basename $BASH_SOURCE) -h 1.2.3.4 -d zabbixdb -u zabbix -p test $(basename $BASH_SOURCE) -d zabbixdb -u zabbix -p - -o /tmp @@ -87,13 +90,15 @@ fi # # PARSE COMMAND LINE ARGUMENTS # -while getopts ":h:d:u:p:o:" opt; do +while getopts ":h:d:u:p:o:qn" opt; do case $opt in h) DBHOST="$OPTARG" ;; d) DBNAME="$OPTARG" ;; u) DBUSER="$OPTARG" ;; p) DBPASS="$OPTARG" ;; o) DUMPDIR="$OPTARG" ;; + n) REVERSELOOKUP="no" ;; + q) QUIET="yes" ;; \?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;; :) echo "Option -$OPTARG requires an argument" >&2; exit 1 ;; esac @@ -115,24 +120,26 @@ fi MYSQL_CONN="-h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME}" MYSQL_BATCH="mysql --batch --silent $MYSQL_CONN" -# Try resolving a given host ip DBHOSTNAME="$DBHOST" -newHostname=$(dig +noall +answer -x $DBHOST | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') -test \! -z "$newHostname" && DBHOSTNAME="$newHostname" - +if [ "${REVERSELOOKUP}" == "yes" ]; then + # Try resolving a given host ip + newHostname=$(dig +noall +answer -x $DBHOST | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') + test \! -z "$newHostname" && DBHOSTNAME="$newHostname" +fi DUMPFILEBASE="zabbix_${DBHOSTNAME}_$(date +%Y%m%d-%H%M).sql" DUMPFILE="${DUMPDIR}/${DUMPFILEBASE}" # # CONFIG DUMP # -cat <&1) if [ $? -ne 0 ]; then echo -e "ERROR while trying to access database:\n$DB_TABLES" 2>&1; exit 1; fi DB_TABLES=$(echo "$DB_TABLES" | sort) @@ -185,7 +192,7 @@ DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) PROCESSED_DATA_TABLES=() i=0 -echo "Starting table backups..." +[ "${QUIET}" == "no" ] && echo "Starting table backups..." while read table; do # large data tables: only store schema if elementIn "$table" "${DATA_TABLES[@]}"; then @@ -197,31 +204,34 @@ while read table; do fi mysqldump --routines --opt --single-transaction --skip-lock-tables \ $dump_opt $MYSQL_CONN --tables ${table} >>"${DUMPFILE}" - - # show percentage - i=$((i+1)); i_percent=$(($i * 100 / $DB_TABLE_NUM)) - if [ $(($i_percent % 12)) -eq 0 ]; then - echo -n "${i_percent}%" - else - if [ $(($i_percent % 2)) -eq 0 ]; then echo -n "."; fi + if [ "${QUIET}" == "no" ]; then + # show percentage + i=$((i+1)); i_percent=$(($i * 100 / $DB_TABLE_NUM)) + if [ $(($i_percent % 12)) -eq 0 ]; then + echo -n "${i_percent}%" + else + if [ $(($i_percent % 2)) -eq 0 ]; then echo -n "."; fi + fi fi done <<<"$DB_TABLES" -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 "Compressing backup file..." +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 "Compressing backup file..." +fi gzip -f "${DUMPFILE}" if [ $? -ne 0 ]; then echo -e "\nERROR: Could not compress backup file, see previous messages" >&2 exit 1 fi -echo -e "\nBackup Completed:\n${DUMPFILE}.gz" +[ "${QUIET}" == "no" ] && echo -e "\nBackup Completed:\n${DUMPFILE}.gz" -exit +exit 0 ################################################################################ # List of all known table names and a flag indicating data (=large) tables From d2695652eccce3d1d30a11e15b9d82ee9ea4f8db Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 22 Jan 2016 13:37:59 +0100 Subject: [PATCH 029/122] DOC Refreshed and corrected some docs and formatting --- zabbix-mysql-dump | 81 +++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index d699033..78bd944 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -1,7 +1,7 @@ #!/usr/bin/env bash # # NAME -# zabbix-mysql-backupconf.sh - Configuration Backup for Zabbix 2.0 w/MySQL +# zabbix-mysql-dump - Configuration Backup for Zabbix with MySQL # # SYNOPSIS # This is a MySQL configuration backup script for Zabbix 2.0, 2.2 or 2.4. @@ -11,17 +11,20 @@ # The script is based on a script by Ricardo Santos # (http://zabbixzone.com/zabbix/backuping-only-the-zabbix-configuration/) # -# Credits for some suggestions concerning the original script to: +# CONTRIBUTORS # - Ricardo Santos +# - Jens Berthold (maxhq) # - Oleksiy Zagorskyi (zalex) # - Petr Jendrejovsky # - Jonathan Bayer # - Andreas Niedermann (dre-) # - Mișu Moldovan (dumol) +# - Daniel Schneller (dschneller) # # HISTORY -# 0.7.2 (2015-04-07) -# ENH: Bash related fixes. +# 0.7.2 (2016-01-22) +# ENH: Add quiet mode. IP reverse lookup optional (by Daniel Schneller) +# ENH: Bash related fixes (by Misu Moldovan) # # 0.7.1 (2015-01-27) # NEW: Parsing of commandline arguments implemented @@ -47,7 +50,7 @@ # 0.2.0 (2011-11-05) # # AUTHOR -# Jens Berthold (maxhq), 2015 +# Jens Berthold (maxhq), 2016 # @@ -72,17 +75,19 @@ USAGE $(basename $BASH_SOURCE) [options] OPTIONS - -h host - hostname/IP of MySQL server (default: $DBHOST) - -d database - Zabbix database name (default: $DBNAME) - -u user - MySQL user to access Zabbix database (default: $DBUSER) - -p password - MySQL user password (specify "-" for a prompt) - -o outputdir - output directory for MySQL dump - (default: $DUMPDIR) - -q - No output on succesful backup. For crontab use. - -n - Skip reverse lookup of IP address for host. + -h host - hostname/IP of MySQL server (default: $DBHOST) + -d database - Zabbix database name (default: $DBNAME) + -u user - MySQL user to access Zabbix database (default: $DBUSER) + -p password - MySQL user password (specify "-" for a prompt) + -o outputdir - output directory for MySQL dump + (default: $DUMPDIR) + -q - No output on succesful backup. For crontab use. + -n - Skip reverse lookup of IP address for host. + EXAMPLE $(basename $BASH_SOURCE) -h 1.2.3.4 -d zabbixdb -u zabbix -p test $(basename $BASH_SOURCE) -d zabbixdb -u zabbix -p - -o /tmp + EOF exit 1 fi @@ -91,17 +96,17 @@ fi # PARSE COMMAND LINE ARGUMENTS # while getopts ":h:d:u:p:o:qn" opt; do - case $opt in - h) DBHOST="$OPTARG" ;; - d) DBNAME="$OPTARG" ;; - u) DBUSER="$OPTARG" ;; - p) DBPASS="$OPTARG" ;; - o) DUMPDIR="$OPTARG" ;; - n) REVERSELOOKUP="no" ;; - q) QUIET="yes" ;; - \?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;; - :) echo "Option -$OPTARG requires an argument" >&2; exit 1 ;; - esac + case $opt in + h) DBHOST="$OPTARG" ;; + d) DBNAME="$OPTARG" ;; + u) DBUSER="$OPTARG" ;; + p) DBPASS="$OPTARG" ;; + o) DUMPDIR="$OPTARG" ;; + n) REVERSELOOKUP="no" ;; + q) QUIET="yes" ;; + \?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;; + :) echo "Option -$OPTARG requires an argument" >&2; exit 1 ;; + esac done if [ "$DBPASS" = "" ]; then @@ -117,22 +122,23 @@ fi # # CONSTANTS # -MYSQL_CONN="-h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME}" +MYSQL_CONN="-h $DBHOST -u $DBUSER -p$DBPASS $DBNAME" MYSQL_BATCH="mysql --batch --silent $MYSQL_CONN" DBHOSTNAME="$DBHOST" -if [ "${REVERSELOOKUP}" == "yes" ]; then +command -v dig >/dev/null 2>&1 +if [ "$REVERSELOOKUP" == "yes" ]; then # Try resolving a given host ip newHostname=$(dig +noall +answer -x $DBHOST | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') test \! -z "$newHostname" && DBHOSTNAME="$newHostname" fi DUMPFILEBASE="zabbix_${DBHOSTNAME}_$(date +%Y%m%d-%H%M).sql" -DUMPFILE="${DUMPDIR}/${DUMPFILEBASE}" +DUMPFILE="$DUMPDIR/$DUMPFILEBASE" # # CONFIG DUMP # -if [ "${QUIET}" == "no" ]; then +if [ "$QUIET" == "no" ]; then cat <<-EOF Configuration: - host: $DBHOST ($DBHOSTNAME) @@ -183,7 +189,7 @@ fi mkdir -p "${DUMPDIR}" # Read table list from database -[ "${QUIET}" == "no" ] && echo "Fetching list of existing tables..." +[ "$QUIET" == "no" ] && echo "Fetching list of existing tables..." DB_TABLES=$($MYSQL_BATCH -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" 2>&1) if [ $? -ne 0 ]; then echo -e "ERROR while trying to access database:\n$DB_TABLES" 2>&1; exit 1; fi DB_TABLES=$(echo "$DB_TABLES" | sort) @@ -192,7 +198,7 @@ DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) PROCESSED_DATA_TABLES=() i=0 -[ "${QUIET}" == "no" ] && echo "Starting table backups..." +[ "$QUIET" == "no" ] && echo "Starting table backups..." while read table; do # large data tables: only store schema if elementIn "$table" "${DATA_TABLES[@]}"; then @@ -202,9 +208,11 @@ while read table; do else dump_opt="--extended-insert=FALSE" fi + mysqldump --routines --opt --single-transaction --skip-lock-tables \ - $dump_opt $MYSQL_CONN --tables ${table} >>"${DUMPFILE}" - if [ "${QUIET}" == "no" ]; then + $dump_opt $MYSQL_CONN --tables ${table} >> "$DUMPFILE" + + if [ "$QUIET" == "no" ]; then # show percentage i=$((i+1)); i_percent=$(($i * 100 / $DB_TABLE_NUM)) if [ $(($i_percent % 12)) -eq 0 ]; then @@ -215,7 +223,10 @@ while read table; do fi done <<<"$DB_TABLES" -if [ "${QUIET}" == "no" ]; then +# +# Compress backup file +# +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 @@ -223,13 +234,13 @@ if [ "${QUIET}" == "no" ]; then echo echo "Compressing backup file..." fi -gzip -f "${DUMPFILE}" +gzip -f "$DUMPFILE" if [ $? -ne 0 ]; then echo -e "\nERROR: Could not compress backup file, see previous messages" >&2 exit 1 fi -[ "${QUIET}" == "no" ] && echo -e "\nBackup Completed:\n${DUMPFILE}.gz" +[ "$QUIET" == "no" ] && echo -e "\nBackup Completed:\n${DUMPFILE}.gz" exit 0 From 1df2b0ccf6851d0e0101bc445cfd007aa0f5697a Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 22 Jan 2016 13:43:13 +0100 Subject: [PATCH 030/122] FIX Only invoke `dig` if available --- zabbix-mysql-dump | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 78bd944..6feecd9 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -127,7 +127,8 @@ MYSQL_BATCH="mysql --batch --silent $MYSQL_CONN" DBHOSTNAME="$DBHOST" command -v dig >/dev/null 2>&1 -if [ "$REVERSELOOKUP" == "yes" ]; then +FIND_DIG=$? +if [ "$REVERSELOOKUP" == "yes" -a $FIND_DIG -eq 0 ]; then # Try resolving a given host ip newHostname=$(dig +noall +answer -x $DBHOST | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') test \! -z "$newHostname" && DBHOSTNAME="$newHostname" From ae5493602e7713d6e83ffbde339036fd34e05590 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 22 Jan 2016 14:48:20 +0100 Subject: [PATCH 031/122] ENH Add database version to filename if available The database version is queried from table `dbversion` (field "optional") and appended to the filename. --- zabbix-mysql-dump | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 6feecd9..8668ee8 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -23,8 +23,11 @@ # # HISTORY # 0.7.2 (2016-01-22) +# FIX: Only invoke `dig` if available +# ENH: Add database version to filename if available # ENH: Add quiet mode. IP reverse lookup optional (by Daniel Schneller) # ENH: Bash related fixes (by Misu Moldovan) +# CHG: Default output directory is now $PWD instead of script dir # # 0.7.1 (2015-01-27) # NEW: Parsing of commandline arguments implemented @@ -133,8 +136,6 @@ if [ "$REVERSELOOKUP" == "yes" -a $FIND_DIG -eq 0 ]; then newHostname=$(dig +noall +answer -x $DBHOST | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') test \! -z "$newHostname" && DBHOSTNAME="$newHostname" fi -DUMPFILEBASE="zabbix_${DBHOSTNAME}_$(date +%Y%m%d-%H%M).sql" -DUMPFILE="$DUMPDIR/$DUMPFILEBASE" # # CONFIG DUMP @@ -145,6 +146,8 @@ if [ "$QUIET" == "no" ]; then - host: $DBHOST ($DBHOSTNAME) - database: $DBNAME - user: $DBUSER + - output: $DUMPDIR + EOF fi # @@ -187,8 +190,6 @@ fi # # BACKUP # -mkdir -p "${DUMPDIR}" - # Read table list from database [ "$QUIET" == "no" ] && echo "Fetching list of existing tables..." DB_TABLES=$($MYSQL_BATCH -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" 2>&1) @@ -196,9 +197,25 @@ if [ $? -ne 0 ]; then echo -e "ERROR while trying to access database:\n$DB_TABLE DB_TABLES=$(echo "$DB_TABLES" | sort) DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) +# Query Zabbix database version +VERSION="" +DB_VER=$($MYSQL_BATCH -N -e "select optional from dbversion;" 2>&1) +if [ $? -eq 0 ]; then + # version string is like: 02030015 + re='(.*)([0-9]{2})([0-9]{4})' + if [[ $DB_VER =~ $re ]]; then + VERSION="_db-${BASH_REMATCH[1]}.$(( ${BASH_REMATCH[2]} + 0 )).$(( ${BASH_REMATCH[3]} + 0 ))" + fi +fi + +DUMPFILEBASE="zabbix_${DBHOSTNAME}_$(date +%Y%m%d-%H%M)${VERSION}.sql" +DUMPFILE="$DUMPDIR/$DUMPFILEBASE" + PROCESSED_DATA_TABLES=() i=0 +mkdir -p "${DUMPDIR}" + [ "$QUIET" == "no" ] && echo "Starting table backups..." while read table; do # large data tables: only store schema From 026ada3bef953757e99f9a2fe4ab4e6fc926fb31 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 22 Jan 2016 15:02:24 +0100 Subject: [PATCH 032/122] ENH Option -r to rotate backup files (by Daniel Schneller) --- zabbix-mysql-dump | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 8668ee8..2422725 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -24,6 +24,7 @@ # HISTORY # 0.7.2 (2016-01-22) # FIX: Only invoke `dig` if available +# ENH: Option -r to rotate backup files (by Daniel Schneller) # ENH: Add database version to filename if available # ENH: Add quiet mode. IP reverse lookup optional (by Daniel Schneller) # ENH: Bash related fixes (by Misu Moldovan) @@ -68,6 +69,7 @@ DBUSER="zabbix" DBPASS="" QUIET="no" REVERSELOOKUP="yes" +GENERATIONSTOKEEP=0 # # SHOW HELP @@ -83,7 +85,9 @@ OPTIONS -u user - MySQL user to access Zabbix database (default: $DBUSER) -p password - MySQL user password (specify "-" for a prompt) -o outputdir - output directory for MySQL dump - (default: $DUMPDIR) + (default: $DUMPDIR) + -r num - Rotate backups, keeping up to num generations in the + output directory. Uses filename to match (default: keep all) -q - No output on succesful backup. For crontab use. -n - Skip reverse lookup of IP address for host. @@ -98,13 +102,14 @@ fi # # PARSE COMMAND LINE ARGUMENTS # -while getopts ":h:d:u:p:o:qn" opt; do +while getopts ":h:d:u:p:o:r:qn" opt; do case $opt in h) DBHOST="$OPTARG" ;; d) DBNAME="$OPTARG" ;; u) DBUSER="$OPTARG" ;; p) DBPASS="$OPTARG" ;; o) DUMPDIR="$OPTARG" ;; + r) GENERATIONSTOKEEP=$(printf '%.0f' "$OPTARG") ;; n) REVERSELOOKUP="no" ;; q) QUIET="yes" ;; \?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;; @@ -208,7 +213,9 @@ if [ $? -eq 0 ]; then fi fi -DUMPFILEBASE="zabbix_${DBHOSTNAME}_$(date +%Y%m%d-%H%M)${VERSION}.sql" +# Assemble file name +DUMPFILENAME_PREFIX="zabbix_cfg_${DBHOSTNAME}" +DUMPFILEBASE="${DUMPFILENAME_PREFIX}_$(date +%Y%m%d-%H%M)${VERSION}.sql" DUMPFILE="$DUMPDIR/$DUMPFILEBASE" PROCESSED_DATA_TABLES=() @@ -242,7 +249,7 @@ while read table; do done <<<"$DB_TABLES" # -# Compress backup file +# COMPRESS BACKUP # if [ "$QUIET" == "no" ]; then echo -e "\n" @@ -260,6 +267,19 @@ fi [ "$QUIET" == "no" ] && echo -e "\nBackup Completed:\n${DUMPFILE}.gz" +# +# ROTATE OLD BACKUPS +# +if [ $GENERATIONSTOKEEP -gt 0 ]; then + [ "$QUIET" == "no" ] && echo "Removing old backups, keeping up to $GENERATIONSTOKEEP" + REMOVE_OLD_CMD="cd \"$DUMPDIR\" && ls -t \"${DUMPFILENAME_PREFIX}\"* | /usr/bin/awk \"NR>${GENERATIONSTOKEEP}\" | xargs rm -f " + eval ${REMOVE_OLD_CMD} + if [ $? -ne 0 ]; then + echo "ERROR: Could not rotate old backups" >&2 + exit 1 + fi +fi + exit 0 ################################################################################ From 1feed5939bafe34965c0ba7cfeb2f61453881de4 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 22 Jan 2016 16:58:24 +0100 Subject: [PATCH 033/122] ENH Option -c to use a MySQL config ("options") file Allows storing credentials etc. in a protected file instead of having to provide them via script parameter. The idea came from @dschneller but I had to change his implementation to be error free and more convenient for different use cases. Now as with MySQL all options in the config file can still be overwritten by command line parameters. --- zabbix-mysql-dump | 70 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 2422725..a7650c8 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -24,10 +24,11 @@ # HISTORY # 0.7.2 (2016-01-22) # FIX: Only invoke `dig` if available -# ENH: Option -r to rotate backup files (by Daniel Schneller) +# ENH: Option -c to use a MySQL config ("options") file (suggested by Daniel Schneller) +# ENH: Option -r to rotate backup files (Daniel Schneller) # ENH: Add database version to filename if available -# ENH: Add quiet mode. IP reverse lookup optional (by Daniel Schneller) -# ENH: Bash related fixes (by Misu Moldovan) +# ENH: Add quiet mode. IP reverse lookup optional (Daniel Schneller) +# ENH: Bash related fixes (Misu Moldovan) # CHG: Default output directory is now $PWD instead of script dir # # 0.7.1 (2015-01-27) @@ -45,7 +46,7 @@ # NEW: Added table list comparison between database and script # # 0.4.0 (2012-03-02) -# REV: Incorporated mysqldump options suggested by Jonathan Bayer +# REV: Incorporated mysqldump options (suggested by Jonathan Bayer) # # 0.3.0 (2012-02-06) # ENH: Backup of Zabbix 1.9.x / 2.0.0, removed unnecessary use of @@ -84,7 +85,10 @@ OPTIONS -d database - Zabbix database name (default: $DBNAME) -u user - MySQL user to access Zabbix database (default: $DBUSER) -p password - MySQL user password (specify "-" for a prompt) - -o outputdir - output directory for MySQL dump + -c file - Use the given MySQL options file. + Special note for option "database": the first one found in + the file via grep is used for mysqldump! + -o outputdir - output directory for MySQL dump. (default: $DUMPDIR) -r num - Rotate backups, keeping up to num generations in the output directory. Uses filename to match (default: keep all) @@ -94,6 +98,8 @@ OPTIONS EXAMPLE $(basename $BASH_SOURCE) -h 1.2.3.4 -d zabbixdb -u zabbix -p test $(basename $BASH_SOURCE) -d zabbixdb -u zabbix -p - -o /tmp + $(basename $BASH_SOURCE) -h 1.2.3.4 -d zabbixdb -c /etc/mysql-backup-credentials.cnf + $(basename $BASH_SOURCE) -c /etc/mysql/mysql.cnf EOF exit 1 @@ -102,12 +108,13 @@ fi # # PARSE COMMAND LINE ARGUMENTS # -while getopts ":h:d:u:p:o:r:qn" opt; do +while getopts ":h:d:u:p:o:r:c:qn" opt; do case $opt in h) DBHOST="$OPTARG" ;; d) DBNAME="$OPTARG" ;; u) DBUSER="$OPTARG" ;; p) DBPASS="$OPTARG" ;; + c) CNFFILE="$OPTARG" ;; o) DUMPDIR="$OPTARG" ;; r) GENERATIONSTOKEEP=$(printf '%.0f' "$OPTARG") ;; n) REVERSELOOKUP="no" ;; @@ -117,22 +124,39 @@ while getopts ":h:d:u:p:o:r:qn" opt; do esac done -if [ "$DBPASS" = "" ]; then - echo "No password given" >&2 - exit 1 -fi - +# Password prompt if [ "$DBPASS" = "-" ]; then read -s -p "Enter MySQL password for user '$DBUSER' (input will be hidden): " DBPASS echo "" fi +# Config file validations +if [ ! -z "$CNFFILE" ]; then + if [ ! -r "$CNFFILE" ]; then + echo "ERROR: Cannot read configuration file $CNFFILE" >&2 + exit 1 + fi + # Database name needs special treatment: + # For mysqldump it has to be specified on the command line! + # Therefore we need to get it from the config file + if [ -z "$DBNAME" ]; then + DBNAME=$(grep -m 1 ^database= "$CNFFILE" | cut -d= -f2) + fi +fi + # # CONSTANTS # -MYSQL_CONN="-h $DBHOST -u $DBUSER -p$DBPASS $DBNAME" -MYSQL_BATCH="mysql --batch --silent $MYSQL_CONN" +MYSQL_OPTS=() +[ ! -z "$CNFFILE" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" --defaults-extra-file="$CNFFILE") +[ ! -z "$DBHOST" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" -h $DBHOST) +[ ! -z "$DBUSER" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" -u $DBUSER) +[ ! -z "$DBPASS" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" -p"$DBPASS") +MYSQL_OPTS_BATCH=("${MYSQL_OPTS[@]}" --batch --silent) +[ ! -z "$DBNAME" ] && MYSQL_OPTS_BATCH=("${MYSQL_OPTS_BATCH[@]}" -D $DBNAME) + +# Host name: try reverse lookup if IP is given DBHOSTNAME="$DBHOST" command -v dig >/dev/null 2>&1 FIND_DIG=$? @@ -197,20 +221,20 @@ fi # # Read table list from database [ "$QUIET" == "no" ] && echo "Fetching list of existing tables..." -DB_TABLES=$($MYSQL_BATCH -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" 2>&1) +DB_TABLES=$(mysql "${MYSQL_OPTS_BATCH[@]}" -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" 2>&1) if [ $? -ne 0 ]; then echo -e "ERROR while trying to access database:\n$DB_TABLES" 2>&1; exit 1; fi DB_TABLES=$(echo "$DB_TABLES" | sort) DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) # Query Zabbix database version VERSION="" -DB_VER=$($MYSQL_BATCH -N -e "select optional from dbversion;" 2>&1) +DB_VER=$(my "${MYSQL_OPTS_BATCH[@]}" -N -e "select optional from dbversion;" 2>&1) if [ $? -eq 0 ]; then - # version string is like: 02030015 - re='(.*)([0-9]{2})([0-9]{4})' - if [[ $DB_VER =~ $re ]]; then - VERSION="_db-${BASH_REMATCH[1]}.$(( ${BASH_REMATCH[2]} + 0 )).$(( ${BASH_REMATCH[3]} + 0 ))" - fi + # version string is like: 02030015 + re='(.*)([0-9]{2})([0-9]{4})' + if [[ $DB_VER =~ $re ]]; then + VERSION="_db-${BASH_REMATCH[1]}.$(( ${BASH_REMATCH[2]} + 0 )).$(( ${BASH_REMATCH[3]} + 0 ))" + fi fi # Assemble file name @@ -234,8 +258,10 @@ while read table; do dump_opt="--extended-insert=FALSE" fi - mysqldump --routines --opt --single-transaction --skip-lock-tables \ - $dump_opt $MYSQL_CONN --tables ${table} >> "$DUMPFILE" + mysqldump "${MYSQL_OPTS[@]}" \ + --routines --opt --single-transaction --skip-lock-tables \ + $dump_opt \ + $DBNAME --tables ${table} >> "$DUMPFILE" if [ "$QUIET" == "no" ]; then # show percentage From cbb6161c0422b4a6b09f255c60c743b99be63533 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 22 Jan 2016 17:02:59 +0100 Subject: [PATCH 034/122] DOC Added hint not to modify default variable values --- zabbix-mysql-dump | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index a7650c8..3eebdb2 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -60,10 +60,11 @@ # # DEFAULT VALUES +# +# DO NOT EDIT THESE VALUES! +# Instead, use command line parameters or a config file to specify options. # -# store backup below current directory DUMPDIR="$PWD" -# DB settings DBHOST="127.0.0.1" DBNAME="zabbix" DBUSER="zabbix" From 0a8bc9e408c9fa42d4e34d6bdef955797b8909fc Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 22 Jan 2016 17:05:35 +0100 Subject: [PATCH 035/122] FIX Fixed typo "my" --> "mysql" --- zabbix-mysql-dump | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 3eebdb2..93c6936 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -229,7 +229,7 @@ DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) # Query Zabbix database version VERSION="" -DB_VER=$(my "${MYSQL_OPTS_BATCH[@]}" -N -e "select optional from dbversion;" 2>&1) +DB_VER=$(mysql "${MYSQL_OPTS_BATCH[@]}" -N -e "select optional from dbversion;" 2>&1) if [ $? -eq 0 ]; then # version string is like: 02030015 re='(.*)([0-9]{2})([0-9]{4})' From 91f655cb977505510fd24ee1bc1f05f91878e511 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 22 Jan 2016 17:30:42 +0100 Subject: [PATCH 036/122] DOC Reformatted help page and adjusted examples --- zabbix-mysql-dump | 60 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 93c6936..588c0fd 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -4,7 +4,7 @@ # zabbix-mysql-dump - Configuration Backup for Zabbix with MySQL # # SYNOPSIS -# This is a MySQL configuration backup script for Zabbix 2.0, 2.2 or 2.4. +# This is a MySQL configuration backup script for Zabbix 1.x and 2.x. # It does a full backup of all configuration tables, but only a schema # backup of large data tables. # @@ -82,25 +82,49 @@ USAGE $(basename $BASH_SOURCE) [options] OPTIONS - -h host - hostname/IP of MySQL server (default: $DBHOST) - -d database - Zabbix database name (default: $DBNAME) - -u user - MySQL user to access Zabbix database (default: $DBUSER) - -p password - MySQL user password (specify "-" for a prompt) - -c file - Use the given MySQL options file. - Special note for option "database": the first one found in - the file via grep is used for mysqldump! - -o outputdir - output directory for MySQL dump. - (default: $DUMPDIR) - -r num - Rotate backups, keeping up to num generations in the - output directory. Uses filename to match (default: keep all) - -q - No output on succesful backup. For crontab use. - -n - Skip reverse lookup of IP address for host. - -EXAMPLE + -h HOST + Hostname/IP of MySQL server. + Default: $DBHOST + + -d DATABASE + Zabbix database name. + Default: $DBNAME + + -u USER + MySQL user to access Zabbix database. + Default: $DBUSER + + -p PASSWORD + MySQL user password (specify "-" for a prompt). + Default: no password + + -o DIR + Save Zabbix MySQL dumps to DIR. + Default: $DUMPDIR + + -c FILE + Use FILE for MySQL options (passed via --defaults-extra-file). + PLEASE NOTE: + mysqldump needs the database to be specified via command line. + So the first "database" options found in the config file is + used for mysqldump. + + -r NUM + Rotate backups while keeping up to NUM generations. + Uses filename to match. + Default: keep all backups + + -n + Skip reverse lookup of IP address for host. + + -q + Quiet mode: no output except for errors (for batch/crontab use). + +EXAMPLES $(basename $BASH_SOURCE) -h 1.2.3.4 -d zabbixdb -u zabbix -p test - $(basename $BASH_SOURCE) -d zabbixdb -u zabbix -p - -o /tmp - $(basename $BASH_SOURCE) -h 1.2.3.4 -d zabbixdb -c /etc/mysql-backup-credentials.cnf + $(basename $BASH_SOURCE) -u zabbix -p - -o /tmp $(basename $BASH_SOURCE) -c /etc/mysql/mysql.cnf + $(basename $BASH_SOURCE) -c /etc/mysql/mysql.cnf -d zabbixdb EOF exit 1 From a337a236941184e8d5050afaba48f5197ab784b9 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 22 Jan 2016 17:32:26 +0100 Subject: [PATCH 037/122] FIX Reading database name from config file did not work The if-statement would never become true so that always the default DB would be used if none was specified via command line. --- zabbix-mysql-dump | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 588c0fd..7e1f9af 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -85,7 +85,7 @@ OPTIONS -h HOST Hostname/IP of MySQL server. Default: $DBHOST - + -d DATABASE Zabbix database name. Default: $DBNAME @@ -93,30 +93,30 @@ OPTIONS -u USER MySQL user to access Zabbix database. Default: $DBUSER - + -p PASSWORD MySQL user password (specify "-" for a prompt). Default: no password - + -o DIR Save Zabbix MySQL dumps to DIR. Default: $DUMPDIR - + -c FILE Use FILE for MySQL options (passed via --defaults-extra-file). PLEASE NOTE: mysqldump needs the database to be specified via command line. So the first "database" options found in the config file is used for mysqldump. - + -r NUM Rotate backups while keeping up to NUM generations. Uses filename to match. Default: keep all backups - + -n Skip reverse lookup of IP address for host. - + -q Quiet mode: no output except for errors (for batch/crontab use). @@ -133,10 +133,11 @@ fi # # PARSE COMMAND LINE ARGUMENTS # +DB_GIVEN=0 while getopts ":h:d:u:p:o:r:c:qn" opt; do case $opt in h) DBHOST="$OPTARG" ;; - d) DBNAME="$OPTARG" ;; + d) DBNAME="$OPTARG"; DB_GIVEN=1 ;; u) DBUSER="$OPTARG" ;; p) DBPASS="$OPTARG" ;; c) CNFFILE="$OPTARG" ;; @@ -164,7 +165,7 @@ if [ ! -z "$CNFFILE" ]; then # Database name needs special treatment: # For mysqldump it has to be specified on the command line! # Therefore we need to get it from the config file - if [ -z "$DBNAME" ]; then + if [ $DB_GIVEN -eq 0 ]; then DBNAME=$(grep -m 1 ^database= "$CNFFILE" | cut -d= -f2) fi fi @@ -204,6 +205,7 @@ if [ "$QUIET" == "no" ]; then EOF fi + # # FUNCTIONS # From 6168774b32dab96b69f360e16b10bfc76938a2f8 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 22 Jan 2016 17:50:42 +0100 Subject: [PATCH 038/122] DOC Changed version number to 0.8.0 --- zabbix-mysql-dump | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 7e1f9af..2895847 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -22,7 +22,7 @@ # - Daniel Schneller (dschneller) # # HISTORY -# 0.7.2 (2016-01-22) +# 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) # ENH: Option -r to rotate backup files (Daniel Schneller) From fb816b98341183a18d7f1a516efdb0d97559b924 Mon Sep 17 00:00:00 2001 From: Nikita Gubenko Date: Wed, 23 Mar 2016 11:09:08 -0700 Subject: [PATCH 039/122] Fixes issue when stderr output is included in backup file name, e.g. if you specify password in the cli, you'll have "zabbix_cfg_localhost_20160323-1758_db-Warning: Using a password on the command line interface can be insecure.?2.4.0.sql.gz" --- zabbix-mysql-dump | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 2895847..ffd8625 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -255,7 +255,7 @@ DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) # Query Zabbix database version VERSION="" -DB_VER=$(mysql "${MYSQL_OPTS_BATCH[@]}" -N -e "select optional from dbversion;" 2>&1) +DB_VER=$(mysql "${MYSQL_OPTS_BATCH[@]}" -N -e "select optional from dbversion;" 2>/dev/null) if [ $? -eq 0 ]; then # version string is like: 02030015 re='(.*)([0-9]{2})([0-9]{4})' From 8226d44492bd80ea6415ead1765ef1f25b4ac904 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Sat, 26 Mar 2016 00:44:07 +0000 Subject: [PATCH 040/122] NEW Added MIT license --- LICENSE.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE.txt diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..3e9f6c9 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Jens Berthold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 4be030e7d174b6e02ae915d28a38ccbfde00857e Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Sat, 26 Mar 2016 00:49:41 +0000 Subject: [PATCH 041/122] DOC Mention license in script --- zabbix-mysql-dump | 3 +++ 1 file changed, 3 insertions(+) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index ffd8625..c1e88f5 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -56,6 +56,9 @@ # # AUTHOR # Jens Berthold (maxhq), 2016 +# +# LICENSE +# This script is released under the MIT License (see LICENSE.txt) # From 4ab9f54aa0f9efc6dbbef3a766720d8fd0914e2b Mon Sep 17 00:00:00 2001 From: ruslan-ohitin Date: Fri, 1 Jul 2016 16:46:21 +0600 Subject: [PATCH 042/122] Change versions 2.4.0 -> 3.0.0 in tables list --- zabbix-mysql-dump | 208 +++++++++++++++++++++++----------------------- 1 file changed, 104 insertions(+), 104 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index c1e88f5..3c5511e 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -4,7 +4,7 @@ # zabbix-mysql-dump - Configuration Backup for Zabbix with MySQL # # SYNOPSIS -# This is a MySQL configuration backup script for Zabbix 1.x and 2.x. +# This is a MySQL configuration backup script for Zabbix 1.x, 2.x and 3.0.x. # It does a full backup of all configuration tables, but only a schema # backup of large data tables. # @@ -343,119 +343,119 @@ exit 0 # __DATA__ -acknowledges 1.3.1 - 2.4.0 DATA -actions 1.3.1 - 2.4.0 -alerts 1.3.1 - 2.4.0 DATA -application_template 2.1.0 - 2.4.0 -applications 1.3.1 - 2.4.0 -auditlog 1.3.1 - 2.4.0 DATA -auditlog_details 1.7 - 2.4.0 DATA +acknowledges 1.3.1 - 3.0.0 DATA +actions 1.3.1 - 3.0.0 +alerts 1.3.1 - 3.0.0 DATA +application_template 2.1.0 - 3.0.0 +applications 1.3.1 - 3.0.0 +auditlog 1.3.1 - 3.0.0 DATA +auditlog_details 1.7 - 3.0.0 DATA autoreg 1.3.1 - 1.3.4 -autoreg_host 1.7 - 2.4.0 -conditions 1.3.1 - 2.4.0 -config 1.3.1 - 2.4.0 -dbversion 2.1.0 - 2.4.0 -dchecks 1.3.4 - 2.4.0 -dhosts 1.3.4 - 2.4.0 -drules 1.3.4 - 2.4.0 -dservices 1.3.4 - 2.4.0 -escalations 1.5.3 - 2.4.0 -events 1.3.1 - 2.4.0 DATA -expressions 1.7 - 2.4.0 -functions 1.3.1 - 2.4.0 -globalmacro 1.7 - 2.4.0 -globalvars 1.9.6 - 2.4.0 -graph_discovery 1.9.0 - 2.4.0 -graph_theme 1.7 - 2.4.0 -graphs 1.3.1 - 2.4.0 -graphs_items 1.3.1 - 2.4.0 -group_discovery 2.1.4 - 2.4.0 -group_prototype 2.1.4 - 2.4.0 -groups 1.3.1 - 2.4.0 +autoreg_host 1.7 - 3.0.0 +conditions 1.3.1 - 3.0.0 +config 1.3.1 - 3.0.0 +dbversion 2.1.0 - 3.0.0 +dchecks 1.3.4 - 3.0.0 +dhosts 1.3.4 - 3.0.0 +drules 1.3.4 - 3.0.0 +dservices 1.3.4 - 3.0.0 +escalations 1.5.3 - 3.0.0 +events 1.3.1 - 3.0.0 DATA +expressions 1.7 - 3.0.0 +functions 1.3.1 - 3.0.0 +globalmacro 1.7 - 3.0.0 +globalvars 1.9.6 - 3.0.0 +graph_discovery 1.9.0 - 3.0.0 +graph_theme 1.7 - 3.0.0 +graphs 1.3.1 - 3.0.0 +graphs_items 1.3.1 - 3.0.0 +group_discovery 2.1.4 - 3.0.0 +group_prototype 2.1.4 - 3.0.0 +groups 1.3.1 - 3.0.0 help_items 1.3.1 - 2.1.8 -history 1.3.1 - 2.4.0 DATA -history_log 1.3.1 - 2.4.0 DATA -history_str 1.3.1 - 2.4.0 DATA +history 1.3.1 - 3.0.0 DATA +history_log 1.3.1 - 3.0.0 DATA +history_str 1.3.1 - 3.0.0 DATA history_str_sync 1.3.1 - 2.2.6 DATA history_sync 1.3.1 - 2.2.6 DATA -history_text 1.3.1 - 2.4.0 DATA -history_uint 1.3.1 - 2.4.0 DATA +history_text 1.3.1 - 3.0.0 DATA +history_uint 1.3.1 - 3.0.0 DATA history_uint_sync 1.3.1 - 2.2.6 DATA -host_discovery 2.1.4 - 2.4.0 -host_inventory 1.9.6 - 2.4.0 +host_discovery 2.1.4 - 3.0.0 +host_inventory 1.9.6 - 3.0.0 host_profile 1.9.3 - 1.9.5 -hostmacro 1.7 - 2.4.0 -hosts 1.3.1 - 2.4.0 -hosts_groups 1.3.1 - 2.4.0 +hostmacro 1.7 - 3.0.0 +hosts 1.3.1 - 3.0.0 +hosts_groups 1.3.1 - 3.0.0 hosts_profiles 1.3.1 - 1.9.2 hosts_profiles_ext 1.6 - 1.9.2 -hosts_templates 1.3.1 - 2.4.0 -housekeeper 1.3.1 - 2.4.0 -httpstep 1.3.3 - 2.4.0 -httpstepitem 1.3.3 - 2.4.0 -httptest 1.3.3 - 2.4.0 -httptestitem 1.3.3 - 2.4.0 -icon_map 1.9.6 - 2.4.0 -icon_mapping 1.9.6 - 2.4.0 -ids 1.3.3 - 2.4.0 -images 1.3.1 - 2.4.0 -interface 1.9.1 - 2.4.0 -interface_discovery 2.1.4 - 2.4.0 -item_condition 2.3.0 - 2.4.0 -item_discovery 1.9.0 - 2.4.0 -items 1.3.1 - 2.4.0 -items_applications 1.3.1 - 2.4.0 -maintenances 1.7 - 2.4.0 -maintenances_groups 1.7 - 2.4.0 -maintenances_hosts 1.7 - 2.4.0 -maintenances_windows 1.7 - 2.4.0 -mappings 1.3.1 - 2.4.0 -media 1.3.1 - 2.4.0 -media_type 1.3.1 - 2.4.0 +hosts_templates 1.3.1 - 3.0.0 +housekeeper 1.3.1 - 3.0.0 +httpstep 1.3.3 - 3.0.0 +httpstepitem 1.3.3 - 3.0.0 +httptest 1.3.3 - 3.0.0 +httptestitem 1.3.3 - 3.0.0 +icon_map 1.9.6 - 3.0.0 +icon_mapping 1.9.6 - 3.0.0 +ids 1.3.3 - 3.0.0 +images 1.3.1 - 3.0.0 +interface 1.9.1 - 3.0.0 +interface_discovery 2.1.4 - 3.0.0 +item_condition 2.3.0 - 3.0.0 +item_discovery 1.9.0 - 3.0.0 +items 1.3.1 - 3.0.0 +items_applications 1.3.1 - 3.0.0 +maintenances 1.7 - 3.0.0 +maintenances_groups 1.7 - 3.0.0 +maintenances_hosts 1.7 - 3.0.0 +maintenances_windows 1.7 - 3.0.0 +mappings 1.3.1 - 3.0.0 +media 1.3.1 - 3.0.0 +media_type 1.3.1 - 3.0.0 node_cksum 1.3.1 - 2.2.6 node_configlog 1.3.1 - 1.4.7 nodes 1.3.1 - 2.2.6 -opcommand 1.9.4 - 2.4.0 -opcommand_grp 1.9.2 - 2.4.0 -opcommand_hst 1.9.2 - 2.4.0 -opconditions 1.5.3 - 2.4.0 -operations 1.3.4 - 2.4.0 -opgroup 1.9.2 - 2.4.0 +opcommand 1.9.4 - 3.0.0 +opcommand_grp 1.9.2 - 3.0.0 +opcommand_hst 1.9.2 - 3.0.0 +opconditions 1.5.3 - 3.0.0 +operations 1.3.4 - 3.0.0 +opgroup 1.9.2 - 3.0.0 opmediatypes 1.7 - 1.8.21 -opmessage 1.9.2 - 2.4.0 -opmessage_grp 1.9.2 - 2.4.0 -opmessage_usr 1.9.2 - 2.4.0 -optemplate 1.9.2 - 2.4.0 -profiles 1.3.1 - 2.4.0 -proxy_autoreg_host 1.7 - 2.4.0 -proxy_dhistory 1.5 - 2.4.0 -proxy_history 1.5.1 - 2.4.0 -regexps 1.7 - 2.4.0 -rights 1.3.1 - 2.4.0 -screens 1.3.1 - 2.4.0 -screens_items 1.3.1 - 2.4.0 -scripts 1.5 - 2.4.0 -service_alarms 1.3.1 - 2.4.0 -services 1.3.1 - 2.4.0 -services_links 1.3.1 - 2.4.0 -services_times 1.3.1 - 2.4.0 -sessions 1.3.1 - 2.4.0 -slides 1.3.4 - 2.4.0 -slideshows 1.3.4 - 2.4.0 -sysmap_element_url 1.9.0 - 2.4.0 -sysmap_url 1.9.0 - 2.4.0 -sysmaps 1.3.1 - 2.4.0 -sysmaps_elements 1.3.1 - 2.4.0 -sysmaps_link_triggers 1.5 - 2.4.0 -sysmaps_links 1.3.1 - 2.4.0 -timeperiods 1.7 - 2.4.0 -trends 1.3.1 - 2.4.0 DATA -trends_uint 1.5 - 2.4.0 DATA -trigger_depends 1.3.1 - 2.4.0 -trigger_discovery 1.9.0 - 2.4.0 -triggers 1.3.1 - 2.4.0 +opmessage 1.9.2 - 3.0.0 +opmessage_grp 1.9.2 - 3.0.0 +opmessage_usr 1.9.2 - 3.0.0 +optemplate 1.9.2 - 3.0.0 +profiles 1.3.1 - 3.0.0 +proxy_autoreg_host 1.7 - 3.0.0 +proxy_dhistory 1.5 - 3.0.0 +proxy_history 1.5.1 - 3.0.0 +regexps 1.7 - 3.0.0 +rights 1.3.1 - 3.0.0 +screens 1.3.1 - 3.0.0 +screens_items 1.3.1 - 3.0.0 +scripts 1.5 - 3.0.0 +service_alarms 1.3.1 - 3.0.0 +services 1.3.1 - 3.0.0 +services_links 1.3.1 - 3.0.0 +services_times 1.3.1 - 3.0.0 +sessions 1.3.1 - 3.0.0 +slides 1.3.4 - 3.0.0 +slideshows 1.3.4 - 3.0.0 +sysmap_element_url 1.9.0 - 3.0.0 +sysmap_url 1.9.0 - 3.0.0 +sysmaps 1.3.1 - 3.0.0 +sysmaps_elements 1.3.1 - 3.0.0 +sysmaps_link_triggers 1.5 - 3.0.0 +sysmaps_links 1.3.1 - 3.0.0 +timeperiods 1.7 - 3.0.0 +trends 1.3.1 - 3.0.0 DATA +trends_uint 1.5 - 3.0.0 DATA +trigger_depends 1.3.1 - 3.0.0 +trigger_discovery 1.9.0 - 3.0.0 +triggers 1.3.1 - 3.0.0 user_history 1.7 - 2.4.0 -users 1.3.1 - 2.4.0 -users_groups 1.3.1 - 2.4.0 -usrgrp 1.3.1 - 2.4.0 -valuemaps 1.3.1 - 2.4.0 +users 1.3.1 - 3.0.0 +users_groups 1.3.1 - 3.0.0 +usrgrp 1.3.1 - 3.0.0 +valuemaps 1.3.1 - 3.0.0 From 64f245ff1dbbfe1ffb7f03506d7a0ef4e4231800 Mon Sep 17 00:00:00 2001 From: ruslan-ohitin Date: Fri, 1 Jul 2016 16:52:27 +0600 Subject: [PATCH 043/122] Add Zabbix 3.0.x tables to the list --- zabbix-mysql-dump | 242 ++++++++++++++++++++++++---------------------- 1 file changed, 126 insertions(+), 116 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 3c5511e..197dc4d 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -343,119 +343,129 @@ exit 0 # __DATA__ -acknowledges 1.3.1 - 3.0.0 DATA -actions 1.3.1 - 3.0.0 -alerts 1.3.1 - 3.0.0 DATA -application_template 2.1.0 - 3.0.0 -applications 1.3.1 - 3.0.0 -auditlog 1.3.1 - 3.0.0 DATA -auditlog_details 1.7 - 3.0.0 DATA -autoreg 1.3.1 - 1.3.4 -autoreg_host 1.7 - 3.0.0 -conditions 1.3.1 - 3.0.0 -config 1.3.1 - 3.0.0 -dbversion 2.1.0 - 3.0.0 -dchecks 1.3.4 - 3.0.0 -dhosts 1.3.4 - 3.0.0 -drules 1.3.4 - 3.0.0 -dservices 1.3.4 - 3.0.0 -escalations 1.5.3 - 3.0.0 -events 1.3.1 - 3.0.0 DATA -expressions 1.7 - 3.0.0 -functions 1.3.1 - 3.0.0 -globalmacro 1.7 - 3.0.0 -globalvars 1.9.6 - 3.0.0 -graph_discovery 1.9.0 - 3.0.0 -graph_theme 1.7 - 3.0.0 -graphs 1.3.1 - 3.0.0 -graphs_items 1.3.1 - 3.0.0 -group_discovery 2.1.4 - 3.0.0 -group_prototype 2.1.4 - 3.0.0 -groups 1.3.1 - 3.0.0 -help_items 1.3.1 - 2.1.8 -history 1.3.1 - 3.0.0 DATA -history_log 1.3.1 - 3.0.0 DATA -history_str 1.3.1 - 3.0.0 DATA -history_str_sync 1.3.1 - 2.2.6 DATA -history_sync 1.3.1 - 2.2.6 DATA -history_text 1.3.1 - 3.0.0 DATA -history_uint 1.3.1 - 3.0.0 DATA -history_uint_sync 1.3.1 - 2.2.6 DATA -host_discovery 2.1.4 - 3.0.0 -host_inventory 1.9.6 - 3.0.0 -host_profile 1.9.3 - 1.9.5 -hostmacro 1.7 - 3.0.0 -hosts 1.3.1 - 3.0.0 -hosts_groups 1.3.1 - 3.0.0 -hosts_profiles 1.3.1 - 1.9.2 -hosts_profiles_ext 1.6 - 1.9.2 -hosts_templates 1.3.1 - 3.0.0 -housekeeper 1.3.1 - 3.0.0 -httpstep 1.3.3 - 3.0.0 -httpstepitem 1.3.3 - 3.0.0 -httptest 1.3.3 - 3.0.0 -httptestitem 1.3.3 - 3.0.0 -icon_map 1.9.6 - 3.0.0 -icon_mapping 1.9.6 - 3.0.0 -ids 1.3.3 - 3.0.0 -images 1.3.1 - 3.0.0 -interface 1.9.1 - 3.0.0 -interface_discovery 2.1.4 - 3.0.0 -item_condition 2.3.0 - 3.0.0 -item_discovery 1.9.0 - 3.0.0 -items 1.3.1 - 3.0.0 -items_applications 1.3.1 - 3.0.0 -maintenances 1.7 - 3.0.0 -maintenances_groups 1.7 - 3.0.0 -maintenances_hosts 1.7 - 3.0.0 -maintenances_windows 1.7 - 3.0.0 -mappings 1.3.1 - 3.0.0 -media 1.3.1 - 3.0.0 -media_type 1.3.1 - 3.0.0 -node_cksum 1.3.1 - 2.2.6 -node_configlog 1.3.1 - 1.4.7 -nodes 1.3.1 - 2.2.6 -opcommand 1.9.4 - 3.0.0 -opcommand_grp 1.9.2 - 3.0.0 -opcommand_hst 1.9.2 - 3.0.0 -opconditions 1.5.3 - 3.0.0 -operations 1.3.4 - 3.0.0 -opgroup 1.9.2 - 3.0.0 -opmediatypes 1.7 - 1.8.21 -opmessage 1.9.2 - 3.0.0 -opmessage_grp 1.9.2 - 3.0.0 -opmessage_usr 1.9.2 - 3.0.0 -optemplate 1.9.2 - 3.0.0 -profiles 1.3.1 - 3.0.0 -proxy_autoreg_host 1.7 - 3.0.0 -proxy_dhistory 1.5 - 3.0.0 -proxy_history 1.5.1 - 3.0.0 -regexps 1.7 - 3.0.0 -rights 1.3.1 - 3.0.0 -screens 1.3.1 - 3.0.0 -screens_items 1.3.1 - 3.0.0 -scripts 1.5 - 3.0.0 -service_alarms 1.3.1 - 3.0.0 -services 1.3.1 - 3.0.0 -services_links 1.3.1 - 3.0.0 -services_times 1.3.1 - 3.0.0 -sessions 1.3.1 - 3.0.0 -slides 1.3.4 - 3.0.0 -slideshows 1.3.4 - 3.0.0 -sysmap_element_url 1.9.0 - 3.0.0 -sysmap_url 1.9.0 - 3.0.0 -sysmaps 1.3.1 - 3.0.0 -sysmaps_elements 1.3.1 - 3.0.0 -sysmaps_link_triggers 1.5 - 3.0.0 -sysmaps_links 1.3.1 - 3.0.0 -timeperiods 1.7 - 3.0.0 -trends 1.3.1 - 3.0.0 DATA -trends_uint 1.5 - 3.0.0 DATA -trigger_depends 1.3.1 - 3.0.0 -trigger_discovery 1.9.0 - 3.0.0 -triggers 1.3.1 - 3.0.0 -user_history 1.7 - 2.4.0 -users 1.3.1 - 3.0.0 -users_groups 1.3.1 - 3.0.0 -usrgrp 1.3.1 - 3.0.0 -valuemaps 1.3.1 - 3.0.0 +acknowledges 1.3.1 - 3.0.0 DATA +actions 1.3.1 - 3.0.0 +alerts 1.3.1 - 3.0.0 DATA +application_discovery 3.0.0 - 3.0.0 +application_prototype 3.0.0 - 3.0.0 +application_template 2.1.0 - 3.0.0 +applications 1.3.1 - 3.0.0 +auditlog 1.3.1 - 3.0.0 DATA +auditlog_details 1.7 - 3.0.0 DATA +autoreg 1.3.1 - 1.3.4 +autoreg_host 1.7 - 3.0.0 +conditions 1.3.1 - 3.0.0 +config 1.3.1 - 3.0.0 +dbversion 2.1.0 - 3.0.0 +dchecks 1.3.4 - 3.0.0 +dhosts 1.3.4 - 3.0.0 +drules 1.3.4 - 3.0.0 +dservices 1.3.4 - 3.0.0 +escalations 1.5.3 - 3.0.0 +events 1.3.1 - 3.0.0 DATA +expressions 1.7 - 3.0.0 +functions 1.3.1 - 3.0.0 +globalmacro 1.7 - 3.0.0 +globalvars 1.9.6 - 3.0.0 +graph_discovery 1.9.0 - 3.0.0 +graph_theme 1.7 - 3.0.0 +graphs 1.3.1 - 3.0.0 +graphs_items 1.3.1 - 3.0.0 +group_discovery 2.1.4 - 3.0.0 +group_prototype 2.1.4 - 3.0.0 +groups 1.3.1 - 3.0.0 +help_items 1.3.1 - 2.1.8 +history 1.3.1 - 3.0.0 DATA +history_log 1.3.1 - 3.0.0 DATA +history_str 1.3.1 - 3.0.0 DATA +history_str_sync 1.3.1 - 2.2.6 DATA +history_sync 1.3.1 - 2.2.6 DATA +history_text 1.3.1 - 3.0.0 DATA +history_uint 1.3.1 - 3.0.0 DATA +history_uint_sync 1.3.1 - 2.2.6 DATA +host_discovery 2.1.4 - 3.0.0 +host_inventory 1.9.6 - 3.0.0 +host_profile 1.9.3 - 1.9.5 +hostmacro 1.7 - 3.0.0 +hosts 1.3.1 - 3.0.0 +hosts_groups 1.3.1 - 3.0.0 +hosts_profiles 1.3.1 - 1.9.2 +hosts_profiles_ext 1.6 - 1.9.2 +hosts_templates 1.3.1 - 3.0.0 +housekeeper 1.3.1 - 3.0.0 +httpstep 1.3.3 - 3.0.0 +httpstepitem 1.3.3 - 3.0.0 +httptest 1.3.3 - 3.0.0 +httptestitem 1.3.3 - 3.0.0 +icon_map 1.9.6 - 3.0.0 +icon_mapping 1.9.6 - 3.0.0 +ids 1.3.3 - 3.0.0 +images 1.3.1 - 3.0.0 +interface 1.9.1 - 3.0.0 +interface_discovery 2.1.4 - 3.0.0 +item_application_prototype 3.0.0 - 3.0.0 +item_condition 2.3.0 - 3.0.0 +item_discovery 1.9.0 - 3.0.0 +items 1.3.1 - 3.0.0 +items_applications 1.3.1 - 3.0.0 +maintenances 1.7 - 3.0.0 +maintenances_groups 1.7 - 3.0.0 +maintenances_hosts 1.7 - 3.0.0 +maintenances_windows 1.7 - 3.0.0 +mappings 1.3.1 - 3.0.0 +media 1.3.1 - 3.0.0 +media_type 1.3.1 - 3.0.0 +node_cksum 1.3.1 - 2.2.6 +node_configlog 1.3.1 - 1.4.7 +nodes 1.3.1 - 2.2.6 +opcommand 1.9.4 - 3.0.0 +opcommand_grp 1.9.2 - 3.0.0 +opcommand_hst 1.9.2 - 3.0.0 +opconditions 1.5.3 - 3.0.0 +operations 1.3.4 - 3.0.0 +opgroup 1.9.2 - 3.0.0 +opmediatypes 1.7 - 1.8.21 +opinventory 3.0.0 - 3.0.0 +opmessage 1.9.2 - 3.0.0 +opmessage_grp 1.9.2 - 3.0.0 +opmessage_usr 1.9.2 - 3.0.0 +optemplate 1.9.2 - 3.0.0 +profiles 1.3.1 - 3.0.0 +proxy_autoreg_host 1.7 - 3.0.0 +proxy_dhistory 1.5 - 3.0.0 +proxy_history 1.5.1 - 3.0.0 +regexps 1.7 - 3.0.0 +rights 1.3.1 - 3.0.0 +screen_user 3.0.0 - 3.0.0 +screen_usrgrp 3.0.0 - 3.0.0 +screens 1.3.1 - 3.0.0 +screens_items 1.3.1 - 3.0.0 +scripts 1.5 - 3.0.0 +service_alarms 1.3.1 - 3.0.0 +services 1.3.1 - 3.0.0 +services_links 1.3.1 - 3.0.0 +services_times 1.3.1 - 3.0.0 +sessions 1.3.1 - 3.0.0 +slides 1.3.4 - 3.0.0 +slideshow_user 3.0.0 - 3.0.0 +slideshow_usrgrp 3.0.0 - 3.0.0 +slideshows 1.3.4 - 3.0.0 +sysmap_element_url 1.9.0 - 3.0.0 +sysmap_url 1.9.0 - 3.0.0 +sysmap_user 3.0.0 - 3.0.0 +sysmap_usrgrp 3.0.0 - 3.0.0 +sysmaps 1.3.1 - 3.0.0 +sysmaps_elements 1.3.1 - 3.0.0 +sysmaps_link_triggers 1.5 - 3.0.0 +sysmaps_links 1.3.1 - 3.0.0 +timeperiods 1.7 - 3.0.0 +trends 1.3.1 - 3.0.0 DATA +trends_uint 1.5 - 3.0.0 DATA +trigger_depends 1.3.1 - 3.0.0 +trigger_discovery 1.9.0 - 3.0.0 +triggers 1.3.1 - 3.0.0 +user_history 1.7 - 2.4.0 +users 1.3.1 - 3.0.0 +users_groups 1.3.1 - 3.0.0 +usrgrp 1.3.1 - 3.0.0 +valuemaps 1.3.1 - 3.0.0 From 8acf5791b4fc5fcbe316bd0895b88b00a75d8cf4 Mon Sep 17 00:00:00 2001 From: ruslan-ohitin Date: Fri, 1 Jul 2016 16:53:19 +0600 Subject: [PATCH 044/122] Change version in README --- README | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README b/README index e8e7707..8f14b61 100644 --- a/README +++ b/README @@ -1,3 +1,3 @@ -This is a MySQL database backup script for all Zabbix versions from 1.3.1 up to 2.4.0. +This is a MySQL database backup script for all Zabbix versions from 1.3.1 up to 3.0.0. -For more informations, see the Wiki (https://github.com/maxhq/zabbix-backup/wiki). \ No newline at end of file +For more informations, see the Wiki (https://github.com/maxhq/zabbix-backup/wiki). From 123bc778ae6b5a8f18820625a67701e6e083b965 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Mon, 11 Jul 2016 12:16:15 +0200 Subject: [PATCH 045/122] CHG Updated table versions (3.0.3 and also 2.x versions) --- zabbix-mysql-dump | 244 +++++++++++++++++++++++----------------------- 1 file changed, 124 insertions(+), 120 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 197dc4d..019f2e5 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -20,8 +20,12 @@ # - Andreas Niedermann (dre-) # - Mișu Moldovan (dumol) # - Daniel Schneller (dschneller) +# - Ruslan Ohitin (ruslan-ohitin) # # HISTORY +# 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) @@ -343,129 +347,129 @@ exit 0 # __DATA__ -acknowledges 1.3.1 - 3.0.0 DATA -actions 1.3.1 - 3.0.0 -alerts 1.3.1 - 3.0.0 DATA -application_discovery 3.0.0 - 3.0.0 -application_prototype 3.0.0 - 3.0.0 -application_template 2.1.0 - 3.0.0 -applications 1.3.1 - 3.0.0 -auditlog 1.3.1 - 3.0.0 DATA -auditlog_details 1.7 - 3.0.0 DATA +acknowledges 1.3.1 - 3.0.3 DATA +actions 1.3.1 - 3.0.3 +alerts 1.3.1 - 3.0.3 DATA +application_discovery 2.5.0 - 3.0.3 +application_prototype 2.5.0 - 3.0.3 +application_template 2.1.0 - 3.0.3 +applications 1.3.1 - 3.0.3 +auditlog 1.3.1 - 3.0.3 DATA +auditlog_details 1.7 - 3.0.3 DATA autoreg 1.3.1 - 1.3.4 -autoreg_host 1.7 - 3.0.0 -conditions 1.3.1 - 3.0.0 -config 1.3.1 - 3.0.0 -dbversion 2.1.0 - 3.0.0 -dchecks 1.3.4 - 3.0.0 -dhosts 1.3.4 - 3.0.0 -drules 1.3.4 - 3.0.0 -dservices 1.3.4 - 3.0.0 -escalations 1.5.3 - 3.0.0 -events 1.3.1 - 3.0.0 DATA -expressions 1.7 - 3.0.0 -functions 1.3.1 - 3.0.0 -globalmacro 1.7 - 3.0.0 -globalvars 1.9.6 - 3.0.0 -graph_discovery 1.9.0 - 3.0.0 -graph_theme 1.7 - 3.0.0 -graphs 1.3.1 - 3.0.0 -graphs_items 1.3.1 - 3.0.0 -group_discovery 2.1.4 - 3.0.0 -group_prototype 2.1.4 - 3.0.0 -groups 1.3.1 - 3.0.0 +autoreg_host 1.7 - 3.0.3 +conditions 1.3.1 - 3.0.3 +config 1.3.1 - 3.0.3 +dbversion 2.1.0 - 3.0.3 +dchecks 1.3.4 - 3.0.3 +dhosts 1.3.4 - 3.0.3 +drules 1.3.4 - 3.0.3 +dservices 1.3.4 - 3.0.3 +escalations 1.5.3 - 3.0.3 +events 1.3.1 - 3.0.3 DATA +expressions 1.7 - 3.0.3 +functions 1.3.1 - 3.0.3 +globalmacro 1.7 - 3.0.3 +globalvars 1.9.6 - 3.0.3 +graph_discovery 1.9.0 - 3.0.3 +graph_theme 1.7 - 3.0.3 +graphs 1.3.1 - 3.0.3 +graphs_items 1.3.1 - 3.0.3 +group_discovery 2.1.4 - 3.0.3 +group_prototype 2.1.4 - 3.0.3 +groups 1.3.1 - 3.0.3 help_items 1.3.1 - 2.1.8 -history 1.3.1 - 3.0.0 DATA -history_log 1.3.1 - 3.0.0 DATA -history_str 1.3.1 - 3.0.0 DATA -history_str_sync 1.3.1 - 2.2.6 DATA -history_sync 1.3.1 - 2.2.6 DATA -history_text 1.3.1 - 3.0.0 DATA -history_uint 1.3.1 - 3.0.0 DATA -history_uint_sync 1.3.1 - 2.2.6 DATA -host_discovery 2.1.4 - 3.0.0 -host_inventory 1.9.6 - 3.0.0 +history 1.3.1 - 3.0.3 DATA +history_log 1.3.1 - 3.0.3 DATA +history_str 1.3.1 - 3.0.3 DATA +history_str_sync 1.3.1 - 2.2.13 DATA +history_sync 1.3.1 - 2.2.13 DATA +history_text 1.3.1 - 3.0.3 DATA +history_uint 1.3.1 - 3.0.3 DATA +history_uint_sync 1.3.1 - 2.2.13 DATA +host_discovery 2.1.4 - 3.0.3 +host_inventory 1.9.6 - 3.0.3 host_profile 1.9.3 - 1.9.5 -hostmacro 1.7 - 3.0.0 -hosts 1.3.1 - 3.0.0 -hosts_groups 1.3.1 - 3.0.0 +hostmacro 1.7 - 3.0.3 +hosts 1.3.1 - 3.0.3 +hosts_groups 1.3.1 - 3.0.3 hosts_profiles 1.3.1 - 1.9.2 hosts_profiles_ext 1.6 - 1.9.2 -hosts_templates 1.3.1 - 3.0.0 -housekeeper 1.3.1 - 3.0.0 -httpstep 1.3.3 - 3.0.0 -httpstepitem 1.3.3 - 3.0.0 -httptest 1.3.3 - 3.0.0 -httptestitem 1.3.3 - 3.0.0 -icon_map 1.9.6 - 3.0.0 -icon_mapping 1.9.6 - 3.0.0 -ids 1.3.3 - 3.0.0 -images 1.3.1 - 3.0.0 -interface 1.9.1 - 3.0.0 -interface_discovery 2.1.4 - 3.0.0 -item_application_prototype 3.0.0 - 3.0.0 -item_condition 2.3.0 - 3.0.0 -item_discovery 1.9.0 - 3.0.0 -items 1.3.1 - 3.0.0 -items_applications 1.3.1 - 3.0.0 -maintenances 1.7 - 3.0.0 -maintenances_groups 1.7 - 3.0.0 -maintenances_hosts 1.7 - 3.0.0 -maintenances_windows 1.7 - 3.0.0 -mappings 1.3.1 - 3.0.0 -media 1.3.1 - 3.0.0 -media_type 1.3.1 - 3.0.0 -node_cksum 1.3.1 - 2.2.6 +hosts_templates 1.3.1 - 3.0.3 +housekeeper 1.3.1 - 3.0.3 +httpstep 1.3.3 - 3.0.3 +httpstepitem 1.3.3 - 3.0.3 +httptest 1.3.3 - 3.0.3 +httptestitem 1.3.3 - 3.0.3 +icon_map 1.9.6 - 3.0.3 +icon_mapping 1.9.6 - 3.0.3 +ids 1.3.3 - 3.0.3 +images 1.3.1 - 3.0.3 +interface 1.9.1 - 3.0.3 +interface_discovery 2.1.4 - 3.0.3 +item_application_prototype 2.5.0 - 3.0.3 +item_condition 2.3.0 - 3.0.3 +item_discovery 1.9.0 - 3.0.3 +items 1.3.1 - 3.0.3 +items_applications 1.3.1 - 3.0.3 +maintenances 1.7 - 3.0.3 +maintenances_groups 1.7 - 3.0.3 +maintenances_hosts 1.7 - 3.0.3 +maintenances_windows 1.7 - 3.0.3 +mappings 1.3.1 - 3.0.3 +media 1.3.1 - 3.0.3 +media_type 1.3.1 - 3.0.3 +node_cksum 1.3.1 - 2.2.13 node_configlog 1.3.1 - 1.4.7 -nodes 1.3.1 - 2.2.6 -opcommand 1.9.4 - 3.0.0 -opcommand_grp 1.9.2 - 3.0.0 -opcommand_hst 1.9.2 - 3.0.0 -opconditions 1.5.3 - 3.0.0 -operations 1.3.4 - 3.0.0 -opgroup 1.9.2 - 3.0.0 -opmediatypes 1.7 - 1.8.21 -opinventory 3.0.0 - 3.0.0 -opmessage 1.9.2 - 3.0.0 -opmessage_grp 1.9.2 - 3.0.0 -opmessage_usr 1.9.2 - 3.0.0 -optemplate 1.9.2 - 3.0.0 -profiles 1.3.1 - 3.0.0 -proxy_autoreg_host 1.7 - 3.0.0 -proxy_dhistory 1.5 - 3.0.0 -proxy_history 1.5.1 - 3.0.0 -regexps 1.7 - 3.0.0 -rights 1.3.1 - 3.0.0 -screen_user 3.0.0 - 3.0.0 -screen_usrgrp 3.0.0 - 3.0.0 -screens 1.3.1 - 3.0.0 -screens_items 1.3.1 - 3.0.0 -scripts 1.5 - 3.0.0 -service_alarms 1.3.1 - 3.0.0 -services 1.3.1 - 3.0.0 -services_links 1.3.1 - 3.0.0 -services_times 1.3.1 - 3.0.0 -sessions 1.3.1 - 3.0.0 -slides 1.3.4 - 3.0.0 -slideshow_user 3.0.0 - 3.0.0 -slideshow_usrgrp 3.0.0 - 3.0.0 -slideshows 1.3.4 - 3.0.0 -sysmap_element_url 1.9.0 - 3.0.0 -sysmap_url 1.9.0 - 3.0.0 -sysmap_user 3.0.0 - 3.0.0 -sysmap_usrgrp 3.0.0 - 3.0.0 -sysmaps 1.3.1 - 3.0.0 -sysmaps_elements 1.3.1 - 3.0.0 -sysmaps_link_triggers 1.5 - 3.0.0 -sysmaps_links 1.3.1 - 3.0.0 -timeperiods 1.7 - 3.0.0 -trends 1.3.1 - 3.0.0 DATA -trends_uint 1.5 - 3.0.0 DATA -trigger_depends 1.3.1 - 3.0.0 -trigger_discovery 1.9.0 - 3.0.0 -triggers 1.3.1 - 3.0.0 -user_history 1.7 - 2.4.0 -users 1.3.1 - 3.0.0 -users_groups 1.3.1 - 3.0.0 -usrgrp 1.3.1 - 3.0.0 -valuemaps 1.3.1 - 3.0.0 +nodes 1.3.1 - 2.2.13 +opcommand 1.9.4 - 3.0.3 +opcommand_grp 1.9.2 - 3.0.3 +opcommand_hst 1.9.2 - 3.0.3 +opconditions 1.5.3 - 3.0.3 +operations 1.3.4 - 3.0.3 +opgroup 1.9.2 - 3.0.3 +opinventory 3.0.0 - 3.0.3 +opmediatypes 1.7 - 1.8.22 +opmessage 1.9.2 - 3.0.3 +opmessage_grp 1.9.2 - 3.0.3 +opmessage_usr 1.9.2 - 3.0.3 +optemplate 1.9.2 - 3.0.3 +profiles 1.3.1 - 3.0.3 +proxy_autoreg_host 1.7 - 3.0.3 +proxy_dhistory 1.5 - 3.0.3 +proxy_history 1.5.1 - 3.0.3 +regexps 1.7 - 3.0.3 +rights 1.3.1 - 3.0.3 +screen_user 3.0.0 - 3.0.3 +screen_usrgrp 3.0.0 - 3.0.3 +screens 1.3.1 - 3.0.3 +screens_items 1.3.1 - 3.0.3 +scripts 1.5 - 3.0.3 +service_alarms 1.3.1 - 3.0.3 +services 1.3.1 - 3.0.3 +services_links 1.3.1 - 3.0.3 +services_times 1.3.1 - 3.0.3 +sessions 1.3.1 - 3.0.3 +slides 1.3.4 - 3.0.3 +slideshow_user 3.0.0 - 3.0.3 +slideshow_usrgrp 3.0.0 - 3.0.3 +slideshows 1.3.4 - 3.0.3 +sysmap_element_url 1.9.0 - 3.0.3 +sysmap_url 1.9.0 - 3.0.3 +sysmap_user 3.0.0 - 3.0.3 +sysmap_usrgrp 3.0.0 - 3.0.3 +sysmaps 1.3.1 - 3.0.3 +sysmaps_elements 1.3.1 - 3.0.3 +sysmaps_link_triggers 1.5 - 3.0.3 +sysmaps_links 1.3.1 - 3.0.3 +timeperiods 1.7 - 3.0.3 +trends 1.3.1 - 3.0.3 DATA +trends_uint 1.5 - 3.0.3 DATA +trigger_depends 1.3.1 - 3.0.3 +trigger_discovery 1.9.0 - 3.0.3 +triggers 1.3.1 - 3.0.3 +user_history 1.7 - 2.4.8 +users 1.3.1 - 3.0.3 +users_groups 1.3.1 - 3.0.3 +usrgrp 1.3.1 - 3.0.3 +valuemaps 1.3.1 - 3.0.3 From a8e71ab394794eb4484de282fb824b1b14921019 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Mon, 11 Jul 2016 12:22:03 +0200 Subject: [PATCH 046/122] DOC Set latest version to 3.0.3 --- README | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README b/README index 8f14b61..3c1891a 100644 --- a/README +++ b/README @@ -1,3 +1,3 @@ -This is a MySQL database backup script for all Zabbix versions from 1.3.1 up to 3.0.0. +This is a MySQL database backup script for all Zabbix versions from 1.3.1 up to 3.0.3. For more informations, see the Wiki (https://github.com/maxhq/zabbix-backup/wiki). From dd09ae2717a9e5246de382c6a0bc2c8a6e6666b0 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Mon, 11 Jul 2016 12:38:59 +0200 Subject: [PATCH 047/122] DOC How to release a new version --- RELEASE.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 RELEASE.md diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000..3f76245 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,11 @@ +## Releasing a new version + +### Before commit + +* Update history in script itself +* Update README + +### After commit + +* Add release at https://github.com/maxhq/zabbix-backup/releases +* Update https://github.com/maxhq/zabbix-backup/wiki From 97b4075ed4b199addec1e510ec2bf94c71d4f22a Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Mon, 11 Jul 2016 12:40:46 +0200 Subject: [PATCH 048/122] DOC Added link to Zabbix.org page --- RELEASE.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE.md b/RELEASE.md index 3f76245..5d935a7 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -9,3 +9,4 @@ * Add release at https://github.com/maxhq/zabbix-backup/releases * Update https://github.com/maxhq/zabbix-backup/wiki +* Update http://zabbix.org/wiki/Docs/howto/mysql_backup_script From 9273643bf06f132bf68905556dfddbdcf31296c1 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Thu, 28 Jul 2016 13:15:30 -0500 Subject: [PATCH 049/122] Add -x flag for xz support --- zabbix-mysql-dump | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 019f2e5..cdee7ba 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -25,7 +25,7 @@ # HISTORY # 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) @@ -39,7 +39,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 # @@ -67,7 +67,7 @@ # # DEFAULT VALUES -# +# # DO NOT EDIT THESE VALUES! # Instead, use command line parameters or a config file to specify options. # @@ -76,6 +76,7 @@ DBHOST="127.0.0.1" DBNAME="zabbix" DBUSER="zabbix" DBPASS="" +XZ="no" QUIET="no" REVERSELOOKUP="yes" GENERATIONSTOKEEP=0 @@ -87,7 +88,7 @@ if [ -z "$1" ]; then cat <&2; exit 1 ;; @@ -235,7 +244,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 @@ -306,7 +315,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 @@ -315,17 +324,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 From 5d77734153a3aa7c53c0817bafe68c35b98b33ce Mon Sep 17 00:00:00 2001 From: Jonathan Date: Thu, 28 Jul 2016 13:15:30 -0500 Subject: [PATCH 050/122] NEW: Option -x to use XZ instead of GZ for compression --- zabbix-mysql-dump | 49 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 019f2e5..1c82ab7 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -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) @@ -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 # @@ -67,7 +69,7 @@ # # DEFAULT VALUES -# +# # DO NOT EDIT THESE VALUES! # Instead, use command line parameters or a config file to specify options. # @@ -76,6 +78,7 @@ DBHOST="127.0.0.1" DBNAME="zabbix" DBUSER="zabbix" DBPASS="" +XZ="no" QUIET="no" REVERSELOOKUP="yes" GENERATIONSTOKEEP=0 @@ -87,7 +90,7 @@ if [ -z "$1" ]; then cat <&2; exit 1 ;; @@ -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 @@ -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 @@ -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 From c0b8b26895b3161bbd3fdf5105a8081ec641163c Mon Sep 17 00:00:00 2001 From: Marthinus Meyer Date: Wed, 7 Sep 2016 18:05:44 -0400 Subject: [PATCH 051/122] FIX warning: here-document at line 206 delimited by end-of-file (wanted 'EOF') --- zabbix-mysql-dump | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 019f2e5..91d1a3a 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -210,7 +210,7 @@ if [ "$QUIET" == "no" ]; then - user: $DBUSER - output: $DUMPDIR - EOF +EOF fi # From 4659367887cbf92e0b9f9d8488da100e46c4f6f6 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 9 Sep 2016 00:05:23 +0200 Subject: [PATCH 052/122] REV Renamed readme file and moved history into readme --- README | 3 --- README.md | 56 +++++++++++++++++++++++++++++++++++++++++++++++ RELEASE.md | 3 +-- zabbix-mysql-dump | 53 +++++++++----------------------------------- 4 files changed, 67 insertions(+), 48 deletions(-) delete mode 100644 README create mode 100644 README.md diff --git a/README b/README deleted file mode 100644 index 3c1891a..0000000 --- a/README +++ /dev/null @@ -1,3 +0,0 @@ -This is a MySQL database backup script for all Zabbix versions from 1.3.1 up to 3.0.3. - -For more informations, see the Wiki (https://github.com/maxhq/zabbix-backup/wiki). diff --git a/README.md b/README.md new file mode 100644 index 0000000..00a2a5d --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +# About this script + +## Short description + +This is a MySQL database backup script for all Zabbix versions from 1.3.1 up to 3.0.3. + +For more informations, see the Wiki (https://github.com/maxhq/zabbix-backup/wiki). + +## Version history + +**0.8.2 (2016-09-08)** + +- FIX: Evil space was masking end of here-document (fixed in #8 by @msjmeyer) + +**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) +- ENH: Option -r to rotate backup files (Daniel Schneller) +- ENH: Add database version to filename if available +- ENH: Add quiet mode. IP reverse lookup optional (Daniel Schneller) +- ENH: Bash related fixes (Misu Moldovan) +- CHG: Default output directory is now $PWD instead of script dir + +**0.7.1 (2015-01-27)** + +- 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 + +**0.6.0 (2014-09-15)** + +- REV: Updated the table list for use with zabbix v2.2.3 + +**0.5.0 (2013-05-13)** + +- NEW: Added table list comparison between database and script + +**0.4.0 (2012-03-02)** + +- REV: Incorporated mysqldump options (suggested by Jonathan Bayer) + +**0.3.0 (2012-02-06)** + +- ENH: Backup of Zabbix 1.9.x / 2.0.0, removed unnecessary use of + variables (DATEBIN etc) for commands that use to be in $PATH + +**0.2.0 (2011-11-05)** diff --git a/RELEASE.md b/RELEASE.md index 5d935a7..c74d192 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -2,8 +2,7 @@ ### Before commit -* Update history in script itself -* Update README +* Update README.md ### After commit diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 91d1a3a..11d07e8 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -3,6 +3,9 @@ # NAME # zabbix-mysql-dump - Configuration Backup for Zabbix with MySQL # +# VERSION +# 0.8.2 +# # SYNOPSIS # This is a MySQL configuration backup script for Zabbix 1.x, 2.x and 3.0.x. # It does a full backup of all configuration tables, but only a schema @@ -22,42 +25,6 @@ # - Daniel Schneller (dschneller) # - Ruslan Ohitin (ruslan-ohitin) # -# HISTORY -# 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) -# ENH: Option -r to rotate backup files (Daniel Schneller) -# ENH: Add database version to filename if available -# ENH: Add quiet mode. IP reverse lookup optional (Daniel Schneller) -# ENH: Bash related fixes (Misu Moldovan) -# CHG: Default output directory is now $PWD instead of script dir -# -# 0.7.1 (2015-01-27) -# 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 -# -# 0.6.0 (2014-09-15) -# REV: Updated the table list for use with zabbix v2.2.3 -# -# 0.5.0 (2013-05-13) -# NEW: Added table list comparison between database and script -# -# 0.4.0 (2012-03-02) -# REV: Incorporated mysqldump options (suggested by Jonathan Bayer) -# -# 0.3.0 (2012-02-06) -# ENH: Backup of Zabbix 1.9.x / 2.0.0, removed unnecessary use of -# variables (DATEBIN etc) for commands that use to be in $PATH -# -# 0.2.0 (2011-11-05) -# # AUTHOR # Jens Berthold (maxhq), 2016 # @@ -67,7 +34,7 @@ # # DEFAULT VALUES -# +# # DO NOT EDIT THESE VALUES! # Instead, use command line parameters or a config file to specify options. # @@ -87,7 +54,7 @@ if [ -z "$1" ]; then cat < Date: Fri, 9 Sep 2016 00:16:47 +0200 Subject: [PATCH 053/122] FIX Prevent "Warning: Using a password on the command line interface can be insecure." --- zabbix-mysql-dump | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 11d07e8..2b6a169 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -156,6 +156,9 @@ MYSQL_OPTS=() MYSQL_OPTS_BATCH=("${MYSQL_OPTS[@]}" --batch --silent) [ ! -z "$DBNAME" ] && MYSQL_OPTS_BATCH=("${MYSQL_OPTS_BATCH[@]}" -D $DBNAME) +# Log file for errors +ERRORLOG=$(mktemp) + # Host name: try reverse lookup if IP is given DBHOSTNAME="$DBHOST" command -v dig >/dev/null 2>&1 @@ -222,8 +225,8 @@ fi # # Read table list from database [ "$QUIET" == "no" ] && echo "Fetching list of existing tables..." -DB_TABLES=$(mysql "${MYSQL_OPTS_BATCH[@]}" -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" 2>&1) -if [ $? -ne 0 ]; then echo -e "ERROR while trying to access database:\n$DB_TABLES" 2>&1; exit 1; fi +DB_TABLES=$(mysql "${MYSQL_OPTS_BATCH[@]}" -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" 2>$ERRORLOG) +if [ $? -ne 0 ]; then echo "ERROR while trying to access database:" 2>&1; cat $ERRORLOG 2>&1; exit 1; fi DB_TABLES=$(echo "$DB_TABLES" | sort) DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) @@ -262,7 +265,9 @@ while read table; do mysqldump "${MYSQL_OPTS[@]}" \ --routines --opt --single-transaction --skip-lock-tables \ $dump_opt \ - $DBNAME --tables ${table} >> "$DUMPFILE" + $DBNAME --tables ${table} >> "$DUMPFILE" 2>$ERRORLOG + + if [ $? -ne 0 ]; then echo -e "\nERROR: Could not backup table ${table}:" >&2; cat $ERRORLOG >&2; exit 1; fi if [ "$QUIET" == "no" ]; then # show percentage @@ -275,6 +280,8 @@ while read table; do fi done <<<"$DB_TABLES" +rm $ERRORLOG + # # COMPRESS BACKUP # From 0f5485c5c0b61967b523fd0238cfb2a065357956 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 9 Sep 2016 00:44:53 +0200 Subject: [PATCH 054/122] ENH Added option -0 for "no compression" and revised code for xz compression --- zabbix-mysql-dump | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 2e04710..381a5d1 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -43,7 +43,7 @@ DBHOST="127.0.0.1" DBNAME="zabbix" DBUSER="zabbix" DBPASS="" -XZ="no" +COMPRESSION="gz" QUIET="no" REVERSELOOKUP="yes" GENERATIONSTOKEEP=0 @@ -96,6 +96,9 @@ OPTIONS but the resulting backup will be about half the size of the same sql file compressed using gz. Your mileage may vary. + -0 + Do not compress the sql dump + -n Skip reverse lookup of IP address for host. @@ -116,7 +119,7 @@ fi # PARSE COMMAND LINE ARGUMENTS # DB_GIVEN=0 -while getopts ":h:d:u:p:o:r:c:xqn" opt; do +while getopts ":h:d:u:p:o:r:c:x0qn" opt; do case $opt in h) DBHOST="$OPTARG" ;; d) DBNAME="$OPTARG"; DB_GIVEN=1 ;; @@ -125,7 +128,8 @@ while getopts ":h:d:u:p:o:r:c:xqn" opt; do c) CNFFILE="$OPTARG" ;; o) DUMPDIR="$OPTARG" ;; r) GENERATIONSTOKEEP=$(printf '%.0f' "$OPTARG") ;; - x) XZ="yes" ;; + x) COMPRESSION="xz" ;; + 0) COMPRESSION="" ;; n) REVERSELOOKUP="no" ;; q) QUIET="yes" ;; \?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;; @@ -156,6 +160,8 @@ fi # # CONSTANTS # +SUFFIX=""; test ! -z $COMPRESSION && SUFFIX=".${COMPRESSION}" + MYSQL_OPTS=() [ ! -z "$CNFFILE" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" --defaults-extra-file="$CNFFILE") [ ! -z "$DBHOST" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" -h $DBHOST) @@ -302,23 +308,16 @@ if [ "$QUIET" == "no" ]; then echo echo "Compressing backup file..." fi -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 + +EXITCODE=0 +if [ "$COMPRESSION" == "gz" ]; then gzip -f "$DUMPFILE"; EXITCODE=$?; fi +if [ "$COMPRESSION" == "xz" ]; then xz -f "$DUMPFILE"; EXITCODE=$?; fi +if [ $EXITCODE -ne 0 ]; then + echo -e "\nERROR: Could not compress backup file, see previous messages" >&2 exit 1 - fi fi -[ "$QUIET" == "no" ] && [ "$XZ" == "no" ] && echo -e "\nBackup Completed:\n${DUMPFILE}.gz" -[ "$QUIET" == "no" ] && [ "$XZ" == "yes" ] && echo -e "\nBackup Completed:\n${DUMPFILE}.xz" +[ "$QUIET" == "no" ] && echo -e "\nBackup Completed:\n${DUMPFILE}${SUFFIX}" # # ROTATE OLD BACKUPS From a4852844037aed4a7f207077816dd05e6f5e9550 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 9 Sep 2016 00:56:00 +0200 Subject: [PATCH 055/122] DOC Updated readme with changes in 0.8.2 --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 00a2a5d..3241d90 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,10 @@ For more informations, see the Wiki (https://github.com/maxhq/zabbix-backup/wiki **0.8.2 (2016-09-08)** +- NEW: Option -x to use XZ instead of GZ for compression +- NEW: Option -0 for "no compression" - FIX: Evil space was masking end of here-document (fixed in #8 by @msjmeyer) +- FIX: Prevent "Warning: Using a password on the command line interface can be insecure." **0.8.1 (2016-07-11)** From cc01e7b5db18c108870ffdc30de85c5f792e0ab2 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 9 Sep 2016 01:15:32 +0200 Subject: [PATCH 056/122] DOC Added "Jonathan Wright" as contributor --- README.md | 2 +- zabbix-mysql-dump | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3241d90..a4a6678 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ For more informations, see the Wiki (https://github.com/maxhq/zabbix-backup/wiki **0.8.2 (2016-09-08)** -- NEW: Option -x to use XZ instead of GZ for compression +- NEW: Option -x to use XZ instead of GZ for compression (Jonathan Wright) - NEW: Option -0 for "no compression" - FIX: Evil space was masking end of here-document (fixed in #8 by @msjmeyer) - FIX: Prevent "Warning: Using a password on the command line interface can be insecure." diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 381a5d1..a61d727 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -24,6 +24,8 @@ # - Mișu Moldovan (dumol) # - Daniel Schneller (dschneller) # - Ruslan Ohitin (ruslan-ohitin) +# - Jonathan Wright (neonardo1) +# - msjmeyer # # AUTHOR # Jens Berthold (maxhq), 2016 From 22f7edae75042d6ed9738d2278fcc3f0f8caff0b Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 9 Sep 2016 01:39:19 +0200 Subject: [PATCH 057/122] DOC Changed some wording in readme --- README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a4a6678..cb0e350 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ -# About this script +# Zabbix MySQL backup -## Short description +This is a MySQL database backup script for the [Zabbix](http://www.zabbix.com/) monitoring software from version 1.3.1 up to 3.0.3. -This is a MySQL database backup script for all Zabbix versions from 1.3.1 up to 3.0.3. - -For more informations, see the Wiki (https://github.com/maxhq/zabbix-backup/wiki). +For more informations, see the [Project Wiki](https://github.com/maxhq/zabbix-backup/wiki). ## Version history From 273e73e437f56ce2f98b1b9375928cd34714714b Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Sat, 10 Sep 2016 12:21:22 +0200 Subject: [PATCH 058/122] DOC Reference latest relase in README --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cb0e350..1ec5b8d 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,15 @@ This is a MySQL database backup script for the [Zabbix](http://www.zabbix.com/) monitoring software from version 1.3.1 up to 3.0.3. -For more informations, see the [Project Wiki](https://github.com/maxhq/zabbix-backup/wiki). +## Download + +Download the latest (stable) release here: + +https://github.com/maxhq/zabbix-backup/releases/latest + +## More informations + +Please see the [Project Wiki](https://github.com/maxhq/zabbix-backup/wiki). ## Version history From 658982e0e21cf3a8208c6ca6cc66334271f5250b Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Sat, 10 Sep 2016 12:33:24 +0200 Subject: [PATCH 059/122] DOC Added "announcment" to RELEASE task list --- RELEASE.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/RELEASE.md b/RELEASE.md index c74d192..663ef06 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -9,3 +9,22 @@ * Add release at https://github.com/maxhq/zabbix-backup/releases * Update https://github.com/maxhq/zabbix-backup/wiki * Update http://zabbix.org/wiki/Docs/howto/mysql_backup_script +* Announce release at https://www.linkedin.com/groups/161448 + + > **New version x.x.x of zabbix-mysql-dump** + > + > zabbix-mysql-dump is a Linux bash script for backing up a Zabbix MySQL database.
+ > Tables holding configuration data will be fully backed up while for mass data tables (events, history, trends, ...) only the table schema is stored. + > + > Overiew: https://github.com/maxhq/zabbix-backup/wiki
+ > Download: https://github.com/maxhq/zabbix-backup/releases/latest + +* Announce release at https://www.xing.com/communities/forums/100845147 + + > **Neue Version x.x.x von zabbix-mysql-dump** + > + > zabbix-mysql-dump ist ein Linux-Bash-Skript zum Backup der MySQL-Datenbank einer Zabbix-Installation.
+ > Es sichert Konfigurationsdaten komplett, jedoch bei Tabellen mit Massendaten (Historie, Events, Trends) nur das "leere" Datenbankschema. + > + > Übersicht: https://github.com/maxhq/zabbix-backup/wiki
+ > Download: https://github.com/maxhq/zabbix-backup/releases/latest From 0096543231f4d26d90bd22900c6373c082301a1b Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 2 Mar 2017 00:20:36 +0100 Subject: [PATCH 060/122] Enhanced known working version range to 3.0.7 (thanks, Lars Vinder) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cb0e350..c339b5e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Zabbix MySQL backup -This is a MySQL database backup script for the [Zabbix](http://www.zabbix.com/) monitoring software from version 1.3.1 up to 3.0.3. +This is a MySQL database backup script for the [Zabbix](http://www.zabbix.com/) monitoring software from version 1.3.1 up to 3.0.7. For more informations, see the [Project Wiki](https://github.com/maxhq/zabbix-backup/wiki). From b16b2d5a5c502670098c303dfa1ca0a31906d027 Mon Sep 17 00:00:00 2001 From: Sergey Date: Fri, 3 Mar 2017 08:10:33 +0400 Subject: [PATCH 061/122] Add postgres dump --- README.md | 4 +- zabbix-dump | 558 ++++++++++++++++++++++++++++++++++++++++++++++ zabbix-mysql-dump | 470 +------------------------------------- 3 files changed, 561 insertions(+), 471 deletions(-) create mode 100755 zabbix-dump mode change 100755 => 120000 zabbix-mysql-dump diff --git a/README.md b/README.md index eab7e1d..7d5029b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# Zabbix MySQL backup +# Zabbix MySQL/PostgreSQL backup -This is a MySQL database backup script for the [Zabbix](http://www.zabbix.com/) monitoring software from version 1.3.1 up to 3.0.7. +This is a MySQL/PostgreSQL database backup script for the [Zabbix](http://www.zabbix.com/) monitoring software from version 1.3.1 up to 3.0.7. ## Download diff --git a/zabbix-dump b/zabbix-dump new file mode 100755 index 0000000..dde6f52 --- /dev/null +++ b/zabbix-dump @@ -0,0 +1,558 @@ +#!/usr/bin/env bash +# +# NAME +# zabbix-mysql-dump - Configuration Backup for Zabbix with MySQL +# +# VERSION +# 0.8.2 +# +# SYNOPSIS +# This is a MySQL configuration backup script for Zabbix 1.x, 2.x and 3.0.x. +# It does a full backup of all configuration tables, but only a schema +# backup of large data tables. +# +# The script is based on a script by Ricardo Santos +# (http://zabbixzone.com/zabbix/backuping-only-the-zabbix-configuration/) +# +# CONTRIBUTORS +# - Ricardo Santos +# - Jens Berthold (maxhq) +# - Oleksiy Zagorskyi (zalex) +# - Petr Jendrejovsky +# - Jonathan Bayer +# - Andreas Niedermann (dre-) +# - Mișu Moldovan (dumol) +# - Daniel Schneller (dschneller) +# - Ruslan Ohitin (ruslan-ohitin) +# - Jonathan Wright (neonardo1) +# - msjmeyer +# +# AUTHOR +# Jens Berthold (maxhq), 2016 +# +# LICENSE +# This script is released under the MIT License (see LICENSE.txt) + + +# +# DEFAULT VALUES +# +# DO NOT EDIT THESE VALUES! +# Instead, use command line parameters or a config file to specify options. +# +DUMPDIR="$PWD" +DBTYPE="mysql" +DBHOST="127.0.0.1" +DBNAME="zabbix" +DBUSER="zabbix" +DBPASS="" +DBPORT="5432" +COMPRESSION="gz" +QUIET="no" +REVERSELOOKUP="yes" +GENERATIONSTOKEEP=0 + +# +# SHOW HELP +# +if [ -z "$1" ]; then + cat <&2; exit 1 ;; + :) echo "Option -$OPTARG requires an argument" >&2; exit 1 ;; + esac +done + +# Password prompt +if [ "$DBPASS" = "-" ]; then + read -s -p "Enter database password for user '$DBUSER' (input will be hidden): " DBPASS + echo "" +fi + +# Config file validations +if [ ! -z "$CNFFILE" ]; then + if [ ! -r "$CNFFILE" ]; then + echo "ERROR: Cannot read configuration file $CNFFILE" >&2 + exit 1 + fi + # Database name needs special treatment: + # For mysqldump it has to be specified on the command line! + # Therefore we need to get it from the config file + if [ $DB_GIVEN -eq 0 ]; then + DBNAME=$(grep -m 1 ^database= "$CNFFILE" | cut -d= -f2) + fi +fi + +# +# CONSTANTS +# +SUFFIX=""; test ! -z $COMPRESSION && SUFFIX=".${COMPRESSION}" + +DB_OPTS=() +[ ! -z "$DBHOST" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBHOST) +case $DBTYPE in + mysql) + [ ! -z "$CNFFILE" ] && DB_OPTS=("${DB_OPTS[@]}" --defaults-extra-file="$CNFFILE") + [ ! -z "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -u $DBUSER) + [ ! -z "$DBPASS" ] && DB_OPTS=("${DB_OPTS[@]}" -p"$DBPASS") + + DB_OPTS_BATCH=("${DB_OPTS[@]}" --batch --silent) + [ ! -z "$DBNAME" ] && DB_OPTS_BATCH=("${DB_OPTS_BATCH[@]}" -D $DBNAME) + ;; + psql) + [ ! -z "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -U $DBUSER) + if [ ! -z "$DBPASS" ]; then + PGPASSFILE=$(mktemp -u) + echo "$DBHOST:$DBPORT:$DBNAME:$DBUSER:$DBPASS" > $PGPASSFILE + chmod 600 $PGPASSFILE + fi + DB_OPTS_BATCH=("${DB_OPTS[@]}" -Atw) + [ ! -z "$DBNAME" ] && DB_OPTS_BATCH=("${DB_OPTS_BATCH[@]}" -d $DBNAME) + ;; +esac +# Log file for errors +ERRORLOG=$(mktemp) + +# Host name: try reverse lookup if IP is given +DBHOSTNAME="$DBHOST" +command -v dig >/dev/null 2>&1 +FIND_DIG=$? +if [ "$REVERSELOOKUP" == "yes" -a $FIND_DIG -eq 0 ]; then + # Try resolving a given host ip + newHostname=$(dig +noall +answer -x $DBHOST | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') + test \! -z "$newHostname" && DBHOSTNAME="$newHostname" +fi + +# +# CONFIG DUMP +# +if [ "$QUIET" == "no" ]; then + cat <<-EOF + Configuration: + - database type: $DBTYPE + - host: $DBHOST ($DBHOSTNAME) + - database: $DBNAME + - user: $DBUSER + - output: $DUMPDIR + +EOF +fi + +# +# FUNCTIONS +# + +# Returns TRUE if argument 1 is part of the given array (remaining arguments) +elementIn () { + local e + for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done + return 1 +} +check_binary() { + if [ ! -x "/usr/bin/$1" ]; then + echo "$1 not found." >&2 + case $1 in + mysql) + echo "(with Debian, \"apt-get install mysql-client\" will help)" >&2 + exit 1 + ;; + psql) + echo "(with Debian, \"apt-get install postgresql-client\" will help)" >&2 + exit 1 + ;; + esac + fi +} +clean_psql_pass() { + if [ $DBTYPE = "psql" -a -n "$PGPASSFILE" ]; then + rm -f $PGPASSFILE + fi +} +# +# CHECKS +# +case $DBTYPE in + mysql) + check_binary mysqldump + ;; + psql) + check_binary pg_dump + ;; + *) + echo "Sorry, don't support database type '$DBTYPE'" + exit 1 + ;; +esac +# +# READ TABLE LIST from __DATA__ section at the end of this script +# (http://stackoverflow.com/a/3477269/2983301) +# +DATA_TABLES=() +while read line; do + table=$(echo "$line" | cut -d" " -f1) + echo "$line" | cut -d" " -f5 | grep -qi "DATA" + test $? -eq 0 && DATA_TABLES+=($table) +done < <(sed '0,/^__DATA__$/d' "$BASH_SOURCE" | tr -s " ") + +# paranoid check +if [ ${#DATA_TABLES[@]} -lt 5 ]; then + echo "ERROR: The number of large data tables configured in this script is less than 5." >&2 + exit 1 +fi + +# +# BACKUP +# +# Read table list from database +[ "$QUIET" == "no" ] && echo "Fetching list of existing tables..." +case $DBTYPE in + mysql) + DB_TABLES=$(mysql "${DB_OPTS_BATCH[@]}" -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" 2>$ERRORLOG) + ;; + psql) + DB_TABLES=$(psql "${DB_OPTS_BATCH[@]}" -c "SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'" 2>$ERRORLOG) + ;; +esac +if [ $? -ne 0 ]; then + echo "ERROR while trying to access database:" 2>&1; + cat $ERRORLOG 2>&1; + clean_psql_pass + exit 1; +fi + +DB_TABLES=$(echo "$DB_TABLES" | sort) +DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) + + +# Query Zabbix database version +VERSION="" +case $DBTYPE in + mysql) + DB_VER=$(mysql "${DB_OPTS_BATCH[@]}" -N -e "select optional from dbversion;" 2>/dev/null) + ;; + psql) + DB_VER=$(psql "${DB_OPTS_BATCH[@]}" -c "select optional from dbversion;" 2>/dev/null) + ;; +esac +if [ $? -eq 0 ]; then + # version string is like: 02030015 + re='(.*)([0-9]{2})([0-9]{4})' + if [[ $DB_VER =~ $re ]]; then + VERSION="_db-${DBTYPE}-${BASH_REMATCH[1]}.$(( ${BASH_REMATCH[2]} + 0 )).$(( ${BASH_REMATCH[3]} + 0 ))" + fi +fi + +# Assemble file name +DUMPFILENAME_PREFIX="zabbix_cfg_${DBHOSTNAME}" +DUMPFILEBASE="${DUMPFILENAME_PREFIX}_$(date +%Y%m%d-%H%M)${VERSION}.sql" +DUMPFILE="$DUMPDIR/$DUMPFILEBASE" + +PROCESSED_DATA_TABLES=() +i=0 + +mkdir -p "${DUMPDIR}" + +[ "$QUIET" == "no" ] && echo "Starting table backups..." +case $DBTYPE in + mysql) + while read table; do + # large data tables: only store schema + if elementIn "$table" "${DATA_TABLES[@]}"; then + dump_opt="--no-data" + PROCESSED_DATA_TABLES+=($table) + # configuration tables: full dump + else + dump_opt="--extended-insert=FALSE" + fi + + mysqldump "${DB_OPTS[@]}" \ + --routines --opt --single-transaction --skip-lock-tables \ + $dump_opt \ + $DBNAME --tables ${table} >> "$DUMPFILE" 2>$ERRORLOG + + if [ $? -ne 0 ]; then echo -e "\nERROR: Could not backup table ${table}:" >&2; cat $ERRORLOG >&2; exit 1; fi + + if [ "$QUIET" == "no" ]; then + # show percentage + i=$((i+1)); i_percent=$(($i * 100 / $DB_TABLE_NUM)) + if [ $(($i_percent % 12)) -eq 0 ]; then + echo -n "${i_percent}%" + else + if [ $(($i_percent % 2)) -eq 0 ]; then echo -n "."; fi + fi + fi + done <<<"$DB_TABLES" + ;; + psql) + while read table; do + if elementIn "$table" "${DATA_TABLES[@]}"; then + dump_opt="--exclude-table-data=$table" + PROCESSED_DATA_TABLES+=($dump_opt) + fi + done <<<"$DB_TABLES" + pg_dump "${DB_OPTS[@]}" ${PROCESSED_DATA_TABLES[@]} > "$DUMPFILE" 2>$ERRORLOG + if [ $? -ne 0 ]; then + echo -e "\nERROR: Could not backup database." >&2 + cat $ERRORLOG >&2 + clean_psql_pass + exit 1 + fi + ;; +esac + +rm $ERRORLOG + +# +# COMPRESS BACKUP +# +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 "Compressing backup file..." +fi + +EXITCODE=0 +if [ "$COMPRESSION" == "gz" ]; then gzip -f "$DUMPFILE"; EXITCODE=$?; fi +if [ "$COMPRESSION" == "xz" ]; then xz -f "$DUMPFILE"; EXITCODE=$?; fi +if [ $EXITCODE -ne 0 ]; then + echo -e "\nERROR: Could not compress backup file, see previous messages" >&2 + clean_psql_pass + exit 1 +fi + +[ "$QUIET" == "no" ] && echo -e "\nBackup Completed:\n${DUMPFILE}${SUFFIX}" + +# +# ROTATE OLD BACKUPS +# +if [ $GENERATIONSTOKEEP -gt 0 ]; then + [ "$QUIET" == "no" ] && echo "Removing old backups, keeping up to $GENERATIONSTOKEEP" + REMOVE_OLD_CMD="cd \"$DUMPDIR\" && ls -t \"${DUMPFILENAME_PREFIX}\"* | /usr/bin/awk \"NR>${GENERATIONSTOKEEP}\" | xargs rm -f " + eval ${REMOVE_OLD_CMD} + if [ $? -ne 0 ]; then + echo "ERROR: Could not rotate old backups" >&2 + clean_psql_pass + exit 1 + fi +fi + +clean_psql_pass + +exit 0 + +################################################################################ +# List of all known table names and a flag indicating data (=large) tables +# + +__DATA__ +acknowledges 1.3.1 - 3.0.3 DATA +actions 1.3.1 - 3.0.3 +alerts 1.3.1 - 3.0.3 DATA +application_discovery 2.5.0 - 3.0.3 +application_prototype 2.5.0 - 3.0.3 +application_template 2.1.0 - 3.0.3 +applications 1.3.1 - 3.0.3 +auditlog 1.3.1 - 3.0.3 DATA +auditlog_details 1.7 - 3.0.3 DATA +autoreg 1.3.1 - 1.3.4 +autoreg_host 1.7 - 3.0.3 +conditions 1.3.1 - 3.0.3 +config 1.3.1 - 3.0.3 +dbversion 2.1.0 - 3.0.3 +dchecks 1.3.4 - 3.0.3 +dhosts 1.3.4 - 3.0.3 +drules 1.3.4 - 3.0.3 +dservices 1.3.4 - 3.0.3 +escalations 1.5.3 - 3.0.3 +events 1.3.1 - 3.0.3 DATA +expressions 1.7 - 3.0.3 +functions 1.3.1 - 3.0.3 +globalmacro 1.7 - 3.0.3 +globalvars 1.9.6 - 3.0.3 +graph_discovery 1.9.0 - 3.0.3 +graph_theme 1.7 - 3.0.3 +graphs 1.3.1 - 3.0.3 +graphs_items 1.3.1 - 3.0.3 +group_discovery 2.1.4 - 3.0.3 +group_prototype 2.1.4 - 3.0.3 +groups 1.3.1 - 3.0.3 +help_items 1.3.1 - 2.1.8 +history 1.3.1 - 3.0.3 DATA +history_log 1.3.1 - 3.0.3 DATA +history_str 1.3.1 - 3.0.3 DATA +history_str_sync 1.3.1 - 2.2.13 DATA +history_sync 1.3.1 - 2.2.13 DATA +history_text 1.3.1 - 3.0.3 DATA +history_uint 1.3.1 - 3.0.3 DATA +history_uint_sync 1.3.1 - 2.2.13 DATA +host_discovery 2.1.4 - 3.0.3 +host_inventory 1.9.6 - 3.0.3 +host_profile 1.9.3 - 1.9.5 +hostmacro 1.7 - 3.0.3 +hosts 1.3.1 - 3.0.3 +hosts_groups 1.3.1 - 3.0.3 +hosts_profiles 1.3.1 - 1.9.2 +hosts_profiles_ext 1.6 - 1.9.2 +hosts_templates 1.3.1 - 3.0.3 +housekeeper 1.3.1 - 3.0.3 +httpstep 1.3.3 - 3.0.3 +httpstepitem 1.3.3 - 3.0.3 +httptest 1.3.3 - 3.0.3 +httptestitem 1.3.3 - 3.0.3 +icon_map 1.9.6 - 3.0.3 +icon_mapping 1.9.6 - 3.0.3 +ids 1.3.3 - 3.0.3 +images 1.3.1 - 3.0.3 +interface 1.9.1 - 3.0.3 +interface_discovery 2.1.4 - 3.0.3 +item_application_prototype 2.5.0 - 3.0.3 +item_condition 2.3.0 - 3.0.3 +item_discovery 1.9.0 - 3.0.3 +items 1.3.1 - 3.0.3 +items_applications 1.3.1 - 3.0.3 +maintenances 1.7 - 3.0.3 +maintenances_groups 1.7 - 3.0.3 +maintenances_hosts 1.7 - 3.0.3 +maintenances_windows 1.7 - 3.0.3 +mappings 1.3.1 - 3.0.3 +media 1.3.1 - 3.0.3 +media_type 1.3.1 - 3.0.3 +node_cksum 1.3.1 - 2.2.13 +node_configlog 1.3.1 - 1.4.7 +nodes 1.3.1 - 2.2.13 +opcommand 1.9.4 - 3.0.3 +opcommand_grp 1.9.2 - 3.0.3 +opcommand_hst 1.9.2 - 3.0.3 +opconditions 1.5.3 - 3.0.3 +operations 1.3.4 - 3.0.3 +opgroup 1.9.2 - 3.0.3 +opinventory 3.0.0 - 3.0.3 +opmediatypes 1.7 - 1.8.22 +opmessage 1.9.2 - 3.0.3 +opmessage_grp 1.9.2 - 3.0.3 +opmessage_usr 1.9.2 - 3.0.3 +optemplate 1.9.2 - 3.0.3 +profiles 1.3.1 - 3.0.3 +proxy_autoreg_host 1.7 - 3.0.3 +proxy_dhistory 1.5 - 3.0.3 +proxy_history 1.5.1 - 3.0.3 +regexps 1.7 - 3.0.3 +rights 1.3.1 - 3.0.3 +screen_user 3.0.0 - 3.0.3 +screen_usrgrp 3.0.0 - 3.0.3 +screens 1.3.1 - 3.0.3 +screens_items 1.3.1 - 3.0.3 +scripts 1.5 - 3.0.3 +service_alarms 1.3.1 - 3.0.3 +services 1.3.1 - 3.0.3 +services_links 1.3.1 - 3.0.3 +services_times 1.3.1 - 3.0.3 +sessions 1.3.1 - 3.0.3 +slides 1.3.4 - 3.0.3 +slideshow_user 3.0.0 - 3.0.3 +slideshow_usrgrp 3.0.0 - 3.0.3 +slideshows 1.3.4 - 3.0.3 +sysmap_element_url 1.9.0 - 3.0.3 +sysmap_url 1.9.0 - 3.0.3 +sysmap_user 3.0.0 - 3.0.3 +sysmap_usrgrp 3.0.0 - 3.0.3 +sysmaps 1.3.1 - 3.0.3 +sysmaps_elements 1.3.1 - 3.0.3 +sysmaps_link_triggers 1.5 - 3.0.3 +sysmaps_links 1.3.1 - 3.0.3 +timeperiods 1.7 - 3.0.3 +trends 1.3.1 - 3.0.3 DATA +trends_uint 1.5 - 3.0.3 DATA +trigger_depends 1.3.1 - 3.0.3 +trigger_discovery 1.9.0 - 3.0.3 +triggers 1.3.1 - 3.0.3 +user_history 1.7 - 2.4.8 +users 1.3.1 - 3.0.3 +users_groups 1.3.1 - 3.0.3 +usrgrp 1.3.1 - 3.0.3 +valuemaps 1.3.1 - 3.0.3 diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump deleted file mode 100755 index a61d727..0000000 --- a/zabbix-mysql-dump +++ /dev/null @@ -1,469 +0,0 @@ -#!/usr/bin/env bash -# -# NAME -# zabbix-mysql-dump - Configuration Backup for Zabbix with MySQL -# -# VERSION -# 0.8.2 -# -# SYNOPSIS -# This is a MySQL configuration backup script for Zabbix 1.x, 2.x and 3.0.x. -# It does a full backup of all configuration tables, but only a schema -# backup of large data tables. -# -# The script is based on a script by Ricardo Santos -# (http://zabbixzone.com/zabbix/backuping-only-the-zabbix-configuration/) -# -# CONTRIBUTORS -# - Ricardo Santos -# - Jens Berthold (maxhq) -# - Oleksiy Zagorskyi (zalex) -# - Petr Jendrejovsky -# - Jonathan Bayer -# - Andreas Niedermann (dre-) -# - Mișu Moldovan (dumol) -# - Daniel Schneller (dschneller) -# - Ruslan Ohitin (ruslan-ohitin) -# - Jonathan Wright (neonardo1) -# - msjmeyer -# -# AUTHOR -# Jens Berthold (maxhq), 2016 -# -# LICENSE -# This script is released under the MIT License (see LICENSE.txt) - - -# -# DEFAULT VALUES -# -# DO NOT EDIT THESE VALUES! -# Instead, use command line parameters or a config file to specify options. -# -DUMPDIR="$PWD" -DBHOST="127.0.0.1" -DBNAME="zabbix" -DBUSER="zabbix" -DBPASS="" -COMPRESSION="gz" -QUIET="no" -REVERSELOOKUP="yes" -GENERATIONSTOKEEP=0 - -# -# SHOW HELP -# -if [ -z "$1" ]; then - cat <&2; exit 1 ;; - :) echo "Option -$OPTARG requires an argument" >&2; exit 1 ;; - esac -done - -# Password prompt -if [ "$DBPASS" = "-" ]; then - read -s -p "Enter MySQL password for user '$DBUSER' (input will be hidden): " DBPASS - echo "" -fi - -# Config file validations -if [ ! -z "$CNFFILE" ]; then - if [ ! -r "$CNFFILE" ]; then - echo "ERROR: Cannot read configuration file $CNFFILE" >&2 - exit 1 - fi - # Database name needs special treatment: - # For mysqldump it has to be specified on the command line! - # Therefore we need to get it from the config file - if [ $DB_GIVEN -eq 0 ]; then - DBNAME=$(grep -m 1 ^database= "$CNFFILE" | cut -d= -f2) - fi -fi - -# -# CONSTANTS -# -SUFFIX=""; test ! -z $COMPRESSION && SUFFIX=".${COMPRESSION}" - -MYSQL_OPTS=() -[ ! -z "$CNFFILE" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" --defaults-extra-file="$CNFFILE") -[ ! -z "$DBHOST" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" -h $DBHOST) -[ ! -z "$DBUSER" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" -u $DBUSER) -[ ! -z "$DBPASS" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" -p"$DBPASS") - -MYSQL_OPTS_BATCH=("${MYSQL_OPTS[@]}" --batch --silent) -[ ! -z "$DBNAME" ] && MYSQL_OPTS_BATCH=("${MYSQL_OPTS_BATCH[@]}" -D $DBNAME) - -# Log file for errors -ERRORLOG=$(mktemp) - -# Host name: try reverse lookup if IP is given -DBHOSTNAME="$DBHOST" -command -v dig >/dev/null 2>&1 -FIND_DIG=$? -if [ "$REVERSELOOKUP" == "yes" -a $FIND_DIG -eq 0 ]; then - # Try resolving a given host ip - newHostname=$(dig +noall +answer -x $DBHOST | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') - test \! -z "$newHostname" && DBHOSTNAME="$newHostname" -fi - -# -# CONFIG DUMP -# -if [ "$QUIET" == "no" ]; then - cat <<-EOF - Configuration: - - host: $DBHOST ($DBHOSTNAME) - - database: $DBNAME - - user: $DBUSER - - output: $DUMPDIR - -EOF -fi - -# -# FUNCTIONS -# - -# Returns TRUE if argument 1 is part of the given array (remaining arguments) -elementIn () { - local e - for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done - return 1 -} - -# -# CHECKS -# -if [ ! -x /usr/bin/mysqldump ]; then - echo "mysqldump not found." >&2 - echo "(with Debian, \"apt-get install mysql-client\" will help)" >&2 - exit 1 -fi - -# -# READ TABLE LIST from __DATA__ section at the end of this script -# (http://stackoverflow.com/a/3477269/2983301) -# -DATA_TABLES=() -while read line; do - table=$(echo "$line" | cut -d" " -f1) - echo "$line" | cut -d" " -f5 | grep -qi "DATA" - test $? -eq 0 && DATA_TABLES+=($table) -done < <(sed '0,/^__DATA__$/d' "$BASH_SOURCE" | tr -s " ") - -# paranoid check -if [ ${#DATA_TABLES[@]} -lt 5 ]; then - echo "ERROR: The number of large data tables configured in this script is less than 5." >&2 - exit 1 -fi - -# -# BACKUP -# -# Read table list from database -[ "$QUIET" == "no" ] && echo "Fetching list of existing tables..." -DB_TABLES=$(mysql "${MYSQL_OPTS_BATCH[@]}" -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" 2>$ERRORLOG) -if [ $? -ne 0 ]; then echo "ERROR while trying to access database:" 2>&1; cat $ERRORLOG 2>&1; exit 1; fi -DB_TABLES=$(echo "$DB_TABLES" | sort) -DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) - -# Query Zabbix database version -VERSION="" -DB_VER=$(mysql "${MYSQL_OPTS_BATCH[@]}" -N -e "select optional from dbversion;" 2>/dev/null) -if [ $? -eq 0 ]; then - # version string is like: 02030015 - re='(.*)([0-9]{2})([0-9]{4})' - if [[ $DB_VER =~ $re ]]; then - VERSION="_db-${BASH_REMATCH[1]}.$(( ${BASH_REMATCH[2]} + 0 )).$(( ${BASH_REMATCH[3]} + 0 ))" - fi -fi - -# Assemble file name -DUMPFILENAME_PREFIX="zabbix_cfg_${DBHOSTNAME}" -DUMPFILEBASE="${DUMPFILENAME_PREFIX}_$(date +%Y%m%d-%H%M)${VERSION}.sql" -DUMPFILE="$DUMPDIR/$DUMPFILEBASE" - -PROCESSED_DATA_TABLES=() -i=0 - -mkdir -p "${DUMPDIR}" - -[ "$QUIET" == "no" ] && echo "Starting table backups..." -while read table; do - # large data tables: only store schema - if elementIn "$table" "${DATA_TABLES[@]}"; then - dump_opt="--no-data" - PROCESSED_DATA_TABLES+=($table) - # configuration tables: full dump - else - dump_opt="--extended-insert=FALSE" - fi - - mysqldump "${MYSQL_OPTS[@]}" \ - --routines --opt --single-transaction --skip-lock-tables \ - $dump_opt \ - $DBNAME --tables ${table} >> "$DUMPFILE" 2>$ERRORLOG - - if [ $? -ne 0 ]; then echo -e "\nERROR: Could not backup table ${table}:" >&2; cat $ERRORLOG >&2; exit 1; fi - - if [ "$QUIET" == "no" ]; then - # show percentage - i=$((i+1)); i_percent=$(($i * 100 / $DB_TABLE_NUM)) - if [ $(($i_percent % 12)) -eq 0 ]; then - echo -n "${i_percent}%" - else - if [ $(($i_percent % 2)) -eq 0 ]; then echo -n "."; fi - fi - fi -done <<<"$DB_TABLES" - -rm $ERRORLOG - -# -# COMPRESS BACKUP -# -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 "Compressing backup file..." -fi - -EXITCODE=0 -if [ "$COMPRESSION" == "gz" ]; then gzip -f "$DUMPFILE"; EXITCODE=$?; fi -if [ "$COMPRESSION" == "xz" ]; then xz -f "$DUMPFILE"; EXITCODE=$?; fi -if [ $EXITCODE -ne 0 ]; then - echo -e "\nERROR: Could not compress backup file, see previous messages" >&2 - exit 1 -fi - -[ "$QUIET" == "no" ] && echo -e "\nBackup Completed:\n${DUMPFILE}${SUFFIX}" - -# -# ROTATE OLD BACKUPS -# -if [ $GENERATIONSTOKEEP -gt 0 ]; then - [ "$QUIET" == "no" ] && echo "Removing old backups, keeping up to $GENERATIONSTOKEEP" - REMOVE_OLD_CMD="cd \"$DUMPDIR\" && ls -t \"${DUMPFILENAME_PREFIX}\"* | /usr/bin/awk \"NR>${GENERATIONSTOKEEP}\" | xargs rm -f " - eval ${REMOVE_OLD_CMD} - if [ $? -ne 0 ]; then - echo "ERROR: Could not rotate old backups" >&2 - exit 1 - fi -fi - -exit 0 - -################################################################################ -# List of all known table names and a flag indicating data (=large) tables -# - -__DATA__ -acknowledges 1.3.1 - 3.0.3 DATA -actions 1.3.1 - 3.0.3 -alerts 1.3.1 - 3.0.3 DATA -application_discovery 2.5.0 - 3.0.3 -application_prototype 2.5.0 - 3.0.3 -application_template 2.1.0 - 3.0.3 -applications 1.3.1 - 3.0.3 -auditlog 1.3.1 - 3.0.3 DATA -auditlog_details 1.7 - 3.0.3 DATA -autoreg 1.3.1 - 1.3.4 -autoreg_host 1.7 - 3.0.3 -conditions 1.3.1 - 3.0.3 -config 1.3.1 - 3.0.3 -dbversion 2.1.0 - 3.0.3 -dchecks 1.3.4 - 3.0.3 -dhosts 1.3.4 - 3.0.3 -drules 1.3.4 - 3.0.3 -dservices 1.3.4 - 3.0.3 -escalations 1.5.3 - 3.0.3 -events 1.3.1 - 3.0.3 DATA -expressions 1.7 - 3.0.3 -functions 1.3.1 - 3.0.3 -globalmacro 1.7 - 3.0.3 -globalvars 1.9.6 - 3.0.3 -graph_discovery 1.9.0 - 3.0.3 -graph_theme 1.7 - 3.0.3 -graphs 1.3.1 - 3.0.3 -graphs_items 1.3.1 - 3.0.3 -group_discovery 2.1.4 - 3.0.3 -group_prototype 2.1.4 - 3.0.3 -groups 1.3.1 - 3.0.3 -help_items 1.3.1 - 2.1.8 -history 1.3.1 - 3.0.3 DATA -history_log 1.3.1 - 3.0.3 DATA -history_str 1.3.1 - 3.0.3 DATA -history_str_sync 1.3.1 - 2.2.13 DATA -history_sync 1.3.1 - 2.2.13 DATA -history_text 1.3.1 - 3.0.3 DATA -history_uint 1.3.1 - 3.0.3 DATA -history_uint_sync 1.3.1 - 2.2.13 DATA -host_discovery 2.1.4 - 3.0.3 -host_inventory 1.9.6 - 3.0.3 -host_profile 1.9.3 - 1.9.5 -hostmacro 1.7 - 3.0.3 -hosts 1.3.1 - 3.0.3 -hosts_groups 1.3.1 - 3.0.3 -hosts_profiles 1.3.1 - 1.9.2 -hosts_profiles_ext 1.6 - 1.9.2 -hosts_templates 1.3.1 - 3.0.3 -housekeeper 1.3.1 - 3.0.3 -httpstep 1.3.3 - 3.0.3 -httpstepitem 1.3.3 - 3.0.3 -httptest 1.3.3 - 3.0.3 -httptestitem 1.3.3 - 3.0.3 -icon_map 1.9.6 - 3.0.3 -icon_mapping 1.9.6 - 3.0.3 -ids 1.3.3 - 3.0.3 -images 1.3.1 - 3.0.3 -interface 1.9.1 - 3.0.3 -interface_discovery 2.1.4 - 3.0.3 -item_application_prototype 2.5.0 - 3.0.3 -item_condition 2.3.0 - 3.0.3 -item_discovery 1.9.0 - 3.0.3 -items 1.3.1 - 3.0.3 -items_applications 1.3.1 - 3.0.3 -maintenances 1.7 - 3.0.3 -maintenances_groups 1.7 - 3.0.3 -maintenances_hosts 1.7 - 3.0.3 -maintenances_windows 1.7 - 3.0.3 -mappings 1.3.1 - 3.0.3 -media 1.3.1 - 3.0.3 -media_type 1.3.1 - 3.0.3 -node_cksum 1.3.1 - 2.2.13 -node_configlog 1.3.1 - 1.4.7 -nodes 1.3.1 - 2.2.13 -opcommand 1.9.4 - 3.0.3 -opcommand_grp 1.9.2 - 3.0.3 -opcommand_hst 1.9.2 - 3.0.3 -opconditions 1.5.3 - 3.0.3 -operations 1.3.4 - 3.0.3 -opgroup 1.9.2 - 3.0.3 -opinventory 3.0.0 - 3.0.3 -opmediatypes 1.7 - 1.8.22 -opmessage 1.9.2 - 3.0.3 -opmessage_grp 1.9.2 - 3.0.3 -opmessage_usr 1.9.2 - 3.0.3 -optemplate 1.9.2 - 3.0.3 -profiles 1.3.1 - 3.0.3 -proxy_autoreg_host 1.7 - 3.0.3 -proxy_dhistory 1.5 - 3.0.3 -proxy_history 1.5.1 - 3.0.3 -regexps 1.7 - 3.0.3 -rights 1.3.1 - 3.0.3 -screen_user 3.0.0 - 3.0.3 -screen_usrgrp 3.0.0 - 3.0.3 -screens 1.3.1 - 3.0.3 -screens_items 1.3.1 - 3.0.3 -scripts 1.5 - 3.0.3 -service_alarms 1.3.1 - 3.0.3 -services 1.3.1 - 3.0.3 -services_links 1.3.1 - 3.0.3 -services_times 1.3.1 - 3.0.3 -sessions 1.3.1 - 3.0.3 -slides 1.3.4 - 3.0.3 -slideshow_user 3.0.0 - 3.0.3 -slideshow_usrgrp 3.0.0 - 3.0.3 -slideshows 1.3.4 - 3.0.3 -sysmap_element_url 1.9.0 - 3.0.3 -sysmap_url 1.9.0 - 3.0.3 -sysmap_user 3.0.0 - 3.0.3 -sysmap_usrgrp 3.0.0 - 3.0.3 -sysmaps 1.3.1 - 3.0.3 -sysmaps_elements 1.3.1 - 3.0.3 -sysmaps_link_triggers 1.5 - 3.0.3 -sysmaps_links 1.3.1 - 3.0.3 -timeperiods 1.7 - 3.0.3 -trends 1.3.1 - 3.0.3 DATA -trends_uint 1.5 - 3.0.3 DATA -trigger_depends 1.3.1 - 3.0.3 -trigger_discovery 1.9.0 - 3.0.3 -triggers 1.3.1 - 3.0.3 -user_history 1.7 - 2.4.8 -users 1.3.1 - 3.0.3 -users_groups 1.3.1 - 3.0.3 -usrgrp 1.3.1 - 3.0.3 -valuemaps 1.3.1 - 3.0.3 diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump new file mode 120000 index 0000000..fc49d6e --- /dev/null +++ b/zabbix-mysql-dump @@ -0,0 +1 @@ +zabbix-dump \ No newline at end of file From aec57ee2abe0ccf1995310560a99f1ff0f1efcd8 Mon Sep 17 00:00:00 2001 From: Sergey Date: Sun, 5 Mar 2017 19:48:07 +0400 Subject: [PATCH 062/122] Add db port option --- zabbix-dump | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index dde6f52..ef18d7f 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -46,7 +46,6 @@ DBHOST="127.0.0.1" DBNAME="zabbix" DBUSER="zabbix" DBPASS="" -DBPORT="5432" COMPRESSION="gz" QUIET="no" REVERSELOOKUP="yes" @@ -70,8 +69,12 @@ OPTIONS Default: $DBHOST -d DATABASE - Zabbix database name. + /Zabbix database name. Default: $DBNAME + + -P DBPORT + Zabbix database port. + Default: $DBPORT -u USER User to access Zabbix database. @@ -127,12 +130,13 @@ fi # PARSE COMMAND LINE ARGUMENTS # DB_GIVEN=0 -while getopts ":t:h:d:u:p:o:r:c:x0qn" opt; do +while getopts ":t:h:d:u:p:P:o:r:c:x0qn" opt; do case $opt in t) DBTYPE="$OPTARG" ;; h) DBHOST="$OPTARG" ;; d) DBNAME="$OPTARG"; DB_GIVEN=1 ;; u) DBUSER="$OPTARG" ;; + P) DBPORT="$OPTARG" ;; p) DBPASS="$OPTARG" ;; c) CNFFILE="$OPTARG" ;; o) DUMPDIR="$OPTARG" ;; @@ -178,14 +182,17 @@ case $DBTYPE in [ ! -z "$CNFFILE" ] && DB_OPTS=("${DB_OPTS[@]}" --defaults-extra-file="$CNFFILE") [ ! -z "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -u $DBUSER) [ ! -z "$DBPASS" ] && DB_OPTS=("${DB_OPTS[@]}" -p"$DBPASS") - + [ -z "$DBPORT" ] && DBPORT="3306" + DB_OPTS=("${DB_OPTS[@]}" -P"$DBPORT") DB_OPTS_BATCH=("${DB_OPTS[@]}" --batch --silent) [ ! -z "$DBNAME" ] && DB_OPTS_BATCH=("${DB_OPTS_BATCH[@]}" -D $DBNAME) ;; psql) [ ! -z "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -U $DBUSER) + [ -z "$DBPORT" ] && DBPORT="5432" + DB_OPTS=("${DB_OPTS[@]}" -p"$DBPORT") if [ ! -z "$DBPASS" ]; then - PGPASSFILE=$(mktemp -u) + export PGPASSFILE=$(mktemp -u) echo "$DBHOST:$DBPORT:$DBNAME:$DBUSER:$DBPASS" > $PGPASSFILE chmod 600 $PGPASSFILE fi @@ -248,7 +255,7 @@ check_binary() { } clean_psql_pass() { if [ $DBTYPE = "psql" -a -n "$PGPASSFILE" ]; then - rm -f $PGPASSFILE + echo rm -f $PGPASSFILE fi } # From f0564a0366faaadb5713a5596bf7eba34374b604 Mon Sep 17 00:00:00 2001 From: Greg Cockburn Date: Wed, 15 Mar 2017 19:23:23 +1100 Subject: [PATCH 063/122] support sockets and server config parsing --- zabbix-mysql-dump | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index a61d727..07e79c0 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -49,6 +49,7 @@ COMPRESSION="gz" QUIET="no" REVERSELOOKUP="yes" GENERATIONSTOKEEP=0 +ZBXCONFIG="/etc/zabbix/zabbix_server.conf" # # SHOW HELP @@ -98,6 +99,9 @@ OPTIONS but the resulting backup will be about half the size of the same sql file compressed using gz. Your mileage may vary. + -z + Specify a zabbix configuration file to read from. + -0 Do not compress the sql dump @@ -112,6 +116,7 @@ EXAMPLES $(basename $BASH_SOURCE) -u zabbix -p - -o /tmp $(basename $BASH_SOURCE) -c /etc/mysql/mysql.cnf $(basename $BASH_SOURCE) -c /etc/mysql/mysql.cnf -d zabbixdb + $(basename $BASH_SOURCE) -z /opt/etc/zabbix_server.conf -o /tmp EOF exit 1 @@ -124,11 +129,13 @@ DB_GIVEN=0 while getopts ":h:d:u:p:o:r:c:x0qn" opt; do case $opt in h) DBHOST="$OPTARG" ;; + s) DBSOCKET="$OPTARG" ;; d) DBNAME="$OPTARG"; DB_GIVEN=1 ;; u) DBUSER="$OPTARG" ;; p) DBPASS="$OPTARG" ;; c) CNFFILE="$OPTARG" ;; o) DUMPDIR="$OPTARG" ;; + z) ZBXCONFIG="$OPTARG" ;; r) GENERATIONSTOKEEP=$(printf '%.0f' "$OPTARG") ;; x) COMPRESSION="xz" ;; 0) COMPRESSION="" ;; @@ -139,6 +146,15 @@ while getopts ":h:d:u:p:o:r:c:x0qn" opt; do esac done +if [[ -f ${ZBXCONFIG} && -r ${ZBXCONFIG} ]]; then + source ${ZBXCONFIG} + DBHOST=$DBHost + DBSOCKET=$DBSocket + DBNAME=$DBName + DBUSER=$DBUser + DBPASS=$DBPassword +fi + # Password prompt if [ "$DBPASS" = "-" ]; then read -s -p "Enter MySQL password for user '$DBUSER' (input will be hidden): " DBPASS @@ -167,6 +183,7 @@ SUFFIX=""; test ! -z $COMPRESSION && SUFFIX=".${COMPRESSION}" MYSQL_OPTS=() [ ! -z "$CNFFILE" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" --defaults-extra-file="$CNFFILE") [ ! -z "$DBHOST" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" -h $DBHOST) +[ ! -z "$DBSOCKET" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" -S $DBSOCKET) [ ! -z "$DBUSER" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" -u $DBUSER) [ ! -z "$DBPASS" ] && MYSQL_OPTS=("${MYSQL_OPTS[@]}" -p"$DBPASS") From 0d5366868ab7421200454bf83055d89f8e34fcf7 Mon Sep 17 00:00:00 2001 From: Greg Cockburn Date: Wed, 15 Mar 2017 19:48:29 +1100 Subject: [PATCH 064/122] fix dig if host isn't set, and force options to be used over config file --- zabbix-mysql-dump | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 07e79c0..f9d1e64 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -126,13 +126,13 @@ fi # PARSE COMMAND LINE ARGUMENTS # DB_GIVEN=0 -while getopts ":h:d:u:p:o:r:c:x0qn" opt; do +while getopts ":h:d:u:z:s:p:o:r:c:x0qn" opt; do case $opt in - h) DBHOST="$OPTARG" ;; - s) DBSOCKET="$OPTARG" ;; - d) DBNAME="$OPTARG"; DB_GIVEN=1 ;; - u) DBUSER="$OPTARG" ;; - p) DBPASS="$OPTARG" ;; + h) ODBHOST="$OPTARG" ;; + s) ODBSOCKET="$OPTARG" ;; + d) ODBNAME="$OPTARG"; DB_GIVEN=1 ;; + u) ODBUSER="$OPTARG" ;; + p) ODBPASS="$OPTARG" ;; c) CNFFILE="$OPTARG" ;; o) DUMPDIR="$OPTARG" ;; z) ZBXCONFIG="$OPTARG" ;; @@ -155,6 +155,12 @@ if [[ -f ${ZBXCONFIG} && -r ${ZBXCONFIG} ]]; then DBPASS=$DBPassword fi +[ ! -z "$ODBHOST" ] && DBHOST=$ODBHOST +[ ! -z "$ODSOCKET" ] && DBHOST=$ODBSOCKET +[ ! -z "$ODBNAME" ] && DBHOST=$ODBNAME +[ ! -z "$ODBUSER" ] && DBHOST=$ODBUSER +[ ! -z "$ODBPASS" ] && DBHOST=$ODBPASS + # Password prompt if [ "$DBPASS" = "-" ]; then read -s -p "Enter MySQL password for user '$DBUSER' (input will be hidden): " DBPASS @@ -197,7 +203,7 @@ ERRORLOG=$(mktemp) DBHOSTNAME="$DBHOST" command -v dig >/dev/null 2>&1 FIND_DIG=$? -if [ "$REVERSELOOKUP" == "yes" -a $FIND_DIG -eq 0 ]; then +if [ "$REVERSELOOKUP" == "yes" -a $FIND_DIG -eq 0 -a -z "$DBHOST" ]; then # Try resolving a given host ip newHostname=$(dig +noall +answer -x $DBHOST | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') test \! -z "$newHostname" && DBHOSTNAME="$newHostname" From ae46152641c1baebc1f1087e464cb3a8ba573002 Mon Sep 17 00:00:00 2001 From: Greg Cockburn Date: Wed, 15 Mar 2017 20:00:01 +1100 Subject: [PATCH 065/122] fig logic --- zabbix-mysql-dump | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index f9d1e64..ce2ed15 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -203,7 +203,7 @@ ERRORLOG=$(mktemp) DBHOSTNAME="$DBHOST" command -v dig >/dev/null 2>&1 FIND_DIG=$? -if [ "$REVERSELOOKUP" == "yes" -a $FIND_DIG -eq 0 -a -z "$DBHOST" ]; then +if [ "$REVERSELOOKUP" == "yes" -a $FIND_DIG -eq 0 -a ! -z "$DBHOST" ]; then # Try resolving a given host ip newHostname=$(dig +noall +answer -x $DBHOST | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') test \! -z "$newHostname" && DBHOSTNAME="$newHostname" From 7bc26b9809a840f21d7558015d59599595e5520b Mon Sep 17 00:00:00 2001 From: Greg Cockburn Date: Mon, 3 Jul 2017 07:37:01 +1000 Subject: [PATCH 066/122] make it a bit safer --- zabbix-mysql-dump | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index ce2ed15..42211ab 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -57,7 +57,7 @@ ZBXCONFIG="/etc/zabbix/zabbix_server.conf" if [ -z "$1" ]; then cat < Date: Mon, 3 Jul 2017 08:05:16 +1000 Subject: [PATCH 067/122] cleanup and a couple of fixes --- zabbix-mysql-dump | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index 42211ab..dcca2a4 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -156,14 +156,14 @@ if [[ -f ${ZBXCONFIG} && -r ${ZBXCONFIG} ]]; then fi [ ! -z "$ODBHOST" ] && DBHOST=$ODBHOST -[ ! -z "$ODSOCKET" ] && DBHOST=$ODBSOCKET -[ ! -z "$ODBNAME" ] && DBHOST=$ODBNAME -[ ! -z "$ODBUSER" ] && DBHOST=$ODBUSER -[ ! -z "$ODBPASS" ] && DBHOST=$ODBPASS +[ ! -z "$ODBSOCKET" ] && DBSOCKET=$ODBSOCKET +[ ! -z "$ODBNAME" ] && DBNAME=$ODBNAME +[ ! -z "$ODBUSER" ] && DBUSER=$ODBUSER +[ ! -z "$ODBPASS" ] && DBPASS=$ODBPASS # Password prompt if [ "$DBPASS" = "-" ]; then - read -s -p "Enter MySQL password for user '$DBUSER' (input will be hidden): " DBPASS + read -r -s -p "Enter MySQL password for user '$DBUSER' (input will be hidden): " DBPASS echo "" fi @@ -203,9 +203,9 @@ ERRORLOG=$(mktemp) DBHOSTNAME="$DBHOST" command -v dig >/dev/null 2>&1 FIND_DIG=$? -if [ "$REVERSELOOKUP" == "yes" -a $FIND_DIG -eq 0 -a ! -z "$DBHOST" ]; then +if [ "$REVERSELOOKUP" == "yes" ] && [ $FIND_DIG -eq 0 ] && [ ! -z "$DBHOST" ]; then # Try resolving a given host ip - newHostname=$(dig +noall +answer -x $DBHOST | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') + newHostname=$(dig +noall +answer -x "${DBHOST}" | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') test \! -z "$newHostname" && DBHOSTNAME="$newHostname" fi @@ -248,11 +248,11 @@ fi # (http://stackoverflow.com/a/3477269/2983301) # DATA_TABLES=() -while read line; do +while read -r line; do table=$(echo "$line" | cut -d" " -f1) echo "$line" | cut -d" " -f5 | grep -qi "DATA" test $? -eq 0 && DATA_TABLES+=($table) -done < <(sed '0,/^__DATA__$/d' "$BASH_SOURCE" | tr -s " ") +done < <(sed '0,/^__DATA__$/d' "${BASH_SOURCE[*]}" | tr -s " ") # paranoid check if [ ${#DATA_TABLES[@]} -lt 5 ]; then @@ -273,7 +273,8 @@ DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) # Query Zabbix database version VERSION="" DB_VER=$(mysql "${MYSQL_OPTS_BATCH[@]}" -N -e "select optional from dbversion;" 2>/dev/null) -if [ $? -eq 0 ]; then +retval=$? +if [ $retval -eq 0 ]; then # version string is like: 02030015 re='(.*)([0-9]{2})([0-9]{4})' if [[ $DB_VER =~ $re ]]; then From 5e4c034b3b90b97f5313511ec6a322cf013415aa Mon Sep 17 00:00:00 2001 From: Wesley Schaft Date: Thu, 18 Oct 2018 10:32:53 +0200 Subject: [PATCH 068/122] added support up to Zabbix 4.0.0 --- README.md | 2 +- zabbix-mysql-dump | 262 ++++++++++++++++++++++++++-------------------- 2 files changed, 148 insertions(+), 116 deletions(-) diff --git a/README.md b/README.md index eab7e1d..d964b95 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Zabbix MySQL backup -This is a MySQL database backup script for the [Zabbix](http://www.zabbix.com/) monitoring software from version 1.3.1 up to 3.0.7. +This is a MySQL database backup script for the [Zabbix](http://www.zabbix.com/) monitoring software from version 1.3.1 up to 4.0. ## Download diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index a61d727..e5ec15b 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -7,7 +7,7 @@ # 0.8.2 # # SYNOPSIS -# This is a MySQL configuration backup script for Zabbix 1.x, 2.x and 3.0.x. +# This is a MySQL configuration backup script for Zabbix 1.x, 2.x, 3.x and 4.x. # It does a full backup of all configuration tables, but only a schema # backup of large data tables. # @@ -214,7 +214,7 @@ elementIn () { # # CHECKS # -if [ ! -x /usr/bin/mysqldump ]; then +if [ ! -x $(which mysqldump) ]; then echo "mysqldump not found." >&2 echo "(with Debian, \"apt-get install mysql-client\" will help)" >&2 exit 1 @@ -341,129 +341,161 @@ exit 0 # __DATA__ -acknowledges 1.3.1 - 3.0.3 DATA -actions 1.3.1 - 3.0.3 -alerts 1.3.1 - 3.0.3 DATA -application_discovery 2.5.0 - 3.0.3 -application_prototype 2.5.0 - 3.0.3 -application_template 2.1.0 - 3.0.3 -applications 1.3.1 - 3.0.3 -auditlog 1.3.1 - 3.0.3 DATA -auditlog_details 1.7 - 3.0.3 DATA +acknowledges 1.3.1 - 4.0.0 DATA +actions 1.3.1 - 4.0.0 +alerts 1.3.1 - 4.0.0 DATA +application_discovery 2.5.0 - 4.0.0 +application_prototype 2.5.0 - 4.0.0 +application_template 2.1.0 - 4.0.0 +applications 1.3.1 - 4.0.0 +auditlog 1.3.1 - 4.0.0 DATA +auditlog_details 1.7 - 4.0.0 DATA autoreg 1.3.1 - 1.3.4 -autoreg_host 1.7 - 3.0.3 -conditions 1.3.1 - 3.0.3 -config 1.3.1 - 3.0.3 -dbversion 2.1.0 - 3.0.3 -dchecks 1.3.4 - 3.0.3 -dhosts 1.3.4 - 3.0.3 -drules 1.3.4 - 3.0.3 -dservices 1.3.4 - 3.0.3 -escalations 1.5.3 - 3.0.3 -events 1.3.1 - 3.0.3 DATA -expressions 1.7 - 3.0.3 -functions 1.3.1 - 3.0.3 -globalmacro 1.7 - 3.0.3 -globalvars 1.9.6 - 3.0.3 -graph_discovery 1.9.0 - 3.0.3 -graph_theme 1.7 - 3.0.3 -graphs 1.3.1 - 3.0.3 -graphs_items 1.3.1 - 3.0.3 -group_discovery 2.1.4 - 3.0.3 -group_prototype 2.1.4 - 3.0.3 -groups 1.3.1 - 3.0.3 +autoreg_host 1.7 - 4.0.0 +conditions 1.3.1 - 4.0.0 +config 1.3.1 - 4.0.0 +corr_condition 3.2.0 - 4.0.0 +corr_condition_group 3.2.0 - 4.0.0 +corr_condition_tag 3.2.0 - 4.0.0 +corr_condition_tagpair 3.2.0 - 4.0.0 +corr_condition_tagvalue 3.2.0 - 4.0.0 +corr_operation 3.2.0 - 4.0.0 +correlation 3.2.0 - 4.0.0 +dashboard 3.4.0 - 4.0.0 +dashboard_user 3.4.0 - 4.0.0 +dashboard_usrgrp 3.4.0 - 4.0.0 +dbversion 2.1.0 - 4.0.0 +dchecks 1.3.4 - 4.0.0 +dhosts 1.3.4 - 4.0.0 +drules 1.3.4 - 4.0.0 +dservices 1.3.4 - 4.0.0 +escalations 1.5.3 - 4.0.0 +event_recovery 3.2.0 - 4.0.0 DATA +event_suppress 4.0.0 - 4.0.0 DATA +event_tag 3.2.0 - 4.0.0 DATA +events 1.3.1 - 4.0.0 DATA +expressions 1.7 - 4.0.0 +functions 1.3.1 - 4.0.0 +globalmacro 1.7 - 4.0.0 +globalvars 1.9.6 - 4.0.0 +graph_discovery 1.9.0 - 4.0.0 +graph_theme 1.7 - 4.0.0 +graphs 1.3.1 - 4.0.0 +graphs_items 1.3.1 - 4.0.0 +group_discovery 2.1.4 - 4.0.0 +group_prototype 2.1.4 - 4.0.0 +groups 1.3.1 - 3.4.1 help_items 1.3.1 - 2.1.8 -history 1.3.1 - 3.0.3 DATA -history_log 1.3.1 - 3.0.3 DATA -history_str 1.3.1 - 3.0.3 DATA +history 1.3.1 - 4.0.0 DATA +history_log 1.3.1 - 4.0.0 DATA +history_str 1.3.1 - 3.4.1 DATA history_str_sync 1.3.1 - 2.2.13 DATA history_sync 1.3.1 - 2.2.13 DATA -history_text 1.3.1 - 3.0.3 DATA -history_uint 1.3.1 - 3.0.3 DATA +history_text 1.3.1 - 4.0.0 DATA +history_uint 1.3.1 - 4.0.0 DATA history_uint_sync 1.3.1 - 2.2.13 DATA -host_discovery 2.1.4 - 3.0.3 -host_inventory 1.9.6 - 3.0.3 +host_discovery 2.1.4 - 4.0.0 +host_inventory 1.9.6 - 4.0.0 host_profile 1.9.3 - 1.9.5 -hostmacro 1.7 - 3.0.3 -hosts 1.3.1 - 3.0.3 -hosts_groups 1.3.1 - 3.0.3 +hostmacro 1.7 - 4.0.0 +hosts 1.3.1 - 4.0.0 +hosts_groups 1.3.1 - 4.0.0 hosts_profiles 1.3.1 - 1.9.2 hosts_profiles_ext 1.6 - 1.9.2 -hosts_templates 1.3.1 - 3.0.3 -housekeeper 1.3.1 - 3.0.3 -httpstep 1.3.3 - 3.0.3 -httpstepitem 1.3.3 - 3.0.3 -httptest 1.3.3 - 3.0.3 -httptestitem 1.3.3 - 3.0.3 -icon_map 1.9.6 - 3.0.3 -icon_mapping 1.9.6 - 3.0.3 -ids 1.3.3 - 3.0.3 -images 1.3.1 - 3.0.3 -interface 1.9.1 - 3.0.3 -interface_discovery 2.1.4 - 3.0.3 -item_application_prototype 2.5.0 - 3.0.3 -item_condition 2.3.0 - 3.0.3 -item_discovery 1.9.0 - 3.0.3 -items 1.3.1 - 3.0.3 -items_applications 1.3.1 - 3.0.3 -maintenances 1.7 - 3.0.3 -maintenances_groups 1.7 - 3.0.3 -maintenances_hosts 1.7 - 3.0.3 -maintenances_windows 1.7 - 3.0.3 -mappings 1.3.1 - 3.0.3 -media 1.3.1 - 3.0.3 -media_type 1.3.1 - 3.0.3 +hosts_templates 1.3.1 - 4.0.0 +housekeeper 1.3.1 - 4.0.0 +hstgrp 4.0.0 - 4.0.0 +httpstep 1.3.3 - 4.0.0 +httpstep_field 3.4.0 - 4.0.0 +httpstepitem 1.3.3 - 4.0.0 +httptest 1.3.3 - 4.0.0 +httptest_field 3.4.0 - 4.0.0 +httptestitem 1.3.3 - 4.0.0 +icon_map 1.9.6 - 4.0.0 +icon_mapping 1.9.6 - 4.0.0 +ids 1.3.3 - 4.0.0 +images 1.3.1 - 4.0.0 +interface 1.9.1 - 4.0.0 +interface_discovery 2.1.4 - 4.0.0 +item_application_prototype 2.5.0 - 4.0.0 +item_condition 2.3.0 - 4.0.0 +item_discovery 1.9.0 - 4.0.0 +item_preproc 3.4.0 - 4.0.0 +items 1.3.1 - 4.0.0 +items_applications 1.3.1 - 4.0.0 +maintenance_tag 4.0.0 - 4.0.0 DATA +maintenances 1.7 - 4.0.0 +maintenances_groups 1.7 - 4.0.0 +maintenances_hosts 1.7 - 4.0.0 +maintenances_windows 1.7 - 4.0.0 +mappings 1.3.1 - 4.0.0 +media 1.3.1 - 4.0.0 +media_type 1.3.1 - 4.0.0 node_cksum 1.3.1 - 2.2.13 node_configlog 1.3.1 - 1.4.7 nodes 1.3.1 - 2.2.13 -opcommand 1.9.4 - 3.0.3 -opcommand_grp 1.9.2 - 3.0.3 -opcommand_hst 1.9.2 - 3.0.3 -opconditions 1.5.3 - 3.0.3 -operations 1.3.4 - 3.0.3 -opgroup 1.9.2 - 3.0.3 -opinventory 3.0.0 - 3.0.3 +opcommand 1.9.4 - 4.0.0 +opcommand_grp 1.9.2 - 4.0.0 +opcommand_hst 1.9.2 - 4.0.0 +opconditions 1.5.3 - 4.0.0 +operations 1.3.4 - 4.0.0 +opgroup 1.9.2 - 4.0.0 +opinventory 3.0.0 - 4.0.0 opmediatypes 1.7 - 1.8.22 -opmessage 1.9.2 - 3.0.3 -opmessage_grp 1.9.2 - 3.0.3 -opmessage_usr 1.9.2 - 3.0.3 -optemplate 1.9.2 - 3.0.3 -profiles 1.3.1 - 3.0.3 -proxy_autoreg_host 1.7 - 3.0.3 -proxy_dhistory 1.5 - 3.0.3 -proxy_history 1.5.1 - 3.0.3 -regexps 1.7 - 3.0.3 -rights 1.3.1 - 3.0.3 -screen_user 3.0.0 - 3.0.3 -screen_usrgrp 3.0.0 - 3.0.3 -screens 1.3.1 - 3.0.3 -screens_items 1.3.1 - 3.0.3 -scripts 1.5 - 3.0.3 -service_alarms 1.3.1 - 3.0.3 -services 1.3.1 - 3.0.3 -services_links 1.3.1 - 3.0.3 -services_times 1.3.1 - 3.0.3 -sessions 1.3.1 - 3.0.3 -slides 1.3.4 - 3.0.3 -slideshow_user 3.0.0 - 3.0.3 -slideshow_usrgrp 3.0.0 - 3.0.3 -slideshows 1.3.4 - 3.0.3 -sysmap_element_url 1.9.0 - 3.0.3 -sysmap_url 1.9.0 - 3.0.3 -sysmap_user 3.0.0 - 3.0.3 -sysmap_usrgrp 3.0.0 - 3.0.3 -sysmaps 1.3.1 - 3.0.3 -sysmaps_elements 1.3.1 - 3.0.3 -sysmaps_link_triggers 1.5 - 3.0.3 -sysmaps_links 1.3.1 - 3.0.3 -timeperiods 1.7 - 3.0.3 -trends 1.3.1 - 3.0.3 DATA -trends_uint 1.5 - 3.0.3 DATA -trigger_depends 1.3.1 - 3.0.3 -trigger_discovery 1.9.0 - 3.0.3 -triggers 1.3.1 - 3.0.3 +opmessage 1.9.2 - 4.0.0 +opmessage_grp 1.9.2 - 4.0.0 +opmessage_usr 1.9.2 - 4.0.0 +optemplate 1.9.2 - 4.0.0 +problem 3.2.0 - 4.0.0 DATA +problem_tag 3.2.0 - 4.0.0 DATA +profiles 1.3.1 - 4.0.0 +proxy_autoreg_host 1.7 - 4.0.0 +proxy_dhistory 1.5 - 4.0.0 +proxy_history 1.5.1 - 4.0.0 +regexps 1.7 - 4.0.0 +rights 1.3.1 - 4.0.0 +screen_user 3.0.0 - 4.0.0 +screen_usrgrp 3.0.0 - 4.0.0 +screens 1.3.1 - 4.0.0 +screens_items 1.3.1 - 4.0.0 +scripts 1.5 - 4.0.0 +service_alarms 1.3.1 - 4.0.0 +services 1.3.1 - 4.0.0 +services_links 1.3.1 - 4.0.0 +services_times 1.3.1 - 4.0.0 +sessions 1.3.1 - 4.0.0 +slides 1.3.4 - 4.0.0 +slideshow_user 3.0.0 - 4.0.0 +slideshow_usrgrp 3.0.0 - 4.0.0 +slideshows 1.3.4 - 4.0.0 +sysmap_element_trigger 3.4.0 - 4.0.0 +sysmap_element_url 1.9.0 - 4.0.0 +sysmap_shape 3.4.0 - 4.0.0 +sysmap_url 1.9.0 - 4.0.0 +sysmap_user 3.0.0 - 4.0.0 +sysmap_usrgrp 3.0.0 - 4.0.0 +sysmaps 1.3.1 - 4.0.0 +sysmaps_elements 1.3.1 - 4.0.0 +sysmaps_link_triggers 1.5 - 4.0.0 +sysmaps_links 1.3.1 - 4.0.0 +tag_filter 4.0.0 - 4.0.0 +task 3.2.0 - 4.0.0 DATA +task_acknowledge 3.4.0 - 4.0.0 DATA +task_check_now 4.0.0 - 4.0.0 DATA +task_close_problem 3.2.0 - 4.0.0 DATA +task_remote_command 3.4.0 - 4.0.0 DATA +task_remote_command_result 3.4.0 - 4.0.0 DATA +timeperiods 1.7 - 4.0.0 +trends 1.3.1 - 4.0.0 DATA +trends_uint 1.5 - 4.0.0 DATA +trigger_depends 1.3.1 - 4.0.0 +trigger_discovery 1.9.0 - 4.0.0 +trigger_tag 3.2.0 - 4.0.0 +triggers 1.3.1 - 4.0.0 user_history 1.7 - 2.4.8 -users 1.3.1 - 3.0.3 -users_groups 1.3.1 - 3.0.3 -usrgrp 1.3.1 - 3.0.3 -valuemaps 1.3.1 - 3.0.3 +users 1.3.1 - 4.0.0 +users_groups 1.3.1 - 4.0.0 +usrgrp 1.3.1 - 4.0.0 +valuemaps 1.3.1 - 4.0.0 +widget 3.4.0 - 4.0.0 +widget_field 3.4.0 - 4.0.0 From 2887051aa4725b3858ded7676ccea8dfc7913953 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Wed, 13 Mar 2019 16:35:29 +0100 Subject: [PATCH 069/122] Show default path to zabbix_server.conf in help output --- zabbix-mysql-dump | 2 ++ 1 file changed, 2 insertions(+) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index dcca2a4..d202c1a 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -101,6 +101,7 @@ OPTIONS -z Specify a zabbix configuration file to read from. + Default: $ZBXCONFIG -0 Do not compress the sql dump @@ -155,6 +156,7 @@ if [[ -f ${ZBXCONFIG} && -r ${ZBXCONFIG} ]]; then DBPASS="${DBPassword}" fi +# options specified explicitely override those from zabbix_server.conf (if any) [ ! -z "$ODBHOST" ] && DBHOST=$ODBHOST [ ! -z "$ODBSOCKET" ] && DBSOCKET=$ODBSOCKET [ ! -z "$ODBNAME" ] && DBNAME=$ODBNAME From bab8708fc7555bd2615bf939534f3fe59866dad3 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Wed, 13 Mar 2019 17:02:50 +0100 Subject: [PATCH 070/122] Remove unneccessary variable --- zabbix-mysql-dump | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index d202c1a..a008a1f 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -275,8 +275,7 @@ DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) # Query Zabbix database version VERSION="" DB_VER=$(mysql "${MYSQL_OPTS_BATCH[@]}" -N -e "select optional from dbversion;" 2>/dev/null) -retval=$? -if [ $retval -eq 0 ]; then +if [ $? -eq 0 ]; then # version string is like: 02030015 re='(.*)([0-9]{2})([0-9]{4})' if [[ $DB_VER =~ $re ]]; then From 93806e647e6957d5163b866850de6d8b03f7e8e2 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Wed, 13 Mar 2019 18:07:05 +0100 Subject: [PATCH 071/122] Treat "maintenance_tag" as a config table --- zabbix-mysql-dump | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix-mysql-dump b/zabbix-mysql-dump index e5ec15b..60b8df1 100755 --- a/zabbix-mysql-dump +++ b/zabbix-mysql-dump @@ -423,7 +423,7 @@ item_discovery 1.9.0 - 4.0.0 item_preproc 3.4.0 - 4.0.0 items 1.3.1 - 4.0.0 items_applications 1.3.1 - 4.0.0 -maintenance_tag 4.0.0 - 4.0.0 DATA +maintenance_tag 4.0.0 - 4.0.0 maintenances 1.7 - 4.0.0 maintenances_groups 1.7 - 4.0.0 maintenances_hosts 1.7 - 4.0.0 From 2b9506de1da675e740f328e5c2a085be9473408b Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 14 Mar 2019 10:31:10 +0100 Subject: [PATCH 072/122] Adjust help texts and code formatting --- zabbix-dump | 75 +++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 40 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index ef18d7f..8b30178 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -1,7 +1,7 @@ #!/usr/bin/env bash # # NAME -# zabbix-mysql-dump - Configuration Backup for Zabbix with MySQL +# zabbix-dump - Configuration Backup for Zabbix' MySQL or PostgreSQL data # # VERSION # 0.8.2 @@ -60,28 +60,28 @@ USAGE $(basename $BASH_SOURCE) [options] OPTIONS - -t DATABASE_TYPE - Zabbix database type (mysql or psql) - Default: $DBTYPE + -t DATABASE_TYPE + Database type (mysql or psql). + Default: $DBTYPE -h HOST - Hostname/IP of MySQL server. + Hostname/IP of database server (DBMS). Default: $DBHOST - -d DATABASE - /Zabbix database name. - Default: $DBNAME - -P DBPORT - Zabbix database port. + DBMS port. Default: $DBPORT + -d DATABASE + Name of Zabbix database. + Default: $DBNAME + -u USER - User to access Zabbix database. + DBMS user to access Zabbix database. Default: $DBUSER -p PASSWORD - User password (specify "-" for a prompt). + DBMS user password (specify "-" for a prompt). Default: no password -o DIR @@ -89,11 +89,10 @@ OPTIONS Default: $DUMPDIR -c FILE - Use FILE for MySQL (only) options (passed via --defaults-extra-file). + MySQL only: use FILE for MySQL options. PLEASE NOTE: - mysqldump needs the database to be specified via command line. - So the first "database" options found in the config file is - used for mysqldump. + So the first "database" options found in the given config file is used + for mysqldump (it needs the database to be specified via command line). -r NUM Rotate backups while keeping up to NUM generations. @@ -101,14 +100,14 @@ OPTIONS Default: keep all backups -x - Compress using xz instead of gz + Compress using XZ instead of GZip. PLEASE NOTE: - xz compression will take much longer and consume more CPU time + 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. + sql dump compressed using GZip. Your mileage may vary. -0 - Do not compress the sql dump + Do not compress the dump. -n Skip reverse lookup of IP address for host. @@ -190,7 +189,7 @@ case $DBTYPE in psql) [ ! -z "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -U $DBUSER) [ -z "$DBPORT" ] && DBPORT="5432" - DB_OPTS=("${DB_OPTS[@]}" -p"$DBPORT") + DB_OPTS=("${DB_OPTS[@]}" -p"$DBPORT") if [ ! -z "$DBPASS" ]; then export PGPASSFILE=$(mktemp -u) echo "$DBHOST:$DBPORT:$DBNAME:$DBUSER:$DBPASS" > $PGPASSFILE @@ -219,11 +218,11 @@ fi if [ "$QUIET" == "no" ]; then cat <<-EOF Configuration: - - database type: $DBTYPE - - host: $DBHOST ($DBHOSTNAME) - - database: $DBNAME - - user: $DBUSER - - output: $DUMPDIR + - type: $DBTYPE + - host: $DBHOST ($DBHOSTNAME) + - database: $DBNAME + - user: $DBUSER + - output: $DUMPDIR EOF fi @@ -240,17 +239,14 @@ elementIn () { } check_binary() { if [ ! -x "/usr/bin/$1" ]; then - echo "$1 not found." >&2 + echo "Executable '$1' not found." >&2 case $1 in mysql) - echo "(with Debian, \"apt-get install mysql-client\" will help)" >&2 - exit 1 - ;; + echo "(with Debian try \"apt-get install mysql-client\")" >&2 ;; psql) - echo "(with Debian, \"apt-get install postgresql-client\" will help)" >&2 - exit 1 - ;; + echo "(with Debian try \"apt-get install postgresql-client\")" >&2 ;; esac + exit 1 fi } clean_psql_pass() { @@ -263,15 +259,13 @@ clean_psql_pass() { # case $DBTYPE in mysql) - check_binary mysqldump - ;; + check_binary mysqldump ;; psql) - check_binary pg_dump - ;; + check_binary pg_dump ;; *) - echo "Sorry, don't support database type '$DBTYPE'" - exit 1 - ;; + echo "Sorry, database type '$DBTYPE' is not supported." + echo "Please specify either 'mysql' or 'psql'." + exit 1 ;; esac # # READ TABLE LIST from __DATA__ section at the end of this script @@ -380,6 +374,7 @@ case $DBTYPE in PROCESSED_DATA_TABLES+=($dump_opt) fi done <<<"$DB_TABLES" + pg_dump "${DB_OPTS[@]}" ${PROCESSED_DATA_TABLES[@]} > "$DUMPFILE" 2>$ERRORLOG if [ $? -ne 0 ]; then echo -e "\nERROR: Could not backup database." >&2 From 0918db708bd78f63203204f9bef31c92c894a3f7 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 14 Mar 2019 10:34:57 +0100 Subject: [PATCH 073/122] Rename script to prepare merge of sergeygalkin's PR --- zabbix-mysql-dump => zabbix-dump | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename zabbix-mysql-dump => zabbix-dump (100%) diff --git a/zabbix-mysql-dump b/zabbix-dump similarity index 100% rename from zabbix-mysql-dump rename to zabbix-dump From 97fa223505cc6c897080c22a3aeac024bdd5f241 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 14 Mar 2019 10:58:17 +0100 Subject: [PATCH 074/122] Replace tabs with spaces --- zabbix-dump | 54 ++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 379e8cf..06adb14 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -360,34 +360,34 @@ mkdir -p "${DUMPDIR}" [ "$QUIET" == "no" ] && echo "Starting table backups..." case $DBTYPE in mysql) -while read table; do - # large data tables: only store schema - if elementIn "$table" "${DATA_TABLES[@]}"; then - dump_opt="--no-data" - PROCESSED_DATA_TABLES+=($table) - # configuration tables: full dump - else - dump_opt="--extended-insert=FALSE" - fi + while read table; do + # large data tables: only store schema + if elementIn "$table" "${DATA_TABLES[@]}"; then + dump_opt="--no-data" + PROCESSED_DATA_TABLES+=($table) + # configuration tables: full dump + else + dump_opt="--extended-insert=FALSE" + fi mysqldump "${DB_OPTS[@]}" \ - --routines --opt --single-transaction --skip-lock-tables \ - $dump_opt \ - $DBNAME --tables ${table} >> "$DUMPFILE" 2>$ERRORLOG - - if [ $? -ne 0 ]; then echo -e "\nERROR: Could not backup table ${table}:" >&2; cat $ERRORLOG >&2; exit 1; fi - - if [ "$QUIET" == "no" ]; then - # show percentage - i=$((i+1)); i_percent=$(($i * 100 / $DB_TABLE_NUM)) - if [ $(($i_percent % 12)) -eq 0 ]; then - echo -n "${i_percent}%" - else - if [ $(($i_percent % 2)) -eq 0 ]; then echo -n "."; fi - fi - fi -done <<<"$DB_TABLES" - ;; + --routines --opt --single-transaction --skip-lock-tables \ + $dump_opt \ + $DBNAME --tables ${table} >> "$DUMPFILE" 2>$ERRORLOG + + if [ $? -ne 0 ]; then echo -e "\nERROR: Could not backup table ${table}:" >&2; cat $ERRORLOG >&2; exit 1; fi + + if [ "$QUIET" == "no" ]; then + # show percentage + i=$((i+1)); i_percent=$(($i * 100 / $DB_TABLE_NUM)) + if [ $(($i_percent % 12)) -eq 0 ]; then + echo -n "${i_percent}%" + else + if [ $(($i_percent % 2)) -eq 0 ]; then echo -n "."; fi + fi + fi + done <<<"$DB_TABLES" + ;; psql) while read table; do if elementIn "$table" "${DATA_TABLES[@]}"; then @@ -403,7 +403,7 @@ done <<<"$DB_TABLES" clean_psql_pass exit 1 fi - ;; + ;; esac rm $ERRORLOG From f01e1d6c6fee0e9b7fdfb51f1d4613f8a3bb3f98 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 14 Mar 2019 11:23:17 +0100 Subject: [PATCH 075/122] Change host option to "-H" and use "-h" and "--help" to show help --- zabbix-dump | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 06adb14..857cfff 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -55,7 +55,7 @@ ZBXCONFIG="/etc/zabbix/zabbix_server.conf" # # SHOW HELP # -if [ -z "$1" ]; then +if [[ "$1" == "--help" || "$1" == "-h" ]]; then cat < Date: Thu, 14 Mar 2019 11:24:41 +0100 Subject: [PATCH 076/122] Change some output text --- zabbix-dump | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 857cfff..3067387 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -77,11 +77,11 @@ OPTIONS Name of Zabbix database. Default: $DBNAME - -u USER + -u DBUSER DBMS user to access Zabbix database. Default: $DBUSER - -p PASSWORD + -p DBPASSWORD DBMS user password (specify "-" for a prompt). Default: no password @@ -89,11 +89,12 @@ OPTIONS Save Zabbix database dumps to DIR. Default: $DUMPDIR - -c FILE - MySQL only: use FILE for MySQL options. + -c MYSQL_CONFIG + MySQL only: + Read database host and credentials from given MySQL config file. PLEASE NOTE: - So the first "database" options found in the given config file is used - for mysqldump (it needs the database to be specified via command line). + The first "database" option found in the config file is used for + mysqldump (it needs the database to be specified via command line). -r NUM Rotate backups while keeping up to NUM generations. @@ -117,11 +118,20 @@ OPTIONS Quiet mode: no output except for errors (for batch/crontab use). EXAMPLES - $(basename "${BASH_SOURCE[*]}") -t psql -h 1.2.3.4 -d zabbixdb -u zabbix -p test - $(basename "${BASH_SOURCE[*]}") -u zabbix -p - -o /tmp + # backup local Zabbix MySQL database into current directory + # (reads database access options from $ZBXCONFIG) + $(basename "${BASH_SOURCE[*]}") + + # backup PostgreSQL database + $(basename "${BASH_SOURCE[*]}") -t psql + + # specify MySQL database and user, ask for password + $(basename "${BASH_SOURCE[*]}") -d zabbixdb -u zabbix -p - -o /tmp + + # read DB options from MySQL config file $(basename "${BASH_SOURCE[*]}") -c /etc/mysql/mysql.cnf + # ...and overwrite database name $(basename "${BASH_SOURCE[*]}") -c /etc/mysql/mysql.cnf -d zabbixdb - $(basename "${BASH_SOURCE[*]}") -z /opt/etc/zabbix_server.conf -o /tmp EOF exit 1 @@ -237,12 +247,12 @@ fi # if [ "$QUIET" == "no" ]; then cat <<-EOF - Configuration: - - type: $DBTYPE - - host: $DBHOST ($DBHOSTNAME) - - database: $DBNAME - - user: $DBUSER - - output: $DUMPDIR +Configuration: + - type: $DBTYPE + - host: $DBHOST ($DBHOSTNAME) + - database: $DBNAME + - user: $DBUSER + - output: $DUMPDIR EOF fi From 0a30b61a4ac4e183cf670bc2fd1b5c2c0a52acdf Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 14 Mar 2019 11:25:18 +0100 Subject: [PATCH 077/122] Re-add accidentially deleted help for option "-z" --- zabbix-dump | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 3067387..2d8e04c 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -89,6 +89,10 @@ OPTIONS Save Zabbix database dumps to DIR. Default: $DUMPDIR + -z ZABBIX_CONFIG + Read database host and credentials from given Zabbix config file. + Default: $ZBXCONFIG + -c MYSQL_CONFIG MySQL only: Read database host and credentials from given MySQL config file. @@ -125,6 +129,9 @@ EXAMPLES # backup PostgreSQL database $(basename "${BASH_SOURCE[*]}") -t psql + # read DB options from given Zabbix config file + $(basename "${BASH_SOURCE[*]}") -z /opt/etc/zabbix_server.conf + # specify MySQL database and user, ask for password $(basename "${BASH_SOURCE[*]}") -d zabbixdb -u zabbix -p - -o /tmp @@ -162,8 +169,9 @@ while getopts ":t:H:d:u:z:s:p:P:o:r:c:x0qn" opt; do esac done -if [[ -f ${ZBXCONFIG} && -r ${ZBXCONFIG} ]]; then - source ${ZBXCONFIG} +if [[ -f "${ZBXCONFIG}" && -r "${ZBXCONFIG}" ]]; then + [ "$QUIET" == "no" ] && echo "Reading database options from ${ZBXCONFIG}..." + source "${ZBXCONFIG}" DBHOST="${DBHost}" DBSOCKET="${DBSocket}" DBNAME="${DBName}" From e6c0b3dac882b30fb2ef9ee4d29b235e92c87f7f Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Wed, 13 Mar 2019 15:30:37 +0100 Subject: [PATCH 078/122] Rename flag DATA to SCHEMAONLY to make it more clear what it means --- zabbix-dump | 58 +++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 2d8e04c..8dc0ef8 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -314,7 +314,7 @@ esac DATA_TABLES=() while read -r line; do table=$(echo "$line" | cut -d" " -f1) - echo "$line" | cut -d" " -f5 | grep -qi "DATA" + echo "$line" | cut -d" " -f5 | grep -qi "SCHEMAONLY" test $? -eq 0 && DATA_TABLES+=($table) done < <(sed '0,/^__DATA__$/d' "${BASH_SOURCE[*]}" | tr -s " ") @@ -467,19 +467,21 @@ clean_psql_pass exit 0 ################################################################################ -# List of all known table names and a flag indicating data (=large) tables +# List of all known table names. +# The flag SCHEMAONLY marks tables that contain monitoring data (as opposed to +# config data), so only their database schema will be backed up. # __DATA__ -acknowledges 1.3.1 - 4.0.0 DATA +acknowledges 1.3.1 - 4.0.0 SCHEMAONLY actions 1.3.1 - 4.0.0 -alerts 1.3.1 - 4.0.0 DATA +alerts 1.3.1 - 4.0.0 SCHEMAONLY application_discovery 2.5.0 - 4.0.0 application_prototype 2.5.0 - 4.0.0 application_template 2.1.0 - 4.0.0 applications 1.3.1 - 4.0.0 -auditlog 1.3.1 - 4.0.0 DATA -auditlog_details 1.7 - 4.0.0 DATA +auditlog 1.3.1 - 4.0.0 SCHEMAONLY +auditlog_details 1.7 - 4.0.0 SCHEMAONLY autoreg 1.3.1 - 1.3.4 autoreg_host 1.7 - 4.0.0 conditions 1.3.1 - 4.0.0 @@ -500,10 +502,10 @@ dhosts 1.3.4 - 4.0.0 drules 1.3.4 - 4.0.0 dservices 1.3.4 - 4.0.0 escalations 1.5.3 - 4.0.0 -event_recovery 3.2.0 - 4.0.0 DATA -event_suppress 4.0.0 - 4.0.0 DATA -event_tag 3.2.0 - 4.0.0 DATA -events 1.3.1 - 4.0.0 DATA +event_recovery 3.2.0 - 4.0.0 SCHEMAONLY +event_suppress 4.0.0 - 4.0.0 SCHEMAONLY +event_tag 3.2.0 - 4.0.0 SCHEMAONLY +events 1.3.1 - 4.0.0 SCHEMAONLY expressions 1.7 - 4.0.0 functions 1.3.1 - 4.0.0 globalmacro 1.7 - 4.0.0 @@ -516,14 +518,14 @@ group_discovery 2.1.4 - 4.0.0 group_prototype 2.1.4 - 4.0.0 groups 1.3.1 - 3.4.1 help_items 1.3.1 - 2.1.8 -history 1.3.1 - 4.0.0 DATA -history_log 1.3.1 - 4.0.0 DATA -history_str 1.3.1 - 3.4.1 DATA -history_str_sync 1.3.1 - 2.2.13 DATA -history_sync 1.3.1 - 2.2.13 DATA -history_text 1.3.1 - 4.0.0 DATA -history_uint 1.3.1 - 4.0.0 DATA -history_uint_sync 1.3.1 - 2.2.13 DATA +history 1.3.1 - 4.0.0 SCHEMAONLY +history_log 1.3.1 - 4.0.0 SCHEMAONLY +history_str 1.3.1 - 3.4.1 SCHEMAONLY +history_str_sync 1.3.1 - 2.2.13 SCHEMAONLY +history_sync 1.3.1 - 2.2.13 SCHEMAONLY +history_text 1.3.1 - 4.0.0 SCHEMAONLY +history_uint 1.3.1 - 4.0.0 SCHEMAONLY +history_uint_sync 1.3.1 - 2.2.13 SCHEMAONLY host_discovery 2.1.4 - 4.0.0 host_inventory 1.9.6 - 4.0.0 host_profile 1.9.3 - 1.9.5 @@ -576,8 +578,8 @@ opmessage 1.9.2 - 4.0.0 opmessage_grp 1.9.2 - 4.0.0 opmessage_usr 1.9.2 - 4.0.0 optemplate 1.9.2 - 4.0.0 -problem 3.2.0 - 4.0.0 DATA -problem_tag 3.2.0 - 4.0.0 DATA +problem 3.2.0 - 4.0.0 SCHEMAONLY +problem_tag 3.2.0 - 4.0.0 SCHEMAONLY profiles 1.3.1 - 4.0.0 proxy_autoreg_host 1.7 - 4.0.0 proxy_dhistory 1.5 - 4.0.0 @@ -609,15 +611,15 @@ sysmaps_elements 1.3.1 - 4.0.0 sysmaps_link_triggers 1.5 - 4.0.0 sysmaps_links 1.3.1 - 4.0.0 tag_filter 4.0.0 - 4.0.0 -task 3.2.0 - 4.0.0 DATA -task_acknowledge 3.4.0 - 4.0.0 DATA -task_check_now 4.0.0 - 4.0.0 DATA -task_close_problem 3.2.0 - 4.0.0 DATA -task_remote_command 3.4.0 - 4.0.0 DATA -task_remote_command_result 3.4.0 - 4.0.0 DATA +task 3.2.0 - 4.0.0 SCHEMAONLY +task_acknowledge 3.4.0 - 4.0.0 SCHEMAONLY +task_check_now 4.0.0 - 4.0.0 SCHEMAONLY +task_close_problem 3.2.0 - 4.0.0 SCHEMAONLY +task_remote_command 3.4.0 - 4.0.0 SCHEMAONLY +task_remote_command_result 3.4.0 - 4.0.0 SCHEMAONLY timeperiods 1.7 - 4.0.0 -trends 1.3.1 - 4.0.0 DATA -trends_uint 1.5 - 4.0.0 DATA +trends 1.3.1 - 4.0.0 SCHEMAONLY +trends_uint 1.5 - 4.0.0 SCHEMAONLY trigger_depends 1.3.1 - 4.0.0 trigger_discovery 1.9.0 - 4.0.0 trigger_tag 3.2.0 - 4.0.0 From 360e574afb0bfac84ddb2cd7084e2221443fcc20 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 14 Mar 2019 11:30:02 +0100 Subject: [PATCH 079/122] Reorder options for getopts command --- zabbix-dump | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix-dump b/zabbix-dump index 8dc0ef8..ab385f6 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -148,7 +148,7 @@ fi # PARSE COMMAND LINE ARGUMENTS # DB_GIVEN=0 -while getopts ":t:H:d:u:z:s:p:P:o:r:c:x0qn" opt; do +while getopts ":c:d:H:o:p:P:r:s:t:u:z:x0qn" opt; do case $opt in H) ODBHOST="$OPTARG" ;; s) ODBSOCKET="$OPTARG" ;; From c066c10997617f203d951cdb4802c46714c12663 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 14 Mar 2019 11:31:45 +0100 Subject: [PATCH 080/122] Also read database port from Zabbix config if available --- zabbix-dump | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/zabbix-dump b/zabbix-dump index ab385f6..58c4d57 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -154,7 +154,7 @@ while getopts ":c:d:H:o:p:P:r:s:t:u:z:x0qn" opt; do s) ODBSOCKET="$OPTARG" ;; d) ODBNAME="$OPTARG"; DB_GIVEN=1 ;; u) ODBUSER="$OPTARG" ;; - P) DBPORT="$OPTARG" ;; + P) ODBPORT="$OPTARG" ;; p) ODBPASS="$OPTARG" ;; c) CNFFILE="$OPTARG" ;; o) DUMPDIR="$OPTARG" ;; @@ -173,6 +173,7 @@ if [[ -f "${ZBXCONFIG}" && -r "${ZBXCONFIG}" ]]; then [ "$QUIET" == "no" ] && echo "Reading database options from ${ZBXCONFIG}..." source "${ZBXCONFIG}" DBHOST="${DBHost}" + DBPORT="${DBPort}" DBSOCKET="${DBSocket}" DBNAME="${DBName}" DBUSER="${DBUser}" @@ -181,6 +182,7 @@ fi # options specified explicitely override those from zabbix_server.conf (if any) [ ! -z "$ODBHOST" ] && DBHOST=$ODBHOST +[ ! -z "$ODBPORT" ] && DBPORT=$ODBPORT [ ! -z "$ODBSOCKET" ] && DBSOCKET=$ODBSOCKET [ ! -z "$ODBNAME" ] && DBNAME=$ODBNAME [ ! -z "$ODBUSER" ] && DBUSER=$ODBUSER From 44a2888b70635752df6db0d58e187e4ddfabd19d Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 14 Mar 2019 11:35:01 +0100 Subject: [PATCH 081/122] Support socket connections for PostgreSQL --- zabbix-dump | 1 + 1 file changed, 1 insertion(+) diff --git a/zabbix-dump b/zabbix-dump index 58c4d57..2219e16 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -227,6 +227,7 @@ case $DBTYPE in [ ! -z "$DBNAME" ] && DB_OPTS_BATCH=("${DB_OPTS_BATCH[@]}" -D $DBNAME) ;; psql) + [ ! -z "$DBSOCKET" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBSOCKET) [ ! -z "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -U $DBUSER) [ -z "$DBPORT" ] && DBPORT="5432" DB_OPTS=("${DB_OPTS[@]}" -p"$DBPORT") From fd2c1f7dab0b708e1e92c49f801743d8bc9a2f99 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 15 Mar 2019 11:39:25 +0100 Subject: [PATCH 082/122] Use "which" instead of hard coded path for binaries: suggested by yangqi --- zabbix-dump | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix-dump b/zabbix-dump index 2219e16..d58b6bf 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -279,7 +279,7 @@ elementIn () { return 1 } check_binary() { - if [ ! -x "/usr/bin/$1" ]; then + if ! which $1 > /dev/null; then echo "Executable '$1' not found." >&2 case $1 in mysql) From 3ed72687a24240b47ce7a3b46af01ccdf4bb1ab9 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 15 Mar 2019 11:27:03 +0100 Subject: [PATCH 083/122] Update README and script with changes an contributors --- README.md | 14 ++++++++++++++ zabbix-dump | 9 +++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f4b79b8..2a12841 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,20 @@ Please see the [Project Wiki](https://github.com/maxhq/zabbix-backup/wiki). ## Version history +**0.9.0 (2019-03-14)** + +- NEW: Support for PostgreSQL databases (Sergey Galkin) +- NEW: Option -P to specify database server port (Sergey Galkin) +- NEW: Support for socket connections to MySQL and PostgreSQL server (Greg Cockburn) +- NEW: Database connection parameters are read from Zabbix servern config by default (Greg Cockburn) +- ENH: Support for Zabbix 4.0 (Wesley Schaft) +- ENH: Options -h and --help to show help (hostname is now specified using -H) +- CHG: Rename script to "zabbix-dump" +- FIX: Support for whitespaces in database parameters (Greg Cockburn) +- FIX: Support for backslashes in manually entered password (Greg Cockburn) +- + + **0.8.2 (2016-09-08)** - NEW: Option -x to use XZ instead of GZ for compression (Jonathan Wright) diff --git a/zabbix-dump b/zabbix-dump index d58b6bf..bef1957 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -4,7 +4,7 @@ # zabbix-dump - Configuration Backup for Zabbix' MySQL or PostgreSQL data # # VERSION -# 0.8.2 +# 0.9.0 # # SYNOPSIS # This is a MySQL configuration backup script for Zabbix 1.x, 2.x, 3.x and 4.x. @@ -26,9 +26,14 @@ # - Ruslan Ohitin (ruslan-ohitin) # - Jonathan Wright (neonardo1) # - msjmeyer +# - Sergey Galkin (sergeygalkin) +# - Greg Cockburn (gergnz) +# - yangqi +# - Johannes Petz (PetzJohannes) +# - Wesley Schaft (wschaft) # # AUTHOR -# Jens Berthold (maxhq), 2016 +# Jens Berthold (maxhq), 2019 # # LICENSE # This script is released under the MIT License (see LICENSE.txt) From 748e766d12d8fe0893afec10121b952cf9ecb6b7 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 15 Mar 2019 12:24:53 +0100 Subject: [PATCH 084/122] New option -Z to skip reading the Zabbix server config file --- README.md | 1 + zabbix-dump | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2a12841..1715083 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Please see the [Project Wiki](https://github.com/maxhq/zabbix-backup/wiki). - NEW: Database connection parameters are read from Zabbix servern config by default (Greg Cockburn) - ENH: Support for Zabbix 4.0 (Wesley Schaft) - ENH: Options -h and --help to show help (hostname is now specified using -H) +- ENH: Options -Z to skip reading the Zabbix server config file - CHG: Rename script to "zabbix-dump" - FIX: Support for whitespaces in database parameters (Greg Cockburn) - FIX: Support for backslashes in manually entered password (Greg Cockburn) diff --git a/zabbix-dump b/zabbix-dump index bef1957..eda17d8 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -56,6 +56,7 @@ QUIET="no" REVERSELOOKUP="yes" GENERATIONSTOKEEP=0 ZBXCONFIG="/etc/zabbix/zabbix_server.conf" +READ_ZBXCONFIG="yes" # # SHOW HELP @@ -98,6 +99,9 @@ OPTIONS Read database host and credentials from given Zabbix config file. Default: $ZBXCONFIG + -Z + Do not try to read the Zabbix server configuration. + -c MYSQL_CONFIG MySQL only: Read database host and credentials from given MySQL config file. @@ -153,7 +157,7 @@ fi # PARSE COMMAND LINE ARGUMENTS # DB_GIVEN=0 -while getopts ":c:d:H:o:p:P:r:s:t:u:z:x0qn" opt; do +while getopts ":c:d:H:o:p:P:r:s:t:u:z:0nqxZ" opt; do case $opt in H) ODBHOST="$OPTARG" ;; s) ODBSOCKET="$OPTARG" ;; @@ -164,6 +168,7 @@ while getopts ":c:d:H:o:p:P:r:s:t:u:z:x0qn" opt; do c) CNFFILE="$OPTARG" ;; o) DUMPDIR="$OPTARG" ;; z) ZBXCONFIG="$OPTARG" ;; + Z) READ_ZBXCONFIG="no" ;; r) GENERATIONSTOKEEP=$(printf '%.0f' "$OPTARG") ;; x) COMPRESSION="xz" ;; 0) COMPRESSION="" ;; @@ -174,7 +179,7 @@ while getopts ":c:d:H:o:p:P:r:s:t:u:z:x0qn" opt; do esac done -if [[ -f "${ZBXCONFIG}" && -r "${ZBXCONFIG}" ]]; then +if [[ "${READ_ZBXCONFIG}" == "yes" && -f "${ZBXCONFIG}" && -r "${ZBXCONFIG}" ]]; then [ "$QUIET" == "no" ] && echo "Reading database options from ${ZBXCONFIG}..." source "${ZBXCONFIG}" DBHOST="${DBHost}" From d4f19bafee60aa37252edbc69f8d4d4cb99a51cf Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 15 Mar 2019 12:27:05 +0100 Subject: [PATCH 085/122] Rename some variables --- zabbix-dump | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index eda17d8..e7b3fd8 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -55,8 +55,8 @@ COMPRESSION="gz" QUIET="no" REVERSELOOKUP="yes" GENERATIONSTOKEEP=0 -ZBXCONFIG="/etc/zabbix/zabbix_server.conf" -READ_ZBXCONFIG="yes" +ZBX_CONFIG="/etc/zabbix/zabbix_server.conf" +READ_ZBX_CONFIG="yes" # # SHOW HELP @@ -97,7 +97,7 @@ OPTIONS -z ZABBIX_CONFIG Read database host and credentials from given Zabbix config file. - Default: $ZBXCONFIG + Default: $ZBX_CONFIG -Z Do not try to read the Zabbix server configuration. @@ -132,7 +132,7 @@ OPTIONS EXAMPLES # backup local Zabbix MySQL database into current directory - # (reads database access options from $ZBXCONFIG) + # (reads database access options from $ZBX_CONFIG) $(basename "${BASH_SOURCE[*]}") # backup PostgreSQL database @@ -165,10 +165,10 @@ while getopts ":c:d:H:o:p:P:r:s:t:u:z:0nqxZ" opt; do u) ODBUSER="$OPTARG" ;; P) ODBPORT="$OPTARG" ;; p) ODBPASS="$OPTARG" ;; - c) CNFFILE="$OPTARG" ;; + c) MYSQL_CONFIG="$OPTARG" ;; o) DUMPDIR="$OPTARG" ;; - z) ZBXCONFIG="$OPTARG" ;; - Z) READ_ZBXCONFIG="no" ;; + z) ZBX_CONFIG="$OPTARG" ;; + Z) READ_ZBX_CONFIG="no" ;; r) GENERATIONSTOKEEP=$(printf '%.0f' "$OPTARG") ;; x) COMPRESSION="xz" ;; 0) COMPRESSION="" ;; @@ -179,9 +179,9 @@ while getopts ":c:d:H:o:p:P:r:s:t:u:z:0nqxZ" opt; do esac done -if [[ "${READ_ZBXCONFIG}" == "yes" && -f "${ZBXCONFIG}" && -r "${ZBXCONFIG}" ]]; then - [ "$QUIET" == "no" ] && echo "Reading database options from ${ZBXCONFIG}..." - source "${ZBXCONFIG}" +if [[ "${READ_ZBX_CONFIG}" == "yes" && -f "${ZBX_CONFIG}" && -r "${ZBX_CONFIG}" ]]; then + [ "$QUIET" == "no" ] && echo "Reading database options from ${ZBX_CONFIG}..." + source "${ZBX_CONFIG}" DBHOST="${DBHost}" DBPORT="${DBPort}" DBSOCKET="${DBSocket}" @@ -204,17 +204,17 @@ if [ "$DBPASS" = "-" ]; then echo "" fi -# Config file validations -if [ ! -z "$CNFFILE" ]; then - if [ ! -r "$CNFFILE" ]; then - echo "ERROR: Cannot read configuration file $CNFFILE" >&2 +# MySQL config file validations +if [ ! -z "$MYSQL_CONFIG" ]; then + if [ ! -r "$MYSQL_CONFIG" ]; then + echo "ERROR: Cannot read configuration file $MYSQL_CONFIG" >&2 exit 1 fi # Database name needs special treatment: # For mysqldump it has to be specified on the command line! # Therefore we need to get it from the config file if [ $DB_GIVEN -eq 0 ]; then - DBNAME=$(grep -m 1 ^database= "$CNFFILE" | cut -d= -f2) + DBNAME=$(grep -m 1 ^database= "$MYSQL_CONFIG" | cut -d= -f2) fi fi @@ -227,7 +227,7 @@ DB_OPTS=() [ ! -z "$DBHOST" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBHOST) case $DBTYPE in mysql) - [ ! -z "$CNFFILE" ] && DB_OPTS=("${DB_OPTS[@]}" --defaults-extra-file="$CNFFILE") + [ ! -z "$MYSQL_CONFIG" ] && DB_OPTS=("${DB_OPTS[@]}" --defaults-extra-file="$MYSQL_CONFIG") [ ! -z "$DBSOCKET" ] && DB_OPTS=("${DB_OPTS[@]}" -S $DBSOCKET) [ ! -z "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -u $DBUSER) [ ! -z "$DBPASS" ] && DB_OPTS=("${DB_OPTS[@]}" -p"$DBPASS") From 7b207e70d6b9ad057448a64a5c67b2bd1639bee0 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 15 Mar 2019 12:40:53 +0100 Subject: [PATCH 086/122] Re-add accidentially deleted option "-t" (merge hell...) --- zabbix-dump | 1 + 1 file changed, 1 insertion(+) diff --git a/zabbix-dump b/zabbix-dump index e7b3fd8..5b68333 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -159,6 +159,7 @@ fi DB_GIVEN=0 while getopts ":c:d:H:o:p:P:r:s:t:u:z:0nqxZ" opt; do case $opt in + t) DBTYPE="$OPTARG" ;; H) ODBHOST="$OPTARG" ;; s) ODBSOCKET="$OPTARG" ;; d) ODBNAME="$OPTARG"; DB_GIVEN=1 ;; From 3900056a48aee1a0e9617c7a2113f9b09bf119f2 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 15 Mar 2019 13:58:53 +0100 Subject: [PATCH 087/122] Mimic special treatment of DBHost in zabbix_serverd.conf Also don't mix default values and those read from zabbix_serverd.conf anymore. --- zabbix-dump | 72 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 5b68333..c65bf1a 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -47,10 +47,10 @@ # DUMPDIR="$PWD" DBTYPE="mysql" -DBHOST="127.0.0.1" -DBNAME="zabbix" -DBUSER="zabbix" -DBPASS="" +DEFAULT_DBHOST="127.0.0.1" +DEFAULT_DBNAME="zabbix" +DEFAULT_DBUSER="zabbix" +DEFAULT_DBPASS="" COMPRESSION="gz" QUIET="no" REVERSELOOKUP="yes" @@ -73,19 +73,24 @@ OPTIONS -H DBHOST Hostname/IP of database server (DBMS). - Default: $DBHOST + Default: $DEFAULT_DBHOST -P DBPORT DBMS port. - Default: $DBPORT + Default for mysql: 3306 + Default for psql: 5432 + + -s DBSOCKET + Path to DBMS socket file. + Can be used as an alternative to specifying host (and maybe port). -d DATABASE Name of Zabbix database. - Default: $DBNAME + Default: $DEFAULT_DBNAME -u DBUSER DBMS user to access Zabbix database. - Default: $DBUSER + Default: $DEFAULT_DBUSER -p DBPASSWORD DBMS user password (specify "-" for a prompt). @@ -131,18 +136,22 @@ OPTIONS Quiet mode: no output except for errors (for batch/crontab use). EXAMPLES - # backup local Zabbix MySQL database into current directory - # (reads database access options from $ZBX_CONFIG) + # read $ZBX_CONFIG to backup local Zabbix server's + # MySQL database into current directory $(basename "${BASH_SOURCE[*]}") - # backup PostgreSQL database + # ...same for PostgreSQL $(basename "${BASH_SOURCE[*]}") -t psql + # DO NOT read $ZBX_CONFIG, + # use instead to backup local MySQL database and ask for password + $(basename "${BASH_SOURCE[*]}") -Z -p - + # read DB options from given Zabbix config file $(basename "${BASH_SOURCE[*]}") -z /opt/etc/zabbix_server.conf # specify MySQL database and user, ask for password - $(basename "${BASH_SOURCE[*]}") -d zabbixdb -u zabbix -p - -o /tmp + $(basename "${BASH_SOURCE[*]}") -Z -d zabbixdb -u zabbix -p - -o /tmp # read DB options from MySQL config file $(basename "${BASH_SOURCE[*]}") -c /etc/mysql/mysql.cnf @@ -180,18 +189,44 @@ while getopts ":c:d:H:o:p:P:r:s:t:u:z:0nqxZ" opt; do esac done +# (Try) reading database config from zabbix_server.conf if [[ "${READ_ZBX_CONFIG}" == "yes" && -f "${ZBX_CONFIG}" && -r "${ZBX_CONFIG}" ]]; then [ "$QUIET" == "no" ] && echo "Reading database options from ${ZBX_CONFIG}..." source "${ZBX_CONFIG}" - DBHOST="${DBHost}" + + # set non-existing variables to their Zabbix defaults (if they are non-empty string) + [ -z ${DBHost+x} ] && DBHost="localhost" + + # Zabbix config has a special treatment for DBHost: + # > If set to localhost, socket is used for MySQL. + # > If set to empty string, socket is used for PostgreSQL. + if [[ ( "$DBTYPE" == "mysql" && "${DBHost}" == "localhost" ) || ( "$DBTYPE" == "psql" && "${DBHost}" == "" ) ]]; then + [ "$DBTYPE" == "mysql" ] && searchstr="mysql" + [ "$DBTYPE" == "psql" ] && searchstr="postgres" + sock=$(netstat -axn | grep -m1 "$searchstr" | sed -r 's/^.*\s+([^ ]+)$/\1/') + if [[ ! -z "$sock" && -S $sock ]]; then DBSOCKET="$sock"; DBHOST=""; else DBHOST="${DBHost}"; fi + else + DBHOST="${DBHost}" + DBSOCKET="${DBSocket}" + fi + DBPORT="${DBPort}" - DBSOCKET="${DBSocket}" DBNAME="${DBName}" DBUSER="${DBUser}" DBPASS="${DBPassword}" +# Otherwise: set default values +else + DBHOST="$DEFAULT_DBHOST" + DBNAME="$DEFAULT_DBNAME" + DBUSER="$DEFAULT_DBUSER" + DBPASS="$DEFAULT_DBPASS" fi -# options specified explicitely override those from zabbix_server.conf (if any) +# Always set default ports, even if we read other parameters from zabbix_server.conf +[[ -z "$DBPORT" && "$DBTYPE" == "mysql" ]] && DBPORT="3306" +[[ -z "$DBPORT" && "$DBTYPE" == "psql" ]] && DBPORT="5432" + +# Options specified via command line override defaults or those from zabbix_server.conf (if any) [ ! -z "$ODBHOST" ] && DBHOST=$ODBHOST [ ! -z "$ODBPORT" ] && DBPORT=$ODBPORT [ ! -z "$ODBSOCKET" ] && DBSOCKET=$ODBSOCKET @@ -232,7 +267,6 @@ case $DBTYPE in [ ! -z "$DBSOCKET" ] && DB_OPTS=("${DB_OPTS[@]}" -S $DBSOCKET) [ ! -z "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -u $DBUSER) [ ! -z "$DBPASS" ] && DB_OPTS=("${DB_OPTS[@]}" -p"$DBPASS") - [ -z "$DBPORT" ] && DBPORT="3306" DB_OPTS=("${DB_OPTS[@]}" -P"$DBPORT") DB_OPTS_BATCH=("${DB_OPTS[@]}" --batch --silent) [ ! -z "$DBNAME" ] && DB_OPTS_BATCH=("${DB_OPTS_BATCH[@]}" -D $DBNAME) @@ -240,7 +274,6 @@ case $DBTYPE in psql) [ ! -z "$DBSOCKET" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBSOCKET) [ ! -z "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -U $DBUSER) - [ -z "$DBPORT" ] && DBPORT="5432" DB_OPTS=("${DB_OPTS[@]}" -p"$DBPORT") if [ ! -z "$DBPASS" ]; then export PGPASSFILE=$(mktemp -u) @@ -271,7 +304,10 @@ if [ "$QUIET" == "no" ]; then cat <<-EOF Configuration: - type: $DBTYPE - - host: $DBHOST ($DBHOSTNAME) +EOF +[ ! -z "$DBHOST" ] && echo " - host: $DBHOST ($DBHOSTNAME)" && echo " - port: $DBPORT" +[ ! -z "$DBSOCKET" ] && echo " - socket: $DBSOCKET" + cat <<-EOF - database: $DBNAME - user: $DBUSER - output: $DUMPDIR From b41330dccd873fb7e3867309c361b92708e4bbdf Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 15 Mar 2019 14:26:13 +0100 Subject: [PATCH 088/122] Only output config options that are set --- zabbix-dump | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index c65bf1a..f0f7dd1 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -305,14 +305,12 @@ if [ "$QUIET" == "no" ]; then Configuration: - type: $DBTYPE EOF -[ ! -z "$DBHOST" ] && echo " - host: $DBHOST ($DBHOSTNAME)" && echo " - port: $DBPORT" -[ ! -z "$DBSOCKET" ] && echo " - socket: $DBSOCKET" - cat <<-EOF - - database: $DBNAME - - user: $DBUSER - - output: $DUMPDIR - -EOF + [ ! -z "$MYSQL_CONFIG" ] && echo " - cfg file: $MYSQL_CONFIG" + [ ! -z "$DBHOST" ] && echo " - host: $DBHOST ($DBHOSTNAME)" && echo " - port: $DBPORT" + [ ! -z "$DBSOCKET" ] && echo " - socket: $DBSOCKET" + [ ! -z "$DBNAME" ] && echo " - database: $DBNAME" + [ ! -z "$DBUSER" ] && echo " - user: $DBUSER" + [ ! -z "$DUMPDIR" ] && echo " - output: $DUMPDIR" fi # From 5582142f680a88c946f414822faf7e6ffa3b2613 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 15 Mar 2019 14:26:54 +0100 Subject: [PATCH 089/122] Fix handling of MySQL option file: --defaults-extra-file must come first --- zabbix-dump | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zabbix-dump b/zabbix-dump index f0f7dd1..00de14b 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -260,7 +260,6 @@ fi SUFFIX=""; test ! -z $COMPRESSION && SUFFIX=".${COMPRESSION}" DB_OPTS=() -[ ! -z "$DBHOST" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBHOST) case $DBTYPE in mysql) [ ! -z "$MYSQL_CONFIG" ] && DB_OPTS=("${DB_OPTS[@]}" --defaults-extra-file="$MYSQL_CONFIG") @@ -284,6 +283,8 @@ case $DBTYPE in [ ! -z "$DBNAME" ] && DB_OPTS_BATCH=("${DB_OPTS_BATCH[@]}" -d $DBNAME) ;; esac +[ ! -z "$DBHOST" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBHOST) + # Log file for errors ERRORLOG=$(mktemp) From de7d90af8e41865a42a32524cd7acfb543d0d4ba Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 15 Mar 2019 14:28:35 +0100 Subject: [PATCH 090/122] Don't read Zabbix config or set defaults if MySQL options file was given --- zabbix-dump | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 00de14b..5b3fed4 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -189,6 +189,8 @@ while getopts ":c:d:H:o:p:P:r:s:t:u:z:0nqxZ" opt; do esac done +[ ! -z "$MYSQL_CONFIG" ] && READ_ZBX_CONFIG="no" + # (Try) reading database config from zabbix_server.conf if [[ "${READ_ZBX_CONFIG}" == "yes" && -f "${ZBX_CONFIG}" && -r "${ZBX_CONFIG}" ]]; then [ "$QUIET" == "no" ] && echo "Reading database options from ${ZBX_CONFIG}..." @@ -216,10 +218,13 @@ if [[ "${READ_ZBX_CONFIG}" == "yes" && -f "${ZBX_CONFIG}" && -r "${ZBX_CONFIG}" DBPASS="${DBPassword}" # Otherwise: set default values else - DBHOST="$DEFAULT_DBHOST" - DBNAME="$DEFAULT_DBNAME" - DBUSER="$DEFAULT_DBUSER" - DBPASS="$DEFAULT_DBPASS" + # if a MySQL config file is specified we assume it contains all connection parameters + if [ -z "$MYSQL_CONFIG" ]; then + DBHOST="$DEFAULT_DBHOST" + DBNAME="$DEFAULT_DBNAME" + DBUSER="$DEFAULT_DBUSER" + DBPASS="$DEFAULT_DBPASS" + fi fi # Always set default ports, even if we read other parameters from zabbix_server.conf @@ -254,6 +259,11 @@ if [ ! -z "$MYSQL_CONFIG" ]; then fi fi +if [ -z "$DBNAME" ]; then + echo "ERROR: Please specify a database name (option -d)" + exit 1 +fi + # # CONSTANTS # From 323937b40bdb8ad57cbe4acbd5dcb11107c4145f Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 15 Mar 2019 14:32:19 +0100 Subject: [PATCH 091/122] Don't set host from Zabbix config file if socket was specified manually --- zabbix-dump | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix-dump b/zabbix-dump index 5b3fed4..61d35d7 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -234,7 +234,7 @@ fi # Options specified via command line override defaults or those from zabbix_server.conf (if any) [ ! -z "$ODBHOST" ] && DBHOST=$ODBHOST [ ! -z "$ODBPORT" ] && DBPORT=$ODBPORT -[ ! -z "$ODBSOCKET" ] && DBSOCKET=$ODBSOCKET +[ ! -z "$ODBSOCKET" ] && DBSOCKET=$ODBSOCKET && DBHOST="" [ ! -z "$ODBNAME" ] && DBNAME=$ODBNAME [ ! -z "$ODBUSER" ] && DBUSER=$ODBUSER [ ! -z "$ODBPASS" ] && DBPASS=$ODBPASS From 13de513a45e605ea531542e118e2dd980e915588 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 15 Mar 2019 14:38:37 +0100 Subject: [PATCH 092/122] Show help about help options (not sure if this is useful) --- zabbix-dump | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/zabbix-dump b/zabbix-dump index 61d35d7..c17e6da 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -135,6 +135,10 @@ OPTIONS -q Quiet mode: no output except for errors (for batch/crontab use). + -h + --help + Show this help. + EXAMPLES # read $ZBX_CONFIG to backup local Zabbix server's # MySQL database into current directory From 6a0df23fe5718dc517d8fd5cfd5adb30e70f34ff Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 15 Mar 2019 15:15:38 +0100 Subject: [PATCH 093/122] Update texts to be posted (LinkedIn, XING) after a release --- RELEASE.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index 663ef06..b52538e 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -13,18 +13,18 @@ > **New version x.x.x of zabbix-mysql-dump** > - > zabbix-mysql-dump is a Linux bash script for backing up a Zabbix MySQL database.
- > Tables holding configuration data will be fully backed up while for mass data tables (events, history, trends, ...) only the table schema is stored. + > zabbix-dump is a Linux bash script for backing up the Zabbix configuration by saving MySQL or PostgreSQL database tables into a file.
+ > Tables holding configuration data will be fully backed up. For mass data tables (events, history, trends, ...) only the table schema is stored without any data (to keep the backup small). > > Overiew: https://github.com/maxhq/zabbix-backup/wiki
> Download: https://github.com/maxhq/zabbix-backup/releases/latest * Announce release at https://www.xing.com/communities/forums/100845147 - > **Neue Version x.x.x von zabbix-mysql-dump** + > **Neue Version x.x.x von zabbix-dump** > - > zabbix-mysql-dump ist ein Linux-Bash-Skript zum Backup der MySQL-Datenbank einer Zabbix-Installation.
- > Es sichert Konfigurationsdaten komplett, jedoch bei Tabellen mit Massendaten (Historie, Events, Trends) nur das "leere" Datenbankschema. + > zabbix-dump ist ein Linux-Bash-Skript zum Backup der Zabbix-Konfiguration durch Sicherung der MySQL- bzw. PostgreSQL-Datenbanktabellen in eine Datei.
+ > Es sichert Konfigurationsdaten komplett, bei Tabellen mit Massendaten (Historie, Events, Trends etc.) jedoch nur das "leere" Datenbankschema (um das Backup zu minimieren). > > Übersicht: https://github.com/maxhq/zabbix-backup/wiki
> Download: https://github.com/maxhq/zabbix-backup/releases/latest From dfef0ca8f14b831e452723ced19950fa40dc256e Mon Sep 17 00:00:00 2001 From: Tiago Cruz Date: Wed, 20 Mar 2019 19:56:21 -0300 Subject: [PATCH 094/122] missing $DBHOST to mysql client - error Cant connect to local MySQL server through socket --- zabbix-dump | 1 + 1 file changed, 1 insertion(+) diff --git a/zabbix-dump b/zabbix-dump index c17e6da..7ceb634 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -278,6 +278,7 @@ case $DBTYPE in mysql) [ ! -z "$MYSQL_CONFIG" ] && DB_OPTS=("${DB_OPTS[@]}" --defaults-extra-file="$MYSQL_CONFIG") [ ! -z "$DBSOCKET" ] && DB_OPTS=("${DB_OPTS[@]}" -S $DBSOCKET) + [ ! -z "$DBHOST" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBHOST) [ ! -z "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -u $DBUSER) [ ! -z "$DBPASS" ] && DB_OPTS=("${DB_OPTS[@]}" -p"$DBPASS") DB_OPTS=("${DB_OPTS[@]}" -P"$DBPORT") From 7a1f5c1d99d57a9490c369655d2749afb601cd3e Mon Sep 17 00:00:00 2001 From: Tiago Cruz Date: Thu, 21 Mar 2019 09:45:44 -0300 Subject: [PATCH 095/122] moved $DBHOST to psql case --- zabbix-dump | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix-dump b/zabbix-dump index 7ceb634..f8cee5c 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -287,6 +287,7 @@ case $DBTYPE in ;; psql) [ ! -z "$DBSOCKET" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBSOCKET) + [ ! -z "$DBHOST" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBHOST) [ ! -z "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -U $DBUSER) DB_OPTS=("${DB_OPTS[@]}" -p"$DBPORT") if [ ! -z "$DBPASS" ]; then @@ -298,7 +299,6 @@ case $DBTYPE in [ ! -z "$DBNAME" ] && DB_OPTS_BATCH=("${DB_OPTS_BATCH[@]}" -d $DBNAME) ;; esac -[ ! -z "$DBHOST" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBHOST) # Log file for errors ERRORLOG=$(mktemp) From 2d38d33270407ab73dbaabe4265a50ee87c25f23 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 21 Mar 2019 21:19:23 +0100 Subject: [PATCH 096/122] Bump version number and add release info --- README.md | 6 ++++-- zabbix-dump | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1715083..a4b89d7 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,10 @@ Please see the [Project Wiki](https://github.com/maxhq/zabbix-backup/wiki). ## Version history +**0.9.1 (2019-03-21)** + +- FIX: Correctly process hostname option -H (Tiago Cruz) + **0.9.0 (2019-03-14)** - NEW: Support for PostgreSQL databases (Sergey Galkin) @@ -26,8 +30,6 @@ Please see the [Project Wiki](https://github.com/maxhq/zabbix-backup/wiki). - CHG: Rename script to "zabbix-dump" - FIX: Support for whitespaces in database parameters (Greg Cockburn) - FIX: Support for backslashes in manually entered password (Greg Cockburn) -- - **0.8.2 (2016-09-08)** diff --git a/zabbix-dump b/zabbix-dump index f8cee5c..f9f147f 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -4,7 +4,7 @@ # zabbix-dump - Configuration Backup for Zabbix' MySQL or PostgreSQL data # # VERSION -# 0.9.0 +# 0.9.1 # # SYNOPSIS # This is a MySQL configuration backup script for Zabbix 1.x, 2.x, 3.x and 4.x. @@ -31,6 +31,7 @@ # - yangqi # - Johannes Petz (PetzJohannes) # - Wesley Schaft (wschaft) +# - Tiago Cruz (tiago-cruz-movile) # # AUTHOR # Jens Berthold (maxhq), 2019 From bfb962e51b38891333ed84b9e7263047841f962d Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 21 Mar 2019 21:38:26 +0100 Subject: [PATCH 097/122] Change title --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a4b89d7..c88d412 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Zabbix MySQL/PostgreSQL backup +# Backup script for Zabbix configuration data (MySQL/PostgreSQL) This is a MySQL/PostgreSQL database backup script for the [Zabbix](http://www.zabbix.com/) monitoring software from version 1.3.1 up to 4.0. From 8e8ce88eb5ca5786d61af7e18c1af3548286f33a Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 21 Mar 2019 22:05:02 +0100 Subject: [PATCH 098/122] Update release instructions --- RELEASE.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index b52538e..3389487 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -11,20 +11,18 @@ * Update http://zabbix.org/wiki/Docs/howto/mysql_backup_script * Announce release at https://www.linkedin.com/groups/161448 - > **New version x.x.x of zabbix-mysql-dump** + > **New version x.x.x of zabbix-dump** > - > zabbix-dump is a Linux bash script for backing up the Zabbix configuration by saving MySQL or PostgreSQL database tables into a file.
+ > zabbix-dump is a Linux bash script for backing up the Zabbix configuration by saving MySQL or PostgreSQL database tables into a compressed file. > Tables holding configuration data will be fully backed up. For mass data tables (events, history, trends, ...) only the table schema is stored without any data (to keep the backup small). > - > Overiew: https://github.com/maxhq/zabbix-backup/wiki
- > Download: https://github.com/maxhq/zabbix-backup/releases/latest + > Overiew: https://github.com/maxhq/zabbix-backup/blob/master/README.md * Announce release at https://www.xing.com/communities/forums/100845147 > **Neue Version x.x.x von zabbix-dump** > - > zabbix-dump ist ein Linux-Bash-Skript zum Backup der Zabbix-Konfiguration durch Sicherung der MySQL- bzw. PostgreSQL-Datenbanktabellen in eine Datei.
+ > zabbix-dump ist ein Linux-Bash-Skript zum Backup der Zabbix-Konfiguration durch Sicherung der MySQL- bzw. PostgreSQL-Datenbanktabellen in eine komprimierte Datei. > Es sichert Konfigurationsdaten komplett, bei Tabellen mit Massendaten (Historie, Events, Trends etc.) jedoch nur das "leere" Datenbankschema (um das Backup zu minimieren). > - > Übersicht: https://github.com/maxhq/zabbix-backup/wiki
- > Download: https://github.com/maxhq/zabbix-backup/releases/latest + > Übersicht: https://github.com/maxhq/zabbix-backup/blob/master/README.md From 1eff0a9d9402b5fd6fac0162b4d48dab413b0308 Mon Sep 17 00:00:00 2001 From: fossabot Date: Fri, 12 Jul 2019 02:12:57 -0700 Subject: [PATCH 099/122] Add license scan report and status Signed-off-by: fossabot --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index c88d412..eb56e0d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ # Backup script for Zabbix configuration data (MySQL/PostgreSQL) +[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmaxhq%2Fzabbix-backup.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmaxhq%2Fzabbix-backup?ref=badge_shield) + This is a MySQL/PostgreSQL database backup script for the [Zabbix](http://www.zabbix.com/) monitoring software from version 1.3.1 up to 4.0. @@ -80,3 +82,7 @@ Please see the [Project Wiki](https://github.com/maxhq/zabbix-backup/wiki). variables (DATEBIN etc) for commands that use to be in $PATH **0.2.0 (2011-11-05)** + + +## License +[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmaxhq%2Fzabbix-backup.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmaxhq%2Fzabbix-backup?ref=badge_large) \ No newline at end of file From 1a35479c06684c7b4adb62ceb0c1cfcf06fb6439 Mon Sep 17 00:00:00 2001 From: Mario Trangoni Date: Fri, 9 Aug 2019 15:55:42 +0200 Subject: [PATCH 100/122] Fix shellcheck SC2236: Use -n instead of ! -z. Signed-off-by: Mario Trangoni --- zabbix-dump | 54 ++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index f9f147f..25de438 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -194,7 +194,7 @@ while getopts ":c:d:H:o:p:P:r:s:t:u:z:0nqxZ" opt; do esac done -[ ! -z "$MYSQL_CONFIG" ] && READ_ZBX_CONFIG="no" +[ -n "$MYSQL_CONFIG" ] && READ_ZBX_CONFIG="no" # (Try) reading database config from zabbix_server.conf if [[ "${READ_ZBX_CONFIG}" == "yes" && -f "${ZBX_CONFIG}" && -r "${ZBX_CONFIG}" ]]; then @@ -211,7 +211,7 @@ if [[ "${READ_ZBX_CONFIG}" == "yes" && -f "${ZBX_CONFIG}" && -r "${ZBX_CONFIG}" [ "$DBTYPE" == "mysql" ] && searchstr="mysql" [ "$DBTYPE" == "psql" ] && searchstr="postgres" sock=$(netstat -axn | grep -m1 "$searchstr" | sed -r 's/^.*\s+([^ ]+)$/\1/') - if [[ ! -z "$sock" && -S $sock ]]; then DBSOCKET="$sock"; DBHOST=""; else DBHOST="${DBHost}"; fi + if [[ -n "$sock" && -S $sock ]]; then DBSOCKET="$sock"; DBHOST=""; else DBHOST="${DBHost}"; fi else DBHOST="${DBHost}" DBSOCKET="${DBSocket}" @@ -237,12 +237,12 @@ fi [[ -z "$DBPORT" && "$DBTYPE" == "psql" ]] && DBPORT="5432" # Options specified via command line override defaults or those from zabbix_server.conf (if any) -[ ! -z "$ODBHOST" ] && DBHOST=$ODBHOST -[ ! -z "$ODBPORT" ] && DBPORT=$ODBPORT -[ ! -z "$ODBSOCKET" ] && DBSOCKET=$ODBSOCKET && DBHOST="" -[ ! -z "$ODBNAME" ] && DBNAME=$ODBNAME -[ ! -z "$ODBUSER" ] && DBUSER=$ODBUSER -[ ! -z "$ODBPASS" ] && DBPASS=$ODBPASS +[ -n "$ODBHOST" ] && DBHOST=$ODBHOST +[ -n "$ODBPORT" ] && DBPORT=$ODBPORT +[ -n "$ODBSOCKET" ] && DBSOCKET=$ODBSOCKET && DBHOST="" +[ -n "$ODBNAME" ] && DBNAME=$ODBNAME +[ -n "$ODBUSER" ] && DBUSER=$ODBUSER +[ -n "$ODBPASS" ] && DBPASS=$ODBPASS # Password prompt if [ "$DBPASS" = "-" ]; then @@ -251,7 +251,7 @@ if [ "$DBPASS" = "-" ]; then fi # MySQL config file validations -if [ ! -z "$MYSQL_CONFIG" ]; then +if [ -n "$MYSQL_CONFIG" ]; then if [ ! -r "$MYSQL_CONFIG" ]; then echo "ERROR: Cannot read configuration file $MYSQL_CONFIG" >&2 exit 1 @@ -277,27 +277,27 @@ SUFFIX=""; test ! -z $COMPRESSION && SUFFIX=".${COMPRESSION}" DB_OPTS=() case $DBTYPE in mysql) - [ ! -z "$MYSQL_CONFIG" ] && DB_OPTS=("${DB_OPTS[@]}" --defaults-extra-file="$MYSQL_CONFIG") - [ ! -z "$DBSOCKET" ] && DB_OPTS=("${DB_OPTS[@]}" -S $DBSOCKET) - [ ! -z "$DBHOST" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBHOST) - [ ! -z "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -u $DBUSER) - [ ! -z "$DBPASS" ] && DB_OPTS=("${DB_OPTS[@]}" -p"$DBPASS") + [ -n "$MYSQL_CONFIG" ] && DB_OPTS=("${DB_OPTS[@]}" --defaults-extra-file="$MYSQL_CONFIG") + [ -n "$DBSOCKET" ] && DB_OPTS=("${DB_OPTS[@]}" -S $DBSOCKET) + [ -n "$DBHOST" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBHOST) + [ -n "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -u $DBUSER) + [ -n "$DBPASS" ] && DB_OPTS=("${DB_OPTS[@]}" -p"$DBPASS") DB_OPTS=("${DB_OPTS[@]}" -P"$DBPORT") DB_OPTS_BATCH=("${DB_OPTS[@]}" --batch --silent) - [ ! -z "$DBNAME" ] && DB_OPTS_BATCH=("${DB_OPTS_BATCH[@]}" -D $DBNAME) + [ -n "$DBNAME" ] && DB_OPTS_BATCH=("${DB_OPTS_BATCH[@]}" -D $DBNAME) ;; psql) - [ ! -z "$DBSOCKET" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBSOCKET) - [ ! -z "$DBHOST" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBHOST) - [ ! -z "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -U $DBUSER) + [ -n "$DBSOCKET" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBSOCKET) + [ -n "$DBHOST" ] && DB_OPTS=("${DB_OPTS[@]}" -h $DBHOST) + [ -n "$DBUSER" ] && DB_OPTS=("${DB_OPTS[@]}" -U $DBUSER) DB_OPTS=("${DB_OPTS[@]}" -p"$DBPORT") - if [ ! -z "$DBPASS" ]; then + if [ -n "$DBPASS" ]; then export PGPASSFILE=$(mktemp -u) echo "$DBHOST:$DBPORT:$DBNAME:$DBUSER:$DBPASS" > $PGPASSFILE chmod 600 $PGPASSFILE fi DB_OPTS_BATCH=("${DB_OPTS[@]}" -Atw) - [ ! -z "$DBNAME" ] && DB_OPTS_BATCH=("${DB_OPTS_BATCH[@]}" -d $DBNAME) + [ -n "$DBNAME" ] && DB_OPTS_BATCH=("${DB_OPTS_BATCH[@]}" -d $DBNAME) ;; esac @@ -308,7 +308,7 @@ ERRORLOG=$(mktemp) DBHOSTNAME="$DBHOST" command -v dig >/dev/null 2>&1 FIND_DIG=$? -if [ "$REVERSELOOKUP" == "yes" ] && [ $FIND_DIG -eq 0 ] && [ ! -z "$DBHOST" ]; then +if [ "$REVERSELOOKUP" == "yes" ] && [ $FIND_DIG -eq 0 ] && [ -n "$DBHOST" ]; then # Try resolving a given host ip newHostname=$(dig +noall +answer -x "${DBHOST}" | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') test \! -z "$newHostname" && DBHOSTNAME="$newHostname" @@ -322,12 +322,12 @@ if [ "$QUIET" == "no" ]; then Configuration: - type: $DBTYPE EOF - [ ! -z "$MYSQL_CONFIG" ] && echo " - cfg file: $MYSQL_CONFIG" - [ ! -z "$DBHOST" ] && echo " - host: $DBHOST ($DBHOSTNAME)" && echo " - port: $DBPORT" - [ ! -z "$DBSOCKET" ] && echo " - socket: $DBSOCKET" - [ ! -z "$DBNAME" ] && echo " - database: $DBNAME" - [ ! -z "$DBUSER" ] && echo " - user: $DBUSER" - [ ! -z "$DUMPDIR" ] && echo " - output: $DUMPDIR" + [ -n "$MYSQL_CONFIG" ] && echo " - cfg file: $MYSQL_CONFIG" + [ -n "$DBHOST" ] && echo " - host: $DBHOST ($DBHOSTNAME)" && echo " - port: $DBPORT" + [ -n "$DBSOCKET" ] && echo " - socket: $DBSOCKET" + [ -n "$DBNAME" ] && echo " - database: $DBNAME" + [ -n "$DBUSER" ] && echo " - user: $DBUSER" + [ -n "$DUMPDIR" ] && echo " - output: $DUMPDIR" fi # From 9bd379c7122a9bd3df29255ae9275f7d2c2320c4 Mon Sep 17 00:00:00 2001 From: Mario Trangoni Date: Fri, 9 Aug 2019 15:58:28 +0200 Subject: [PATCH 101/122] Fix shellcheck SC2162: read without -r will mangle backslashes Signed-off-by: Mario Trangoni --- zabbix-dump | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 25de438..7f72dee 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -443,7 +443,7 @@ mkdir -p "${DUMPDIR}" [ "$QUIET" == "no" ] && echo "Starting table backups..." case $DBTYPE in mysql) - while read table; do + while read -r table; do # large data tables: only store schema if elementIn "$table" "${DATA_TABLES[@]}"; then dump_opt="--no-data" @@ -472,7 +472,7 @@ case $DBTYPE in done <<<"$DB_TABLES" ;; psql) - while read table; do + while read -r table; do if elementIn "$table" "${DATA_TABLES[@]}"; then dump_opt="--exclude-table-data=$table" PROCESSED_DATA_TABLES+=($dump_opt) From c52320cc8d0af861b25521788975cdae1c3e49c1 Mon Sep 17 00:00:00 2001 From: ironbishop Date: Tue, 17 Sep 2019 13:45:34 +0200 Subject: [PATCH 102/122] Escape special characters while reading password While reading from "${ZBX_CONFIG}", `DBPassword` can contain special characters, which are interpreted by the source command. Sourcing the (php) configuration file, in such situation can lead to backup failure. --- zabbix-dump | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index f9f147f..aa506f3 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -216,11 +216,13 @@ if [[ "${READ_ZBX_CONFIG}" == "yes" && -f "${ZBX_CONFIG}" && -r "${ZBX_CONFIG}" DBHOST="${DBHost}" DBSOCKET="${DBSocket}" fi - + DBPORT="${DBPort}" DBNAME="${DBName}" DBUSER="${DBUser}" - DBPASS="${DBPassword}" + # Rereading password with awk, to avoid shell special characters execution. + DBPASS="`/usr/bin/awk -F'=' '/^DBPassword/{ print $2 }' "${ZBX_CONFIG}"`" + # Otherwise: set default values else # if a MySQL config file is specified we assume it contains all connection parameters From 0d79386bbcbeb696e8a3cb4371726eeeb2055b64 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 16 Jan 2020 13:01:33 +0100 Subject: [PATCH 103/122] Don't source (PHP) Zabbix config to prevent possible escaping issues Pointed out by @ironbishop in PR #19 --- zabbix-dump | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index aa506f3..1ce21a6 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -199,29 +199,28 @@ done # (Try) reading database config from zabbix_server.conf if [[ "${READ_ZBX_CONFIG}" == "yes" && -f "${ZBX_CONFIG}" && -r "${ZBX_CONFIG}" ]]; then [ "$QUIET" == "no" ] && echo "Reading database options from ${ZBX_CONFIG}..." - source "${ZBX_CONFIG}" + + # Reading config with awk (instead of source'ing the file) to avoid shell special characters execution + DBHOST="$(/usr/bin/awk -F'=' '/^DBHost/{ print $2 }' "${ZBX_CONFIG}")" + DBPORT="$(/usr/bin/awk -F'=' '/^DBPort/{ print $2 }' "${ZBX_CONFIG}")" + DBNAME="$(/usr/bin/awk -F'=' '/^DBName/{ print $2 }' "${ZBX_CONFIG}")" + DBUSER="$(/usr/bin/awk -F'=' '/^DBUser/{ print $2 }' "${ZBX_CONFIG}")" + DBPASS="$(/usr/bin/awk -F'=' '/^DBPassword/{ print $2 }' "${ZBX_CONFIG}")" # set non-existing variables to their Zabbix defaults (if they are non-empty string) - [ -z ${DBHost+x} ] && DBHost="localhost" + [ -z ${DBHOST+x} ] && DBHOST="localhost" # Zabbix config has a special treatment for DBHost: # > If set to localhost, socket is used for MySQL. # > If set to empty string, socket is used for PostgreSQL. - if [[ ( "$DBTYPE" == "mysql" && "${DBHost}" == "localhost" ) || ( "$DBTYPE" == "psql" && "${DBHost}" == "" ) ]]; then + if [[ ( "$DBTYPE" == "mysql" && "$DBHOST" == "localhost" ) || ( "$DBTYPE" == "psql" && "$DBHOST" == "" ) ]]; then [ "$DBTYPE" == "mysql" ] && searchstr="mysql" [ "$DBTYPE" == "psql" ] && searchstr="postgres" sock=$(netstat -axn | grep -m1 "$searchstr" | sed -r 's/^.*\s+([^ ]+)$/\1/') - if [[ ! -z "$sock" && -S $sock ]]; then DBSOCKET="$sock"; DBHOST=""; else DBHOST="${DBHost}"; fi + if [[ ! -z "$sock" && -S $sock ]]; then DBSOCKET="$sock"; DBHOST=""; fi else - DBHOST="${DBHost}" - DBSOCKET="${DBSocket}" + DBSOCKET="$(/usr/bin/awk -F'=' '/^DBSocket/{ print $2 }' "${ZBX_CONFIG}")" fi - - DBPORT="${DBPort}" - DBNAME="${DBName}" - DBUSER="${DBUser}" - # Rereading password with awk, to avoid shell special characters execution. - DBPASS="`/usr/bin/awk -F'=' '/^DBPassword/{ print $2 }' "${ZBX_CONFIG}"`" # Otherwise: set default values else @@ -325,11 +324,11 @@ Configuration: - type: $DBTYPE EOF [ ! -z "$MYSQL_CONFIG" ] && echo " - cfg file: $MYSQL_CONFIG" - [ ! -z "$DBHOST" ] && echo " - host: $DBHOST ($DBHOSTNAME)" && echo " - port: $DBPORT" - [ ! -z "$DBSOCKET" ] && echo " - socket: $DBSOCKET" - [ ! -z "$DBNAME" ] && echo " - database: $DBNAME" - [ ! -z "$DBUSER" ] && echo " - user: $DBUSER" - [ ! -z "$DUMPDIR" ] && echo " - output: $DUMPDIR" + [ ! -z "$DBHOST" ] && echo " - host: $DBHOST ($DBHOSTNAME)" && echo " - port: $DBPORT" + [ ! -z "$DBSOCKET" ] && echo " - socket: $DBSOCKET" + [ ! -z "$DBNAME" ] && echo " - database: $DBNAME" + [ ! -z "$DBUSER" ] && echo " - user: $DBUSER" + [ ! -z "$DUMPDIR" ] && echo " - output: $DUMPDIR" fi # From c6e7d93f8d78b15a23ab699e88f79480cc2306c8 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 16 Jan 2020 13:08:43 +0100 Subject: [PATCH 104/122] Fix accidentally disabled cleanup of postgresql password file --- zabbix-dump | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix-dump b/zabbix-dump index 1ce21a6..b2ab191 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -355,7 +355,7 @@ check_binary() { } clean_psql_pass() { if [ $DBTYPE = "psql" -a -n "$PGPASSFILE" ]; then - echo rm -f $PGPASSFILE + rm -f $PGPASSFILE fi } From c592a3ac4c65076e8981f1b9e32288ac7413fb63 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 16 Jan 2020 13:38:10 +0100 Subject: [PATCH 105/122] Revert "Add license scan report and status" This reverts commit 1eff0a9d9402b5fd6fac0162b4d48dab413b0308. --- README.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/README.md b/README.md index eb56e0d..c88d412 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,4 @@ # Backup script for Zabbix configuration data (MySQL/PostgreSQL) -[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmaxhq%2Fzabbix-backup.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmaxhq%2Fzabbix-backup?ref=badge_shield) - This is a MySQL/PostgreSQL database backup script for the [Zabbix](http://www.zabbix.com/) monitoring software from version 1.3.1 up to 4.0. @@ -82,7 +80,3 @@ Please see the [Project Wiki](https://github.com/maxhq/zabbix-backup/wiki). variables (DATEBIN etc) for commands that use to be in $PATH **0.2.0 (2011-11-05)** - - -## License -[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmaxhq%2Fzabbix-backup.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmaxhq%2Fzabbix-backup?ref=badge_large) \ No newline at end of file From ee6a2f8aa5dcc32db0b5dee56434a5eddec2c8a0 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 16 Jan 2020 15:45:42 +0100 Subject: [PATCH 106/122] Fix get-table-list.pl to work with Git repository --- get-table-list.pl | 78 ++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 32 deletions(-) diff --git a/get-table-list.pl b/get-table-list.pl index 4fe583e..ba88d6c 100755 --- a/get-table-list.pl +++ b/get-table-list.pl @@ -1,11 +1,11 @@ #!/usr/bin/env perl # NAME -# get-table-list.pl - List current and historic Zabbix database tables +# get-table-list.pl - List current and historic Zabbix database tables # # SYNOPSIS # This is mainly a helper script for developing the backup script. # -# It connects to svn://svn.zabbix.com (using Subversion client "svn") and +# It connects to svn://svn.zabbix.com (using Subversion client "svn") and # fetches the schema definitions of all tagged Zabbix versions beginning # from 1.3.1 (this takes a while). # @@ -16,28 +16,30 @@ # v0.1 - 2014-09-19 First version # # AUTHOR -# Jens Berthold (maxhq), 2014 +# Jens Berthold (maxhq), 2020 +use strict; +use warnings; -use File::Temp qw/ tempfile /; +use version; +my $REPO = 'https://git.zabbix.com/scm/zbx/zabbix.git'; +my $REPO_WEB = 'https://git.zabbix.com/projects/ZBX/repos/zabbix/raw'; my $tables = {}; # for each table, create a list of Zabbix versions that know it -my (undef, $tmpfile) = tempfile(); sub stop { my ($msg) = @_; print "ERROR: $msg\n"; - unlink $tmpfile; exit; } # sort version numbers correctly sub cmpver { my ($a, $b) = @_; - + # split version parts: 1.2.3rc1 --> 1 2 3rc1 my @a_parts = split /\./, $a; my @b_parts = split /\./, $b; - + for (my $i=0; $i= scalar(@b_parts); # split number parts: 3rc1 --> 3 rc 1 @@ -50,9 +52,9 @@ sub cmpver { return -1 if $a_type and not $b_type; return 1 if not $a_type and $b_type; # a1 before b1 - $cmp = $a_type cmp $b_type; return $cmp unless $cmp == 0; + $cmp = ($a_type//"") cmp ($b_type//""); return $cmp unless $cmp == 0; # rc1 before rc2 - $cmp = $a_idx <=> $b_idx; return $cmp unless $cmp == 0; + $cmp = ($a_idx//0) <=> ($b_idx//0); return $cmp unless $cmp == 0; } # 1.2 before 1.2.1 return -1 if scalar(@a_parts) < scalar(@b_parts); @@ -60,28 +62,43 @@ sub cmpver { return 0; } -# Check for subversion client -my $svn = `which svn`; -stop("No subversion client found") unless $svn; +# Check for Git client +`which git` or stop("No Git client found"); + +# Get tag list from repo: +# 7f6b20903537b9bbf72fe2b75ab7fac557856aad refs/tags/1.0 +# 693709cc4a80777f7759856c853b38cbc920f068 refs/tags/1.1 +print "Querying existing tags from $REPO...\n"; +my @tags_raw = `git ls-remote -t $REPO`; +# remove trailing newline +chomp @tags_raw; +# skip release candidates, betas and tags like "zabicom-xxx" +@tags_raw = grep { m{ refs/tags/ \d+ \. \d+ ( \. \d+ )? $}x } @tags_raw; -# List tags from subversion repo -my @tags = `svn ls 'svn://svn.zabbix.com/tags'`; -chomp @tags; # remove trailing newline -@tags = (map { $_ =~ s/\/$//; $_ } @tags); # remove trailing slash -@tags = sort { cmpver($a,$b) } @tags; +# Create HashRef: +# 1.0 => 7f6b20903537b9bbf72fe2b75ab7fac557856aad +# 1.1 => 693709cc4a80777f7759856c853b38cbc920f068 +my $tags = { map { m{^ (\w+) \s+ refs/tags/ (.*) $}x; $2 => $1 } @tags_raw }; -for my $tag (@tags) { +# Loop over tags and read table schema +print "Reading table schemas...\n"; +for my $tag (sort { cmpver($a,$b) } keys %$tags) { next if cmpver($tag, "1.3.1") < 0; # before Zabbix 1.3.1, schema was stored as pure SQL my $schema; my $subdir; - printf "%-10s %s", $tag, "Searching schema..."; + printf " - %-8s %s", $tag, "Looking for schema..."; # search in subdir /schema (<= 1.9.8) and /src for schema.(sql|tmpl) for my $sub (qw(schema src)) { - my @files = `svn ls 'svn://svn.zabbix.com/tags/$tag/create/$sub' 2>/dev/null`; - next unless @files; # directory not found? - chomp @files; + # file list: + # 100644 blob 1f0a05eb826dfcdb26f9429ad30c720454374ca1 data.tmpl + # 100644 blob b98b5eecc62731508c09d9e76d7aed9d4eb201f2 schema.tmpl + my @files_raw = `curl -s $REPO_WEB/create/$sub?at=refs%2Ftags%2F$tag`; + next unless @files_raw; # directory not found? + chomp @files_raw; # remove trailing newline + my @files = map { /^ \d+ \s+ \w+ \s+ \w+ \s+ (.*) $/x; $1 } @files_raw; + ($schema) = grep /^schema\.(sql|tmpl)/, @files; $subdir = $sub; last; @@ -90,10 +107,9 @@ sub cmpver { print "\nNo schema found in tag $tag\n"; next; } - print " ($schema) Download... "; - system("svn --force export svn://svn.zabbix.com/tags/$tag/create/$subdir/$schema $tmpfile >/dev/null"); - open my $fh, '<', $tmpfile or stop("Couldn't open temp file: $!"); - while (<$fh>) { + print " Processing ($schema)... "; + my @table = `curl -s $REPO_WEB/create/$subdir/$schema?at=refs%2Ftags%2F$tag`; + for (@table) { chomp; next unless m/^TABLE/; my (undef, $table) = split /\|/; @@ -103,14 +119,12 @@ sub cmpver { print " Done\n"; } -unlink $tmpfile; - # # Print out results # print "\n\n"; -print "TABLE FIRST USE LAST USE\n"; -print "-------------------------------------------------\n"; +print "TABLE FIRST USE LAST USE\n"; +print "------------------------------------------------\n"; for my $tab (sort keys %$tables) { - printf "%-25s %-8s - %s\n", $tab, $tables->{$tab}->[0], $tables->{$tab}->[-1]; + printf "%-26s %-8s - %s\n", $tab, $tables->{$tab}->[0], $tables->{$tab}->[-1]; } From 9d093de64119f0a00e62b203917b3872988a1414 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 16 Jan 2020 16:00:45 +0100 Subject: [PATCH 107/122] get-table-list.pl: merge zabbix-dump's existing table config into output --- get-table-list.pl | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/get-table-list.pl b/get-table-list.pl index ba88d6c..52a40a8 100755 --- a/get-table-list.pl +++ b/get-table-list.pl @@ -24,7 +24,8 @@ my $REPO = 'https://git.zabbix.com/scm/zbx/zabbix.git'; my $REPO_WEB = 'https://git.zabbix.com/projects/ZBX/repos/zabbix/raw'; -my $tables = {}; # for each table, create a list of Zabbix versions that know it +my $tabinfo = {}; # for each table, create a list of Zabbix versions that know it +my $tabinfo_old = {}; # old data from zabbix_dump sub stop { my ($msg) = @_; @@ -62,6 +63,22 @@ sub cmpver { return 0; } +# Read old table informations from zabbix-dump +open my $fh, '<', './zabbix-dump' or stop("Couldn't find 'zabbix-dump': $!"); +my $within_data_section = 0; +while (<$fh>) { + chomp; + if (/^__DATA__/) { $within_data_section = 1; next } + next unless $within_data_section; + my ($table, $from, undef, $to, $mode) = split /\s+/; + + $tabinfo_old->{$table} = { + from => $from, + to => $to, + schema_only => ($mode//"") eq "SCHEMAONLY" ? 1 : 0, + }; +} + # Check for Git client `which git` or stop("No Git client found"); @@ -113,8 +130,8 @@ sub cmpver { chomp; next unless m/^TABLE/; my (undef, $table) = split /\|/; - $tables->{$table} //= []; - push @{$tables->{$table}}, $tag; + $tabinfo->{$table} //= []; + push @{$tabinfo->{$table}}, $tag; } print " Done\n"; } @@ -123,8 +140,11 @@ sub cmpver { # Print out results # print "\n\n"; -print "TABLE FIRST USE LAST USE\n"; -print "------------------------------------------------\n"; -for my $tab (sort keys %$tables) { - printf "%-26s %-8s - %s\n", $tab, $tables->{$tab}->[0], $tables->{$tab}->[-1]; +print "TABLE FIRST USE LAST USE MODE\n"; +print "----------------------------------------------------\n"; +for my $tab (sort keys %$tabinfo) { + my $mode = $tabinfo_old->{$tab} + ? ($tabinfo_old->{$tab}->{schema_only} ? ' SCHEMAONLY' : '') + : ' <-- NEW TABLE! Only store schema?'; + printf "%-26s %-8s - %-8s%s\n", $tab, $tabinfo->{$tab}->[0], $tabinfo->{$tab}->[-1], $mode; } From c992748cf580d30caec725c453f834001c66852e Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 16 Jan 2020 16:09:59 +0100 Subject: [PATCH 108/122] get-table-list.pl: move some code into subroutines --- get-table-list.pl | 83 ++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/get-table-list.pl b/get-table-list.pl index 52a40a8..852f108 100755 --- a/get-table-list.pl +++ b/get-table-list.pl @@ -24,8 +24,6 @@ my $REPO = 'https://git.zabbix.com/scm/zbx/zabbix.git'; my $REPO_WEB = 'https://git.zabbix.com/projects/ZBX/repos/zabbix/raw'; -my $tabinfo = {}; # for each table, create a list of Zabbix versions that know it -my $tabinfo_old = {}; # old data from zabbix_dump sub stop { my ($msg) = @_; @@ -63,47 +61,66 @@ sub cmpver { return 0; } -# Read old table informations from zabbix-dump -open my $fh, '<', './zabbix-dump' or stop("Couldn't find 'zabbix-dump': $!"); -my $within_data_section = 0; -while (<$fh>) { - chomp; - if (/^__DATA__/) { $within_data_section = 1; next } - next unless $within_data_section; - my ($table, $from, undef, $to, $mode) = split /\s+/; - - $tabinfo_old->{$table} = { - from => $from, - to => $to, - schema_only => ($mode//"") eq "SCHEMAONLY" ? 1 : 0, +# Get tag list from repo: +sub get_taglist { + print "Querying existing tags from $REPO...\n"; + + # Returned format: + # 7f6b20903537b9bbf72fe2b75ab7fac557856aad refs/tags/1.0 + # 693709cc4a80777f7759856c853b38cbc920f068 refs/tags/1.1 + my @tags_raw = `git ls-remote -t $REPO`; + # remove trailing newline + chomp @tags_raw; + # skip release candidates, betas and tags like "zabicom-xxx" + @tags_raw = grep { m{ refs/tags/ \d+ \. \d+ ( \. \d+ )? $}x } @tags_raw; + + # Create HashRef: + # 1.0 => 7f6b20903537b9bbf72fe2b75ab7fac557856aad + # 1.1 => 693709cc4a80777f7759856c853b38cbc920f068 + return { + map { m{^ (\w+) \s+ refs/tags/ (.*) $}x; $2 => $1 } @tags_raw }; } -# Check for Git client +# Read old table informations from zabbix-dump +sub get_old_tabinfo { + my $tabinfo_old = {}; + + open my $fh, '<', './zabbix-dump' or stop("Couldn't find 'zabbix-dump': $!"); + + my $within_data_section = 0; + while (<$fh>) { + chomp; + if (/^__DATA__/) { $within_data_section = 1; next } + next unless $within_data_section; + my ($table, $from, undef, $to, $mode) = split /\s+/; + + $tabinfo_old->{$table} = { + from => $from, + to => $to, + schema_only => ($mode//"") eq "SCHEMAONLY" ? 1 : 0, + }; + } + + return $tabinfo_old; +} + + + `which git` or stop("No Git client found"); -# Get tag list from repo: -# 7f6b20903537b9bbf72fe2b75ab7fac557856aad refs/tags/1.0 -# 693709cc4a80777f7759856c853b38cbc920f068 refs/tags/1.1 -print "Querying existing tags from $REPO...\n"; -my @tags_raw = `git ls-remote -t $REPO`; -# remove trailing newline -chomp @tags_raw; -# skip release candidates, betas and tags like "zabicom-xxx" -@tags_raw = grep { m{ refs/tags/ \d+ \. \d+ ( \. \d+ )? $}x } @tags_raw; - -# Create HashRef: -# 1.0 => 7f6b20903537b9bbf72fe2b75ab7fac557856aad -# 1.1 => 693709cc4a80777f7759856c853b38cbc920f068 -my $tags = { map { m{^ (\w+) \s+ refs/tags/ (.*) $}x; $2 => $1 } @tags_raw }; +my $tabinfo_old = get_old_tabinfo(); # old data from zabbix_dump + +my $tags = get_taglist(); +my $tabinfo = {}; # for each table, create a list of Zabbix versions that know it -# Loop over tags and read table schema print "Reading table schemas...\n"; + +# Loop over tags and read table schema for my $tag (sort { cmpver($a,$b) } keys %$tags) { next if cmpver($tag, "1.3.1") < 0; # before Zabbix 1.3.1, schema was stored as pure SQL - my $schema; - my $subdir; + my ($schema, $subdir); printf " - %-8s %s", $tag, "Looking for schema..."; # search in subdir /schema (<= 1.9.8) and /src for schema.(sql|tmpl) From 32103283f2d5040433398e7871b5294ffb44b07b Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 16 Jan 2020 16:18:33 +0100 Subject: [PATCH 109/122] Update table list to Zabbix 4.4.4 --- zabbix-dump | 305 ++++++++++++++++++++++++++-------------------------- 1 file changed, 155 insertions(+), 150 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index f9f147f..8489541 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -538,161 +538,166 @@ exit 0 # __DATA__ -acknowledges 1.3.1 - 4.0.0 SCHEMAONLY -actions 1.3.1 - 4.0.0 -alerts 1.3.1 - 4.0.0 SCHEMAONLY -application_discovery 2.5.0 - 4.0.0 -application_prototype 2.5.0 - 4.0.0 -application_template 2.1.0 - 4.0.0 -applications 1.3.1 - 4.0.0 -auditlog 1.3.1 - 4.0.0 SCHEMAONLY -auditlog_details 1.7 - 4.0.0 SCHEMAONLY +acknowledges 1.3.1 - 4.4.4 SCHEMAONLY +actions 1.3.1 - 4.4.4 +alerts 1.3.1 - 4.4.4 SCHEMAONLY +application_discovery 2.5.0 - 4.4.4 +application_prototype 2.5.0 - 4.4.4 +application_template 2.1.0 - 4.4.4 +applications 1.3.1 - 4.4.4 +auditlog 1.3.1 - 4.4.4 SCHEMAONLY +auditlog_details 1.7 - 4.4.4 SCHEMAONLY autoreg 1.3.1 - 1.3.4 -autoreg_host 1.7 - 4.0.0 -conditions 1.3.1 - 4.0.0 -config 1.3.1 - 4.0.0 -corr_condition 3.2.0 - 4.0.0 -corr_condition_group 3.2.0 - 4.0.0 -corr_condition_tag 3.2.0 - 4.0.0 -corr_condition_tagpair 3.2.0 - 4.0.0 -corr_condition_tagvalue 3.2.0 - 4.0.0 -corr_operation 3.2.0 - 4.0.0 -correlation 3.2.0 - 4.0.0 -dashboard 3.4.0 - 4.0.0 -dashboard_user 3.4.0 - 4.0.0 -dashboard_usrgrp 3.4.0 - 4.0.0 -dbversion 2.1.0 - 4.0.0 -dchecks 1.3.4 - 4.0.0 -dhosts 1.3.4 - 4.0.0 -drules 1.3.4 - 4.0.0 -dservices 1.3.4 - 4.0.0 -escalations 1.5.3 - 4.0.0 -event_recovery 3.2.0 - 4.0.0 SCHEMAONLY -event_suppress 4.0.0 - 4.0.0 SCHEMAONLY -event_tag 3.2.0 - 4.0.0 SCHEMAONLY -events 1.3.1 - 4.0.0 SCHEMAONLY -expressions 1.7 - 4.0.0 -functions 1.3.1 - 4.0.0 -globalmacro 1.7 - 4.0.0 -globalvars 1.9.6 - 4.0.0 -graph_discovery 1.9.0 - 4.0.0 -graph_theme 1.7 - 4.0.0 -graphs 1.3.1 - 4.0.0 -graphs_items 1.3.1 - 4.0.0 -group_discovery 2.1.4 - 4.0.0 -group_prototype 2.1.4 - 4.0.0 -groups 1.3.1 - 3.4.1 +autoreg_host 1.7 - 4.4.4 +conditions 1.3.1 - 4.4.4 +config 1.3.1 - 4.4.4 +config_autoreg_tls 4.4.0 - 4.4.4 +corr_condition 3.2.0 - 4.4.4 +corr_condition_group 3.2.0 - 4.4.4 +corr_condition_tag 3.2.0 - 4.4.4 +corr_condition_tagpair 3.2.0 - 4.4.4 +corr_condition_tagvalue 3.2.0 - 4.4.4 +corr_operation 3.2.0 - 4.4.4 +correlation 3.2.0 - 4.4.4 +dashboard 3.4.0 - 4.4.4 +dashboard_user 3.4.0 - 4.4.4 +dashboard_usrgrp 3.4.0 - 4.4.4 +dbversion 2.1.0 - 4.4.4 +dchecks 1.3.4 - 4.4.4 +dhosts 1.3.4 - 4.4.4 +drules 1.3.4 - 4.4.4 +dservices 1.3.4 - 4.4.4 +escalations 1.5.3 - 4.4.4 +event_recovery 3.2.0 - 4.4.4 SCHEMAONLY +event_suppress 4.0.0 - 4.4.4 SCHEMAONLY +event_tag 3.2.0 - 4.4.4 SCHEMAONLY +events 1.3.1 - 4.4.4 SCHEMAONLY +expressions 1.7 - 4.4.4 +functions 1.3.1 - 4.4.4 +globalmacro 1.7 - 4.4.4 +globalvars 1.9.6 - 4.4.4 +graph_discovery 1.9.0 - 4.4.4 +graph_theme 1.7 - 4.4.4 +graphs 1.3.1 - 4.4.4 +graphs_items 1.3.1 - 4.4.4 +group_discovery 2.1.4 - 4.4.4 +group_prototype 2.1.4 - 4.4.4 +groups 1.3.1 - 3.4.15 help_items 1.3.1 - 2.1.8 -history 1.3.1 - 4.0.0 SCHEMAONLY -history_log 1.3.1 - 4.0.0 SCHEMAONLY -history_str 1.3.1 - 3.4.1 SCHEMAONLY -history_str_sync 1.3.1 - 2.2.13 SCHEMAONLY -history_sync 1.3.1 - 2.2.13 SCHEMAONLY -history_text 1.3.1 - 4.0.0 SCHEMAONLY -history_uint 1.3.1 - 4.0.0 SCHEMAONLY -history_uint_sync 1.3.1 - 2.2.13 SCHEMAONLY -host_discovery 2.1.4 - 4.0.0 -host_inventory 1.9.6 - 4.0.0 +history 1.3.1 - 4.4.4 SCHEMAONLY +history_log 1.3.1 - 4.4.4 SCHEMAONLY +history_str 1.3.1 - 4.4.4 SCHEMAONLY +history_str_sync 1.3.1 - 2.2.23 SCHEMAONLY +history_sync 1.3.1 - 2.2.23 SCHEMAONLY +history_text 1.3.1 - 4.4.4 SCHEMAONLY +history_uint 1.3.1 - 4.4.4 SCHEMAONLY +history_uint_sync 1.3.1 - 2.2.23 SCHEMAONLY +host_discovery 2.1.4 - 4.4.4 +host_inventory 1.9.6 - 4.4.4 host_profile 1.9.3 - 1.9.5 -hostmacro 1.7 - 4.0.0 -hosts 1.3.1 - 4.0.0 -hosts_groups 1.3.1 - 4.0.0 +host_tag 4.2.0 - 4.4.4 +hostmacro 1.7 - 4.4.4 +hosts 1.3.1 - 4.4.4 +hosts_groups 1.3.1 - 4.4.4 hosts_profiles 1.3.1 - 1.9.2 hosts_profiles_ext 1.6 - 1.9.2 -hosts_templates 1.3.1 - 4.0.0 -housekeeper 1.3.1 - 4.0.0 -hstgrp 4.0.0 - 4.0.0 -httpstep 1.3.3 - 4.0.0 -httpstep_field 3.4.0 - 4.0.0 -httpstepitem 1.3.3 - 4.0.0 -httptest 1.3.3 - 4.0.0 -httptest_field 3.4.0 - 4.0.0 -httptestitem 1.3.3 - 4.0.0 -icon_map 1.9.6 - 4.0.0 -icon_mapping 1.9.6 - 4.0.0 -ids 1.3.3 - 4.0.0 -images 1.3.1 - 4.0.0 -interface 1.9.1 - 4.0.0 -interface_discovery 2.1.4 - 4.0.0 -item_application_prototype 2.5.0 - 4.0.0 -item_condition 2.3.0 - 4.0.0 -item_discovery 1.9.0 - 4.0.0 -item_preproc 3.4.0 - 4.0.0 -items 1.3.1 - 4.0.0 -items_applications 1.3.1 - 4.0.0 -maintenance_tag 4.0.0 - 4.0.0 -maintenances 1.7 - 4.0.0 -maintenances_groups 1.7 - 4.0.0 -maintenances_hosts 1.7 - 4.0.0 -maintenances_windows 1.7 - 4.0.0 -mappings 1.3.1 - 4.0.0 -media 1.3.1 - 4.0.0 -media_type 1.3.1 - 4.0.0 -node_cksum 1.3.1 - 2.2.13 +hosts_templates 1.3.1 - 4.4.4 +housekeeper 1.3.1 - 4.4.4 +hstgrp 4.0.0 - 4.4.4 +httpstep 1.3.3 - 4.4.4 +httpstep_field 3.4.0 - 4.4.4 +httpstepitem 1.3.3 - 4.4.4 +httptest 1.3.3 - 4.4.4 +httptest_field 3.4.0 - 4.4.4 +httptestitem 1.3.3 - 4.4.4 +icon_map 1.9.6 - 4.4.4 +icon_mapping 1.9.6 - 4.4.4 +ids 1.3.3 - 4.4.4 +images 1.3.1 - 4.4.4 +interface 1.9.1 - 4.4.4 +interface_discovery 2.1.4 - 4.4.4 +item_application_prototype 2.5.0 - 4.4.4 +item_condition 2.3.0 - 4.4.4 +item_discovery 1.9.0 - 4.4.4 +item_preproc 3.4.0 - 4.4.4 +item_rtdata 4.4.0 - 4.4.4 SCHEMAONLY +items 1.3.1 - 4.4.4 +items_applications 1.3.1 - 4.4.4 +lld_macro_path 4.2.0 - 4.4.4 +maintenance_tag 4.0.0 - 4.4.4 +maintenances 1.7 - 4.4.4 +maintenances_groups 1.7 - 4.4.4 +maintenances_hosts 1.7 - 4.4.4 +maintenances_windows 1.7 - 4.4.4 +mappings 1.3.1 - 4.4.4 +media 1.3.1 - 4.4.4 +media_type 1.3.1 - 4.4.4 +media_type_param 4.4.0 - 4.4.4 +node_cksum 1.3.1 - 2.2.23 node_configlog 1.3.1 - 1.4.7 -nodes 1.3.1 - 2.2.13 -opcommand 1.9.4 - 4.0.0 -opcommand_grp 1.9.2 - 4.0.0 -opcommand_hst 1.9.2 - 4.0.0 -opconditions 1.5.3 - 4.0.0 -operations 1.3.4 - 4.0.0 -opgroup 1.9.2 - 4.0.0 -opinventory 3.0.0 - 4.0.0 +nodes 1.3.1 - 2.2.23 +opcommand 1.9.4 - 4.4.4 +opcommand_grp 1.9.2 - 4.4.4 +opcommand_hst 1.9.2 - 4.4.4 +opconditions 1.5.3 - 4.4.4 +operations 1.3.4 - 4.4.4 +opgroup 1.9.2 - 4.4.4 +opinventory 3.0.0 - 4.4.4 opmediatypes 1.7 - 1.8.22 -opmessage 1.9.2 - 4.0.0 -opmessage_grp 1.9.2 - 4.0.0 -opmessage_usr 1.9.2 - 4.0.0 -optemplate 1.9.2 - 4.0.0 -problem 3.2.0 - 4.0.0 SCHEMAONLY -problem_tag 3.2.0 - 4.0.0 SCHEMAONLY -profiles 1.3.1 - 4.0.0 -proxy_autoreg_host 1.7 - 4.0.0 -proxy_dhistory 1.5 - 4.0.0 -proxy_history 1.5.1 - 4.0.0 -regexps 1.7 - 4.0.0 -rights 1.3.1 - 4.0.0 -screen_user 3.0.0 - 4.0.0 -screen_usrgrp 3.0.0 - 4.0.0 -screens 1.3.1 - 4.0.0 -screens_items 1.3.1 - 4.0.0 -scripts 1.5 - 4.0.0 -service_alarms 1.3.1 - 4.0.0 -services 1.3.1 - 4.0.0 -services_links 1.3.1 - 4.0.0 -services_times 1.3.1 - 4.0.0 -sessions 1.3.1 - 4.0.0 -slides 1.3.4 - 4.0.0 -slideshow_user 3.0.0 - 4.0.0 -slideshow_usrgrp 3.0.0 - 4.0.0 -slideshows 1.3.4 - 4.0.0 -sysmap_element_trigger 3.4.0 - 4.0.0 -sysmap_element_url 1.9.0 - 4.0.0 -sysmap_shape 3.4.0 - 4.0.0 -sysmap_url 1.9.0 - 4.0.0 -sysmap_user 3.0.0 - 4.0.0 -sysmap_usrgrp 3.0.0 - 4.0.0 -sysmaps 1.3.1 - 4.0.0 -sysmaps_elements 1.3.1 - 4.0.0 -sysmaps_link_triggers 1.5 - 4.0.0 -sysmaps_links 1.3.1 - 4.0.0 -tag_filter 4.0.0 - 4.0.0 -task 3.2.0 - 4.0.0 SCHEMAONLY -task_acknowledge 3.4.0 - 4.0.0 SCHEMAONLY -task_check_now 4.0.0 - 4.0.0 SCHEMAONLY -task_close_problem 3.2.0 - 4.0.0 SCHEMAONLY -task_remote_command 3.4.0 - 4.0.0 SCHEMAONLY -task_remote_command_result 3.4.0 - 4.0.0 SCHEMAONLY -timeperiods 1.7 - 4.0.0 -trends 1.3.1 - 4.0.0 SCHEMAONLY -trends_uint 1.5 - 4.0.0 SCHEMAONLY -trigger_depends 1.3.1 - 4.0.0 -trigger_discovery 1.9.0 - 4.0.0 -trigger_tag 3.2.0 - 4.0.0 -triggers 1.3.1 - 4.0.0 +opmessage 1.9.2 - 4.4.4 +opmessage_grp 1.9.2 - 4.4.4 +opmessage_usr 1.9.2 - 4.4.4 +optemplate 1.9.2 - 4.4.4 +problem 3.2.0 - 4.4.4 SCHEMAONLY +problem_tag 3.2.0 - 4.4.4 SCHEMAONLY +profiles 1.3.1 - 4.4.4 +proxy_autoreg_host 1.7 - 4.4.4 +proxy_dhistory 1.5 - 4.4.4 +proxy_history 1.5.1 - 4.4.4 +regexps 1.7 - 4.4.4 +rights 1.3.1 - 4.4.4 +screen_user 3.0.0 - 4.4.4 +screen_usrgrp 3.0.0 - 4.4.4 +screens 1.3.1 - 4.4.4 +screens_items 1.3.1 - 4.4.4 +scripts 1.5 - 4.4.4 +service_alarms 1.3.1 - 4.4.4 +services 1.3.1 - 4.4.4 +services_links 1.3.1 - 4.4.4 +services_times 1.3.1 - 4.4.4 +sessions 1.3.1 - 4.4.4 +slides 1.3.4 - 4.4.4 +slideshow_user 3.0.0 - 4.4.4 +slideshow_usrgrp 3.0.0 - 4.4.4 +slideshows 1.3.4 - 4.4.4 +sysmap_element_trigger 3.4.0 - 4.4.4 +sysmap_element_url 1.9.0 - 4.4.4 +sysmap_shape 3.4.0 - 4.4.4 +sysmap_url 1.9.0 - 4.4.4 +sysmap_user 3.0.0 - 4.4.4 +sysmap_usrgrp 3.0.0 - 4.4.4 +sysmaps 1.3.1 - 4.4.4 +sysmaps_elements 1.3.1 - 4.4.4 +sysmaps_link_triggers 1.5 - 4.4.4 +sysmaps_links 1.3.1 - 4.4.4 +tag_filter 4.0.0 - 4.4.4 +task 3.2.0 - 4.4.4 SCHEMAONLY +task_acknowledge 3.4.0 - 4.4.4 SCHEMAONLY +task_check_now 4.0.0 - 4.4.4 SCHEMAONLY +task_close_problem 3.2.0 - 4.4.4 SCHEMAONLY +task_remote_command 3.4.0 - 4.4.4 SCHEMAONLY +task_remote_command_result 3.4.0 - 4.4.4 SCHEMAONLY +timeperiods 1.7 - 4.4.4 +trends 1.3.1 - 4.4.4 SCHEMAONLY +trends_uint 1.5 - 4.4.4 SCHEMAONLY +trigger_depends 1.3.1 - 4.4.4 +trigger_discovery 1.9.0 - 4.4.4 +trigger_tag 3.2.0 - 4.4.4 +triggers 1.3.1 - 4.4.4 user_history 1.7 - 2.4.8 -users 1.3.1 - 4.0.0 -users_groups 1.3.1 - 4.0.0 -usrgrp 1.3.1 - 4.0.0 -valuemaps 1.3.1 - 4.0.0 -widget 3.4.0 - 4.0.0 -widget_field 3.4.0 - 4.0.0 +users 1.3.1 - 4.4.4 +users_groups 1.3.1 - 4.4.4 +usrgrp 1.3.1 - 4.4.4 +valuemaps 1.3.1 - 4.4.4 +widget 3.4.0 - 4.4.4 +widget_field 3.4.0 - 4.4.4 From 27a105e314acd660f7e178acefe26ddf8e736d6b Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 16 Jan 2020 16:48:56 +0100 Subject: [PATCH 110/122] Set hostname to `uname -n` if DB is on localhost --- zabbix-dump | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 15df53f..a594ba4 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -305,11 +305,17 @@ esac # Log file for errors ERRORLOG=$(mktemp) -# Host name: try reverse lookup if IP is given -DBHOSTNAME="$DBHOST" +# Host name +if [[ -z "$DBHOST" || "$DBHOST" == "127.0.0.1" || "$DBHOST" == "127.0.0.1" ]]; then + DBHOSTNAME="$(uname -n)" +else + DBHOSTNAME="$DBHOST" +fi + +# Try reverse lookup if IP is given command -v dig >/dev/null 2>&1 FIND_DIG=$? -if [ "$REVERSELOOKUP" == "yes" ] && [ $FIND_DIG -eq 0 ] && [ -n "$DBHOST" ]; then +if [[ "$REVERSELOOKUP" == "yes" && $FIND_DIG -eq 0 && -n "$DBHOST" ]]; then # Try resolving a given host ip newHostname=$(dig +noall +answer -x "${DBHOST}" | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') test \! -z "$newHostname" && DBHOSTNAME="$newHostname" From 2b325d5bf58cdeb2fc60e9981377efd1ed7d1251 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 16 Jan 2020 17:09:51 +0100 Subject: [PATCH 111/122] Bump version number to 0.9.2 and update docs --- README.md | 11 ++++++++++- zabbix-dump | 6 ++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c88d412..749406d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Backup script for Zabbix configuration data (MySQL/PostgreSQL) -This is a MySQL/PostgreSQL database backup script for the [Zabbix](http://www.zabbix.com/) monitoring software from version 1.3.1 up to 4.0. +This is a MySQL/PostgreSQL database backup script for the [Zabbix](http://www.zabbix.com/) monitoring software from version 1.3.1 up to 4.4. ## Download @@ -14,6 +14,15 @@ Please see the [Project Wiki](https://github.com/maxhq/zabbix-backup/wiki). ## Version history +**0.9.2 (2020-01-16)** + +- ENH: Support for Zabbix 4.4 +- ENH: Fix (non-critical) shellcheck issues (Mario Trangoni) +- CHG: Fix and enhance helper script get-table-list.pl +- FIX: Escape special characters while reading password (ironbishop) +- FIX: Re-enable accidentally disabled cleanup of postgresql password file +- FIX: Insert hostname into backup file also if database resides on localhost + **0.9.1 (2019-03-21)** - FIX: Correctly process hostname option -H (Tiago Cruz) diff --git a/zabbix-dump b/zabbix-dump index a594ba4..4db26dd 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -4,7 +4,7 @@ # zabbix-dump - Configuration Backup for Zabbix' MySQL or PostgreSQL data # # VERSION -# 0.9.1 +# 0.9.2 # # SYNOPSIS # This is a MySQL configuration backup script for Zabbix 1.x, 2.x, 3.x and 4.x. @@ -32,9 +32,11 @@ # - Johannes Petz (PetzJohannes) # - Wesley Schaft (wschaft) # - Tiago Cruz (tiago-cruz-movile) +# - Mario Trangoni (mjtrangoni) +# - ironbishop # # AUTHOR -# Jens Berthold (maxhq), 2019 +# Jens Berthold (maxhq), 2020 # # LICENSE # This script is released under the MIT License (see LICENSE.txt) From b5464e56c5231444cf8db723bf6d38239f8c832d Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Thu, 16 Jan 2020 18:02:28 +0100 Subject: [PATCH 112/122] Update release docs --- RELEASE.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/RELEASE.md b/RELEASE.md index 3389487..aaa23f6 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -6,7 +6,26 @@ ### After commit +* Tag the release commit: `git tag v0.9.2 && git push --tags` * Add release at https://github.com/maxhq/zabbix-backup/releases + + **Summary of main changes** + + ``` +This release fixes ... + +### Direct download + +[zabbix-mysql-dump](https://raw.githubusercontent.com/maxhq/zabbix-backup/v0.9.2/zabbix-dump) (v0.9.2) + +### Changelog + +Please see the [version history in the README](https://github.com/maxhq/zabbix-backup/blob/v0.9.2/README.md#version-history). + +### Thanks! +- @username + ``` + * Update https://github.com/maxhq/zabbix-backup/wiki * Update http://zabbix.org/wiki/Docs/howto/mysql_backup_script * Announce release at https://www.linkedin.com/groups/161448 @@ -17,6 +36,7 @@ > Tables holding configuration data will be fully backed up. For mass data tables (events, history, trends, ...) only the table schema is stored without any data (to keep the backup small). > > Overiew: https://github.com/maxhq/zabbix-backup/blob/master/README.md + > Latest release: https://github.com/maxhq/zabbix-backup/releases/latest * Announce release at https://www.xing.com/communities/forums/100845147 @@ -26,3 +46,4 @@ > Es sichert Konfigurationsdaten komplett, bei Tabellen mit Massendaten (Historie, Events, Trends etc.) jedoch nur das "leere" Datenbankschema (um das Backup zu minimieren). > > Übersicht: https://github.com/maxhq/zabbix-backup/blob/master/README.md + > Neueste Version: https://github.com/maxhq/zabbix-backup/releases/latest From b3c335542a52fcba609f991dd64acb83849facbe Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 17 Jan 2020 16:04:46 +0100 Subject: [PATCH 113/122] Check for unknown tables; make PostgreSQL dump more stable --- RELEASE.md | 2 +- zabbix-dump | 54 ++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index aaa23f6..086bd5e 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -27,7 +27,7 @@ Please see the [version history in the README](https://github.com/maxhq/zabbix-b ``` * Update https://github.com/maxhq/zabbix-backup/wiki -* Update http://zabbix.org/wiki/Docs/howto/mysql_backup_script +* Update https://zabbix.org/wiki/Docs/howto/database_backup_script * Announce release at https://www.linkedin.com/groups/161448 > **New version x.x.x of zabbix-dump** diff --git a/zabbix-dump b/zabbix-dump index 4db26dd..8779b78 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -60,6 +60,7 @@ REVERSELOOKUP="yes" GENERATIONSTOKEEP=0 ZBX_CONFIG="/etc/zabbix/zabbix_server.conf" READ_ZBX_CONFIG="yes" +HANDLE_UNKNOWN="fail" # # SHOW HELP @@ -135,6 +136,13 @@ OPTIONS -n Skip reverse lookup of IP address for host. + -f + Force backup for unknown tables / forward compatibility: + Don't complain about unknown tables and make a full backup of them + + -i + Ingore unknown tables (don't include them into the backup) + -q Quiet mode: no output except for errors (for batch/crontab use). @@ -173,7 +181,7 @@ fi # PARSE COMMAND LINE ARGUMENTS # DB_GIVEN=0 -while getopts ":c:d:H:o:p:P:r:s:t:u:z:0nqxZ" opt; do +while getopts ":c:d:H:o:p:P:r:s:t:u:z:0nqxZfi" opt; do case $opt in t) DBTYPE="$OPTARG" ;; H) ODBHOST="$OPTARG" ;; @@ -190,6 +198,8 @@ while getopts ":c:d:H:o:p:P:r:s:t:u:z:0nqxZ" opt; do x) COMPRESSION="xz" ;; 0) COMPRESSION="" ;; n) REVERSELOOKUP="no" ;; + f) HANDLE_UNKNOWN="backup" ;; + i) HANDLE_UNKNOWN="ignore" ;; q) QUIET="yes" ;; \?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;; :) echo "Option -$OPTARG requires an argument" >&2; exit 1 ;; @@ -386,10 +396,12 @@ esac # (http://stackoverflow.com/a/3477269/2983301) # DATA_TABLES=() +KNOWN_TABLES=() while read -r line; do table=$(echo "$line" | cut -d" " -f1) echo "$line" | cut -d" " -f5 | grep -qi "SCHEMAONLY" test $? -eq 0 && DATA_TABLES+=($table) + KNOWN_TABLES+=($table) done < <(sed '0,/^__DATA__$/d' "${BASH_SOURCE[*]}" | tr -s " ") # paranoid check @@ -408,7 +420,7 @@ case $DBTYPE in DB_TABLES=$(mysql "${DB_OPTS_BATCH[@]}" -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" 2>$ERRORLOG) ;; psql) - DB_TABLES=$(psql "${DB_OPTS_BATCH[@]}" -c "SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'" 2>$ERRORLOG) + DB_TABLES=$(psql "${DB_OPTS_BATCH[@]}" -c "SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_catalog='$DBNAME' AND table_type='BASE TABLE'" 2>$ERRORLOG) ;; esac if [ $? -ne 0 ]; then @@ -421,6 +433,27 @@ fi DB_TABLES=$(echo "$DB_TABLES" | sort) DB_TABLE_NUM=$(echo "$DB_TABLES" | wc -l) +# Check if existing tables are known +UNKNOWN_TABLES=() +while read -r table; do + elementIn "$table" "${KNOWN_TABLES[@]}" || UNKNOWN_TABLES+=($table) +done <<<"$DB_TABLES" +if [ ${#UNKNOWN_TABLES[@]} -gt 0 ]; then + if [ "$QUIET" == "no" ]; then + echo "" + [ $HANDLE_UNKNOWN == "fail" ] && echo "ERROR" + echo "Unknown tables found in database:" + for tab in "${UNKNOWN_TABLES[@]}"; do echo " - $tab"; done + [ $HANDLE_UNKNOWN == "backup" ] && echo "They will be included (full data backup) as -f was specified" + [ $HANDLE_UNKNOWN == "ignore" ] && echo "They will be ignored as -i was specified" + [ $HANDLE_UNKNOWN == "fail" ] && echo "To include them (full data backup) specify -f, to ignore them use -i" + fi + if [ $HANDLE_UNKNOWN == "fail" ]; then + clean_psql_pass + exit 1; + fi +fi + # Query Zabbix database version VERSION="" case $DBTYPE in @@ -453,6 +486,10 @@ mkdir -p "${DUMPDIR}" case $DBTYPE in mysql) while read -r table; do + if [ $HANDLE_UNKNOWN == "ignore" ]; then + if elementIn "$table" "${UNKNOWN_TABLES[@]}"; then continue; fi + fi + # large data tables: only store schema if elementIn "$table" "${DATA_TABLES[@]}"; then dump_opt="--no-data" @@ -481,14 +518,21 @@ case $DBTYPE in done <<<"$DB_TABLES" ;; psql) + DUMP_OPTS=() while read -r table; do + if [ $HANDLE_UNKNOWN == "ignore" ]; then + if elementIn "$table" "${UNKNOWN_TABLES[@]}"; then + DUMP_OPTS+=(--exclude-table=$table) + fi + fi if elementIn "$table" "${DATA_TABLES[@]}"; then - dump_opt="--exclude-table-data=$table" - PROCESSED_DATA_TABLES+=($dump_opt) + DUMP_OPTS+=(--exclude-table-data=$table) + PROCESSED_DATA_TABLES+=($table) fi done <<<"$DB_TABLES" - pg_dump "${DB_OPTS[@]}" ${PROCESSED_DATA_TABLES[@]} > "$DUMPFILE" 2>$ERRORLOG + pg_dump "${DB_OPTS[@]}" -d $DBNAME "${DUMP_OPTS[@]}" > "$DUMPFILE" 2>$ERRORLOG + if [ $? -ne 0 ]; then echo -e "\nERROR: Could not backup database." >&2 cat $ERRORLOG >&2 From d5d5f97a13cc35588757f982958544242756633c Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 17 Jan 2020 16:14:20 +0100 Subject: [PATCH 114/122] Replace --extended-insert=FALSE with equivalent --skip-extended-insert --- zabbix-dump | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix-dump b/zabbix-dump index 8779b78..71ca5f6 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -496,7 +496,7 @@ case $DBTYPE in PROCESSED_DATA_TABLES+=($table) # configuration tables: full dump else - dump_opt="--extended-insert=FALSE" + dump_opt="--skip-extended-insert" fi mysqldump "${DB_OPTS[@]}" \ From fc1896b61523cd473d8b652c53bf4aeb1732e151 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 17 Jan 2020 16:23:24 +0100 Subject: [PATCH 115/122] Rename misleading variable DATA_TABLES to SCHEMAONLY_TABLES --- zabbix-dump | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 71ca5f6..5e75aa3 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -395,17 +395,17 @@ esac # READ TABLE LIST from __DATA__ section at the end of this script # (http://stackoverflow.com/a/3477269/2983301) # -DATA_TABLES=() +SCHEMAONLY_TABLES=() KNOWN_TABLES=() while read -r line; do table=$(echo "$line" | cut -d" " -f1) echo "$line" | cut -d" " -f5 | grep -qi "SCHEMAONLY" - test $? -eq 0 && DATA_TABLES+=($table) + test $? -eq 0 && SCHEMAONLY_TABLES+=($table) KNOWN_TABLES+=($table) done < <(sed '0,/^__DATA__$/d' "${BASH_SOURCE[*]}" | tr -s " ") # paranoid check -if [ ${#DATA_TABLES[@]} -lt 5 ]; then +if [ ${#SCHEMAONLY_TABLES[@]} -lt 5 ]; then echo "ERROR: The number of large data tables configured in this script is less than 5." >&2 exit 1 fi @@ -477,7 +477,7 @@ DUMPFILENAME_PREFIX="zabbix_cfg_${DBHOSTNAME}" DUMPFILEBASE="${DUMPFILENAME_PREFIX}_$(date +%Y%m%d-%H%M)${VERSION}.sql" DUMPFILE="$DUMPDIR/$DUMPFILEBASE" -PROCESSED_DATA_TABLES=() +PROCESSED_SCHEMAONLY_TABLES=() i=0 mkdir -p "${DUMPDIR}" @@ -491,9 +491,9 @@ case $DBTYPE in fi # large data tables: only store schema - if elementIn "$table" "${DATA_TABLES[@]}"; then + if elementIn "$table" "${SCHEMAONLY_TABLES[@]}"; then dump_opt="--no-data" - PROCESSED_DATA_TABLES+=($table) + PROCESSED_SCHEMAONLY_TABLES+=($table) # configuration tables: full dump else dump_opt="--skip-extended-insert" @@ -525,9 +525,9 @@ case $DBTYPE in DUMP_OPTS+=(--exclude-table=$table) fi fi - if elementIn "$table" "${DATA_TABLES[@]}"; then + if elementIn "$table" "${SCHEMAONLY_TABLES[@]}"; then DUMP_OPTS+=(--exclude-table-data=$table) - PROCESSED_DATA_TABLES+=($table) + PROCESSED_SCHEMAONLY_TABLES+=($table) fi done <<<"$DB_TABLES" @@ -550,7 +550,7 @@ rm $ERRORLOG 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 + for table in "${PROCESSED_SCHEMAONLY_TABLES[@]}"; do echo " - $table"; done echo echo "Compressing backup file..." From 7b6616e938d4d24150ca06736ead8cb373104e2f Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 17 Jan 2020 16:46:17 +0100 Subject: [PATCH 116/122] Do not backup each MySQL table individually anymore This significantly reduces the MySQL backup time --- zabbix-dump | 53 ++++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 5e75aa3..2cea3ef 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -447,6 +447,7 @@ if [ ${#UNKNOWN_TABLES[@]} -gt 0 ]; then [ $HANDLE_UNKNOWN == "backup" ] && echo "They will be included (full data backup) as -f was specified" [ $HANDLE_UNKNOWN == "ignore" ] && echo "They will be ignored as -i was specified" [ $HANDLE_UNKNOWN == "fail" ] && echo "To include them (full data backup) specify -f, to ignore them use -i" + echo "" fi if [ $HANDLE_UNKNOWN == "fail" ]; then clean_psql_pass @@ -485,37 +486,39 @@ mkdir -p "${DUMPDIR}" [ "$QUIET" == "no" ] && echo "Starting table backups..." case $DBTYPE in mysql) - while read -r table; do - if [ $HANDLE_UNKNOWN == "ignore" ]; then - if elementIn "$table" "${UNKNOWN_TABLES[@]}"; then continue; fi - fi + # dump schemas + DUMP_OPTS=() + if [ $HANDLE_UNKNOWN == "ignore" ]; then + while read -r table; do + if elementIn "$table" "${UNKNOWN_TABLES[@]}"; then + DUMP_OPTS+=(--ignore-table=$DBNAME.$table) + fi + done <<<"$DB_TABLES" + fi + + mysqldump "${DB_OPTS[@]}" --opt --single-transaction --skip-lock-tables \ + "${DUMP_OPTS[@]}" --no-data --routines $DBNAME > "$DUMPFILE" 2>$ERRORLOG + + if [ $? -ne 0 ]; then echo $'\nERROR: Could not backup table schemas.\n' >&2; cat $ERRORLOG >&2; exit 1; fi - # large data tables: only store schema + # dump data + DUMP_OPTS=() + while read -r table; do if elementIn "$table" "${SCHEMAONLY_TABLES[@]}"; then - dump_opt="--no-data" + DUMP_OPTS+=(--ignore-table=$DBNAME.$table) PROCESSED_SCHEMAONLY_TABLES+=($table) - # configuration tables: full dump - else - dump_opt="--skip-extended-insert" fi - - mysqldump "${DB_OPTS[@]}" \ - --routines --opt --single-transaction --skip-lock-tables \ - $dump_opt \ - $DBNAME --tables ${table} >> "$DUMPFILE" 2>$ERRORLOG - - if [ $? -ne 0 ]; then echo -e "\nERROR: Could not backup table ${table}:" >&2; cat $ERRORLOG >&2; exit 1; fi - - if [ "$QUIET" == "no" ]; then - # show percentage - i=$((i+1)); i_percent=$(($i * 100 / $DB_TABLE_NUM)) - if [ $(($i_percent % 12)) -eq 0 ]; then - echo -n "${i_percent}%" - else - if [ $(($i_percent % 2)) -eq 0 ]; then echo -n "."; fi + if [ $HANDLE_UNKNOWN == "ignore" ]; then + if elementIn "$table" "${UNKNOWN_TABLES[@]}"; then + DUMP_OPTS+=(--ignore-table=$DBNAME.$table) fi fi done <<<"$DB_TABLES" + + mysqldump "${DB_OPTS[@]}" --opt --single-transaction --skip-lock-tables \ + "${DUMP_OPTS[@]}" --no-create-info --skip-extended-insert $DBNAME >> "$DUMPFILE" 2>$ERRORLOG + + if [ $? -ne 0 ]; then echo $'\nERROR: Could not backup table data.\n' >&2; cat $ERRORLOG >&2; exit 1; fi ;; psql) DUMP_OPTS=() @@ -534,7 +537,7 @@ case $DBTYPE in pg_dump "${DB_OPTS[@]}" -d $DBNAME "${DUMP_OPTS[@]}" > "$DUMPFILE" 2>$ERRORLOG if [ $? -ne 0 ]; then - echo -e "\nERROR: Could not backup database." >&2 + echo $'\nERROR: Could not backup database.\n' >&2 cat $ERRORLOG >&2 clean_psql_pass exit 1 From 815c211917bd8038aca148112beec7a614c95a30 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 17 Jan 2020 17:27:20 +0100 Subject: [PATCH 117/122] Process 'DBSchema' option in Zabbix config and add -S to specify schema --- zabbix-dump | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 2cea3ef..1a9c6e5 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -51,6 +51,7 @@ DUMPDIR="$PWD" DBTYPE="mysql" DEFAULT_DBHOST="127.0.0.1" +DEFAULT_DBSCHEMA="public" DEFAULT_DBNAME="zabbix" DEFAULT_DBUSER="zabbix" DEFAULT_DBPASS="" @@ -88,6 +89,10 @@ OPTIONS Path to DBMS socket file. Can be used as an alternative to specifying host (and maybe port). + -S SCHEMA + Name of database schema (PostgreSQL only). + Default: $DEFAULT_DBSCHEMA + -d DATABASE Name of Zabbix database. Default: $DEFAULT_DBNAME @@ -181,11 +186,12 @@ fi # PARSE COMMAND LINE ARGUMENTS # DB_GIVEN=0 -while getopts ":c:d:H:o:p:P:r:s:t:u:z:0nqxZfi" opt; do +while getopts ":c:S:d:H:o:p:P:r:s:t:u:z:0nqxZfi" opt; do case $opt in t) DBTYPE="$OPTARG" ;; H) ODBHOST="$OPTARG" ;; s) ODBSOCKET="$OPTARG" ;; + S) ODBSCHEMA="$OPTARG" ;; d) ODBNAME="$OPTARG"; DB_GIVEN=1 ;; u) ODBUSER="$OPTARG" ;; P) ODBPORT="$OPTARG" ;; @@ -216,6 +222,7 @@ if [[ "${READ_ZBX_CONFIG}" == "yes" && -f "${ZBX_CONFIG}" && -r "${ZBX_CONFIG}" DBHOST="$(/usr/bin/awk -F'=' '/^DBHost/{ print $2 }' "${ZBX_CONFIG}")" DBPORT="$(/usr/bin/awk -F'=' '/^DBPort/{ print $2 }' "${ZBX_CONFIG}")" DBNAME="$(/usr/bin/awk -F'=' '/^DBName/{ print $2 }' "${ZBX_CONFIG}")" + DBSCHEMA="$(/usr/bin/awk -F'=' '/^DBSchema/{ print $2 }' "${ZBX_CONFIG}")" DBUSER="$(/usr/bin/awk -F'=' '/^DBUser/{ print $2 }' "${ZBX_CONFIG}")" DBPASS="$(/usr/bin/awk -F'=' '/^DBPassword/{ print $2 }' "${ZBX_CONFIG}")" @@ -253,6 +260,7 @@ fi [ -n "$ODBHOST" ] && DBHOST=$ODBHOST [ -n "$ODBPORT" ] && DBPORT=$ODBPORT [ -n "$ODBSOCKET" ] && DBSOCKET=$ODBSOCKET && DBHOST="" +[ -n "$ODBSCHEMA" ] && DBSCHEMA=$ODBSCHEMA [ -n "$ODBNAME" ] && DBNAME=$ODBNAME [ -n "$ODBUSER" ] && DBUSER=$ODBUSER [ -n "$ODBPASS" ] && DBPASS=$ODBPASS @@ -344,6 +352,7 @@ EOF [ -n "$MYSQL_CONFIG" ] && echo " - cfg file: $MYSQL_CONFIG" [ -n "$DBHOST" ] && echo " - host: $DBHOST ($DBHOSTNAME)" && echo " - port: $DBPORT" [ -n "$DBSOCKET" ] && echo " - socket: $DBSOCKET" + [ -n "$DBSCHEMA" ] && echo " - schema: $DBSCHEMA" [ -n "$DBNAME" ] && echo " - database: $DBNAME" [ -n "$DBUSER" ] && echo " - user: $DBUSER" [ -n "$DUMPDIR" ] && echo " - output: $DUMPDIR" @@ -487,7 +496,7 @@ mkdir -p "${DUMPDIR}" case $DBTYPE in mysql) # dump schemas - DUMP_OPTS=() + DUMP_OPTS=(--opt --single-transaction --skip-lock-tables --no-data --routines) if [ $HANDLE_UNKNOWN == "ignore" ]; then while read -r table; do if elementIn "$table" "${UNKNOWN_TABLES[@]}"; then @@ -496,13 +505,12 @@ case $DBTYPE in done <<<"$DB_TABLES" fi - mysqldump "${DB_OPTS[@]}" --opt --single-transaction --skip-lock-tables \ - "${DUMP_OPTS[@]}" --no-data --routines $DBNAME > "$DUMPFILE" 2>$ERRORLOG + mysqldump "${DB_OPTS[@]}" "${DUMP_OPTS[@]}" $DBNAME > "$DUMPFILE" 2>$ERRORLOG if [ $? -ne 0 ]; then echo $'\nERROR: Could not backup table schemas.\n' >&2; cat $ERRORLOG >&2; exit 1; fi # dump data - DUMP_OPTS=() + DUMP_OPTS=(--opt --single-transaction --skip-lock-tables --no-create-info --skip-extended-insert) while read -r table; do if elementIn "$table" "${SCHEMAONLY_TABLES[@]}"; then DUMP_OPTS+=(--ignore-table=$DBNAME.$table) @@ -515,8 +523,7 @@ case $DBTYPE in fi done <<<"$DB_TABLES" - mysqldump "${DB_OPTS[@]}" --opt --single-transaction --skip-lock-tables \ - "${DUMP_OPTS[@]}" --no-create-info --skip-extended-insert $DBNAME >> "$DUMPFILE" 2>$ERRORLOG + mysqldump "${DB_OPTS[@]}" "${DUMP_OPTS[@]}" $DBNAME >> "$DUMPFILE" 2>$ERRORLOG if [ $? -ne 0 ]; then echo $'\nERROR: Could not backup table data.\n' >&2; cat $ERRORLOG >&2; exit 1; fi ;; @@ -534,7 +541,9 @@ case $DBTYPE in fi done <<<"$DB_TABLES" - pg_dump "${DB_OPTS[@]}" -d $DBNAME "${DUMP_OPTS[@]}" > "$DUMPFILE" 2>$ERRORLOG + [ -n "$DBSCHEMA" ] && DUMP_OPTS=("${DUMP_OPTS[@]}" -n $DBSCHEMA) + + pg_dump "${DB_OPTS[@]}" "${DUMP_OPTS[@]}" -d $DBNAME > "$DUMPFILE" 2>$ERRORLOG if [ $? -ne 0 ]; then echo $'\nERROR: Could not backup database.\n' >&2 From 2326371e89714aa7bd37b18bb19f9e0f1eef490c Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 17 Jan 2020 17:32:46 +0100 Subject: [PATCH 118/122] Also output error message for unknown tables in quiet mode --- zabbix-dump | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix-dump b/zabbix-dump index 1a9c6e5..62f0006 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -448,7 +448,7 @@ while read -r table; do elementIn "$table" "${KNOWN_TABLES[@]}" || UNKNOWN_TABLES+=($table) done <<<"$DB_TABLES" if [ ${#UNKNOWN_TABLES[@]} -gt 0 ]; then - if [ "$QUIET" == "no" ]; then + if [[ "$QUIET" == "no" || $HANDLE_UNKNOWN == "fail" ]]; then echo "" [ $HANDLE_UNKNOWN == "fail" ] && echo "ERROR" echo "Unknown tables found in database:" From 6eed145d85726ab4dbc4743d130e6a6bb286b3ca Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 17 Jan 2020 17:33:25 +0100 Subject: [PATCH 119/122] Get rid of "echo -e" in favor of "echo $''" --- zabbix-dump | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 62f0006..0e77fff 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -560,24 +560,22 @@ rm $ERRORLOG # COMPRESS BACKUP # if [ "$QUIET" == "no" ]; then - echo -e "\n" - echo "For the following large tables only the schema (without data) was stored:" + echo $'\nFor the following large tables only the schema (without data) was stored:' for table in "${PROCESSED_SCHEMAONLY_TABLES[@]}"; do echo " - $table"; done - echo - echo "Compressing backup file..." + echo $'\nCompressing backup file...' fi EXITCODE=0 if [ "$COMPRESSION" == "gz" ]; then gzip -f "$DUMPFILE"; EXITCODE=$?; fi if [ "$COMPRESSION" == "xz" ]; then xz -f "$DUMPFILE"; EXITCODE=$?; fi if [ $EXITCODE -ne 0 ]; then - echo -e "\nERROR: Could not compress backup file, see previous messages" >&2 + echo $'\nERROR: Could not compress backup file, see previous messages' >&2 clean_psql_pass exit 1 fi -[ "$QUIET" == "no" ] && echo -e "\nBackup Completed:\n${DUMPFILE}${SUFFIX}" +[ "$QUIET" == "no" ] && echo "Backup Completed" && echo "${DUMPFILE}${SUFFIX}" # # ROTATE OLD BACKUPS From c2971abc61da62442088eb0c28175f052cde36c7 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 17 Jan 2020 17:33:59 +0100 Subject: [PATCH 120/122] Skip IP reverse lookup for localhost, fix multiline dig answers --- zabbix-dump | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 0e77fff..22e8f21 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -330,15 +330,15 @@ if [[ -z "$DBHOST" || "$DBHOST" == "127.0.0.1" || "$DBHOST" == "127.0.0.1" ]]; t DBHOSTNAME="$(uname -n)" else DBHOSTNAME="$DBHOST" -fi -# Try reverse lookup if IP is given -command -v dig >/dev/null 2>&1 -FIND_DIG=$? -if [[ "$REVERSELOOKUP" == "yes" && $FIND_DIG -eq 0 && -n "$DBHOST" ]]; then - # Try resolving a given host ip - newHostname=$(dig +noall +answer -x "${DBHOST}" | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') - test \! -z "$newHostname" && DBHOSTNAME="$newHostname" + # Try reverse lookup if IP is given + command -v dig >/dev/null 2>&1 + FIND_DIG=$? + if [[ "$REVERSELOOKUP" == "yes" && $FIND_DIG -eq 0 && -n "$DBHOST" ]]; then + # Try resolving a given host ip + newHostname=$(dig +noall +answer -x "${DBHOST}" | head -n1 | sed -r 's/((\S+)\s+)+([^\.]+)\..*/\3/') + test \! -z "$newHostname" && DBHOSTNAME="$newHostname" + fi fi # From fa9332e23b7173a2c976b3272d8e364ab0a74c27 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 17 Jan 2020 17:54:54 +0100 Subject: [PATCH 121/122] New option -v (--version) to show version information --- zabbix-dump | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/zabbix-dump b/zabbix-dump index 22e8f21..59aac36 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -1,11 +1,9 @@ #!/usr/bin/env bash +VERSION=0.9.2 # # NAME # zabbix-dump - Configuration Backup for Zabbix' MySQL or PostgreSQL data # -# VERSION -# 0.9.2 -# # SYNOPSIS # This is a MySQL configuration backup script for Zabbix 1.x, 2.x, 3.x and 4.x. # It does a full backup of all configuration tables, but only a schema @@ -64,7 +62,17 @@ READ_ZBX_CONFIG="yes" HANDLE_UNKNOWN="fail" # -# SHOW HELP +# Show version +# +show_version() { + echo "zabbix-dump version $VERSION" + exit +} + +if [[ "$1" == "--version" ]]; then show_version; fi + +# +# Show help # if [[ "$1" == "--help" || "$1" == "-h" ]]; then cat <&2; exit 1 ;; :) echo "Option -$OPTARG requires an argument" >&2; exit 1 ;; esac From 4a093d348a3ca07402c6482990a3a8e29a9b6a86 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Fri, 17 Jan 2020 17:55:20 +0100 Subject: [PATCH 122/122] Bump version to 0.9.3 and update changelog --- README.md | 8 ++++++++ zabbix-dump | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 749406d..5f572dd 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,14 @@ Please see the [Project Wiki](https://github.com/maxhq/zabbix-backup/wiki). ## Version history +**0.9.3 (2020-01-17)** + +- ENH: Check for unknown tables +- ENH: Speed up MySQL backup by not calling mysqldump for every single table anymore +- ENH: New option -S to specify PostgreSQL schema +- FIX: Stabilize and enhance PostgreSQL dump +- FIX: Skip IP reverse lookup for localhost, fix multiline dig answers + **0.9.2 (2020-01-16)** - ENH: Support for Zabbix 4.4 diff --git a/zabbix-dump b/zabbix-dump index 59aac36..23d5170 100755 --- a/zabbix-dump +++ b/zabbix-dump @@ -1,5 +1,5 @@ #!/usr/bin/env bash -VERSION=0.9.2 +VERSION=0.9.3 # # NAME # zabbix-dump - Configuration Backup for Zabbix' MySQL or PostgreSQL data