Advertisement
maxhq

Configuration backup for Zabbix 2.0 and 2.2 w/MySQL

May 13th, 2013
1,102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # NAME
  3. #     zabbix-mysql-backupconf.sh - Configuration Backup for Zabbix 2.0 w/MySQL
  4. #
  5. # SYNOPSIS
  6. #     This is a MySQL configuration backup script for Zabbix v2.0 oder 2.2.
  7. #     It does a full backup of all configuration tables, but only a schema
  8. #     backup of large data tables.
  9. #
  10. #     The original script was written by Ricardo Santos and published at
  11. #     http://zabbixzone.com/zabbix/backuping-only-the-zabbix-configuration/
  12. #     and
  13. #     https://github.com/xsbr/zabbixzone/blob/master/zabbix-mysql-backupconf.sh
  14. #
  15. #     Credits for some suggestions concerning the original script to:
  16. #      - Ricardo Santos
  17. #      - Oleksiy Zagorskyi (zalex)
  18. #      - Petr Jendrejovsky
  19. #      - Jonathan Bayer
  20. #
  21. # HISTORY
  22. #     v0.5 - 2013-05-13 Added table list comparison between database and script
  23. #     v0.4 - 2012-03-02 Incorporated mysqldump options suggested by Jonathan Bayer
  24. #     v0.3 - 2012-02-06 Backup of Zabbix 1.9.x / 2.0.0, removed unnecessary use of
  25. #                       variables (DATEBIN etc) for commands that use to be in $PATH
  26. #     v0.2 - 2011-11-05
  27. #
  28. # AUTHOR
  29. #     Jens Berthold (maxhq), 2013
  30.  
  31.  
  32. #
  33. # CONFIGURATION
  34. #
  35.  
  36. # mysql database
  37. DBHOST="1.2.3.4"
  38. DBNAME="zabbix"
  39. DBUSER="zabbix"
  40. DBPASS="password"
  41.  
  42. # backup target path
  43. #MAINDIR="/var/lib/zabbix/backupconf"
  44. # following will store the backup in a subdirectory of the current directory
  45. MAINDIR="`dirname \"$0\"`"
  46.  
  47. #
  48. # CONSTANTS
  49. #
  50.  
  51. # configuration tables
  52. CONFTABLES=( actions applications autoreg_host conditions config dbversion \
  53. dchecks dhosts drules dservices escalations expressions functions globalmacro \
  54. globalvars graph_discovery graph_theme graphs graphs_items groups help_items \
  55. host_inventory hostmacro hosts hosts_groups hosts_templates housekeeper \
  56. httpstep httpstepitem httptest httptestitem icon_map icon_mapping ids images \
  57. interface item_discovery items items_applications maintenances \
  58. maintenances_groups maintenances_hosts maintenances_windows mappings media \
  59. media_type node_cksum nodes opcommand opcommand_grp opcommand_hst opconditions \
  60. operations opgroup opmessage opmessage_grp opmessage_usr optemplate profiles \
  61. proxy_autoreg_host proxy_dhistory proxy_history regexps rights screens \
  62. screens_items scripts service_alarms services services_links services_times \
  63. sessions slides slideshows sysmap_element_url sysmap_url sysmaps \
  64. sysmaps_elements sysmaps_link_triggers sysmaps_links timeperiods \
  65. trigger_depends trigger_discovery triggers user_history users users_groups \
  66. usrgrp valuemaps )
  67.  
  68. # tables with large data
  69. DATATABLES=( acknowledges alerts auditlog_details auditlog events \
  70. history history_log history_str history_str_sync history_sync history_text \
  71. history_uint history_uint_sync trends trends_uint )
  72.  
  73. DUMPDIR="${MAINDIR}/`date +%Y%m%d-%H%M`"
  74. DUMPFILE="${DUMPDIR}/zabbix-conf-backup-`date +%Y%m%d-%H%M`.sql"
  75.  
  76. #
  77. # CHECKS
  78. #
  79. if [ ! -x /usr/bin/mysqldump ]; then
  80.     echo "mysqldump not found."
  81.     echo "(with Debian, \"apt-get install mysql-client\" will help)"
  82.     exit 1
  83. fi
  84.  
  85. #
  86. # compare table list between script and database
  87. #
  88. FILE_TABLES_LIVE=`mktemp`
  89. FILE_TABLES=`mktemp`
  90.  
  91. # Get all current Zabbix tables from databse
  92. mysql -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --batch --silent \
  93.     -e "SELECT table_name FROM information_schema.tables WHERE table_schema = '$DBNAME'" \
  94.     | sort >> $FILE_TABLES_LIVE
  95.  
  96. # Merge CONFTABLES and DATATABLES into one array
  97. allTables=( "${CONFTABLES[@]}" "${DATATABLES[@]}" )
  98. printf '%s\n' "${allTables[@]}" | sort >> $FILE_TABLES
  99.  
  100. difference=`diff --suppress-common-lines $FILE_TABLES $FILE_TABLES_LIVE | grep -v "^\w"`
  101.  
  102. if [ ! -z "$difference" ]; then
  103.     echo -e "The Zabbix database differs from the configuration in this script."
  104.     if [ `echo "$difference" | grep -c "^>"` -gt 0 ]; then
  105.         echo -e "\nThese additional tables where found in '$DBNAME' on $DBHOST:"
  106.         echo "$difference" | grep "^>" | sed 's/^>/ -/gm'
  107.     fi
  108.     if [ `echo "$difference" | grep -c "^<"` -gt 0 ]; then
  109.         echo -e "\nThese configured tables are missing in '$DBNAME' on $DBHOST:"
  110.         echo "$difference" | grep "^<" | sed 's/^</ -/gm'
  111.     fi
  112.     rm $FILE_TABLES_LIVE; rm $FILE_TABLES
  113.     exit
  114. fi
  115. rm $FILE_TABLES_LIVE; rm $FILE_TABLES
  116.  
  117. #
  118. # BACKUP
  119. #
  120. mkdir -p "${DUMPDIR}"
  121.  
  122. >"${DUMPFILE}"
  123.  
  124. # full backup of configuration tables
  125. echo "Full backup of configuration tables:"
  126. for table in ${CONFTABLES[*]}; do
  127.     echo " - ${table}"
  128.     mysqldump --routines --opt --single-transaction --skip-lock-tables --extended-insert=FALSE \
  129.         -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --tables ${table} >>"${DUMPFILE}"
  130. done
  131.  
  132. # scheme backup of large data tables
  133. echo "Scheme backup of data tables:"
  134. for table in ${DATATABLES[*]}; do
  135.     echo " - ${table}"
  136.     mysqldump --routines --opt --single-transaction --skip-lock-tables --no-data \
  137.         -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} --tables ${table} >>"${DUMPFILE}"
  138. done
  139.  
  140. echo "Compressing backup file ${DUMPFILE}..."
  141. gzip -f "${DUMPFILE}"
  142.  
  143. echo
  144. echo "Backup Completed - ${DUMPDIR}"
  145. echo "Hit ENTER"
  146. read
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement