Ad Widget

Collapse

Copying / Duplicating Zabbix Configuration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Toasty
    Junior Member
    • Dec 2010
    • 2

    #1

    Copying / Duplicating Zabbix Configuration

    Hi folks,

    I'm wondering what's the best way to share/copy the Zabbix configuration (templates, groups, users, permissions etc) from one Zabbix server to another. I'm looking for an approach I can automate, so that I can do the copy whenever I want to, just by calling a script.


    A bit more detail: We have one production Zabbix server which we'll be frequently adding things to (items/templates etc) via the web interface. However, as well as our production Zabbix server, we also have a Zabbix server in each of our other environments (eg preprod/test/dev), and we'd like to have those as up to date as possible, configuration wise.

    So although we want to copy the Zabbix configuration across from the production server, we don't want to copy host-specific information or any captured metrics, as each environment will contain different hosts. Another way of putting it, is that we want to be able to setup a 'fresh' Zabbix server but with all of our templates/groups/graphs/alerts/permissions etc ready to go, based on what's currently on our production Zabbix.


    Options to do this as I see it:
    1) Use the API to grab the configuration data from production, and then apply it to the destination server
    + Using the API is almost always my preferred option. The fact that we can script against Zabbix's API is one of the main reasons we're using it.
    - But there doesn't seem to be any export/import API calls.
    - So I'd have to call a ton of separate API calls to export and import everything. I may miss things, and/or I'm not sure what kinds of dependencies in data import order there might be etc. So it looks like it might be a lot of work. (and if in the future new configuration elements are adding to Zabbix which would also need copying, we'd need to modify the script)

    2) Export of templates/maps/permissions/screens from the Zabbix web interface
    + XML format is easy to manipulate in a script if need be.
    + Zabbix itself deals with knowing how to export/import the configuration data
    - Import/export is fiddly to automate: the web interface is designed for humans, not scripts, so isn't the easiest to make use of, may change in the future, etc.
    - From personal experience sometimes to get imports to work I've needed to try them several times to get them to complete, so not sure that an automated script would be very reliable.

    3) Do something at the database level
    + Presumably I'd have a lot of control, it would be fast etc.
    - I don't really want to have any dependency on Zabbix at the database level if I can avoid it. I don't want to have to understand exactly what goes in each of the 88 tables or how the data is structured internally. I don't want to rely on the schema staying the same. etc.
    ~ Or maybe it's easier/safer than I think? (could I just import the whole database then clear out a few tables which are host/data specific?)


    Any thoughts/suggestions?
    Any help much appreciated
  • sire
    Senior Member
    • Jul 2010
    • 210

    #2
    My vote is for option 3. It's not that difficult.
    Regards,
    Sergey Syreskin

    Monitored hosts: 2646 / Active items: 23604 / Server performance: 765.74

    Temporary out of Zabbix business

    Comment

    • walterheck
      Senior Member
      • Jul 2009
      • 153

      #3
      Option 3. Clear out the alert, history* and hosts (where status <> 3) tables and you should be good to go. I might have forgotten more tables, but these are the most important ones.

      cheers,
      Free and Open Source Zabbix Templates Repository | Hosted Zabbix @ Tribily (http://tribily.com)

      Comment

      • Toasty
        Junior Member
        • Dec 2010
        • 2

        #4
        Just to say that so far option 3 seems to be working well, specifically as walterheck described.

        Thanks for your help

        Comment

        • ghoz
          Senior Member
          • May 2011
          • 204

          #5
          On a related note, this is what I'm using to backup zabbix configuration only (including hosts )
          it creates 2 dumps , one for schema, another for config data :

          Code:
          #!/bin/sh
          # backup zabbix config only
          
          DBNAME=zabbix
          DBUSER=xxx
          DBPASS=xxx
          
          BK_DEST=/home/backup/local/
          
          # zabbix schema
          mysqldump -u$DBUSER  -p"$DBPASS" -B "$DBNAME" --no-data > "$BK_DEST/$DBNAME-0-schema.sql"
          
          # zabbix config
          mysqldump -u"$DBUSER"  -p"$DBPASS" -B "$DBNAME" --single-transaction --no-create-info --no-create-db \
              --ignore-table="$DBNAME.acknowledges" \
              --ignore-table="$DBNAME.alerts" \
              --ignore-table="$DBNAME.auditlog" \
              --ignore-table="$DBNAME.auditlog_details" \
              --ignore-table="$DBNAME.escalations" \
              --ignore-table="$DBNAME.events" \
              --ignore-table="$DBNAME.history" \
              --ignore-table="$DBNAME.history_log" \
              --ignore-table="$DBNAME.history_str" \
              --ignore-table="$DBNAME.history_str_sync" \
              --ignore-table="$DBNAME.history_sync" \
              --ignore-table="$DBNAME.history_text" \
              --ignore-table="$DBNAME.history_uint" \
              --ignore-table="$DBNAME.history_uint_sync" \
              --ignore-table="$DBNAME.trends" \
              --ignore-table="$DBNAME.trends_uint" \
              > "$BK_DEST/$DBNAME-1-config.sql"
          
          # optional not tested :
          # discovery, warning : may trigger discovery actions
          #    --ignore-table="$DBNAME.dhosts" \
          #    --ignore-table="$DBNAME.dservices" \
          
          # proxy history
          #    --ignore-table="$DBNAME.proxy_history" \
          #    --ignore-table="$DBNAME.proxy_dhistory" \
          remarks/comments welcome...

          Comment

          • Pada
            Senior Member
            • Apr 2012
            • 236

            #6
            Thank you very much for the script.

            I created a mysql user with just SELECT privileges on my Zabbix 1.8 database, just so that I can be assured that the script won't be able to accidentally lock my tables:
            Code:
            create user 'zabbixbackup'@'localhost' identified by 'zabbixbackup';
            grant select on zabbix18.* to 'zabbixbackup'@'localhost';
            I then added an argument to the mysqldump command-line so that it does not try to lock the tables either, as well as gzipping the results to a date specific filename:
            Code:
            #!/bin/sh
            # backup zabbix config only
            
            DBNAME=zabbix18
            DBUSER=zabbixbackup
            DBPASS=zabbixbackup
            
            BK_DEST=/root/backups/
            
            # zabbix schema
            mysqldump -u$DBUSER  -p"$DBPASS" -B "$DBNAME" --no-data --skip-lock-tables | /bin/gzip -9 -c > "$BK_DEST/$DBNAME-`date +%Y-%m-%d`-schema.sql.gz"
            
            # zabbix config
            mysqldump -u"$DBUSER"  -p"$DBPASS" -B "$DBNAME" --single-transaction --skip-lock-tables --no-create-info --no-create-db \
                --ignore-table="$DBNAME.acknowledges" \
                --ignore-table="$DBNAME.alerts" \
                --ignore-table="$DBNAME.auditlog" \
                --ignore-table="$DBNAME.auditlog_details" \
                --ignore-table="$DBNAME.escalations" \
                --ignore-table="$DBNAME.events" \
                --ignore-table="$DBNAME.history" \
                --ignore-table="$DBNAME.history_log" \
                --ignore-table="$DBNAME.history_str" \
                --ignore-table="$DBNAME.history_str_sync" \
                --ignore-table="$DBNAME.history_sync" \
                --ignore-table="$DBNAME.history_text" \
                --ignore-table="$DBNAME.history_uint" \
                --ignore-table="$DBNAME.history_uint_sync" \
                --ignore-table="$DBNAME.dhosts" \
                --ignore-table="$DBNAME.dservices" \
                --ignore-table="$DBNAME.proxy_history" \
                --ignore-table="$DBNAME.proxy_dhistory" \
                --ignore-table="$DBNAME.trends" \
                --ignore-table="$DBNAME.trends_uint" \
                | /bin/gzip -9 -c > "$BK_DEST/$DBNAME-`date +%Y-%m-%d`-config.sql.gz"
            And then lastly I added a cron entry to execute my script every morning 01:00.

            This script of yours works, but I haven't tried to import it yet.

            Comment

            • 4rk4n3
              Junior Member
              • Mar 2014
              • 8

              #7
              Hi thanks for this script

              It works, I get the zabbix's database but if i want to restore this database with the command :

              Code:
              mysql --user=user --password=password db_name < /path/of/database.sql
              I get this message :

              Code:
              ERROR 1062 (23000) at line 30: Duplicate entry '2' for key 'PRIMARY'
              And i don't know how to resolv this. Do you have an idea ?

              Comment

              • Pada
                Senior Member
                • Apr 2012
                • 236

                #8
                You'll get that error because there are already data in that table.

                You can either replace the statements like
                Code:
                LOCK TABLES `x` WRITE;
                with
                Code:
                TRUNCATE TABLE `x`; 'LOCK TABLES `x` WRITE;
                , which you can do with the following command:
                Code:
                sed -i 's/LOCK TABLES `\(.*\)`/TRUNCATAE TABLE `\1`; LOCK TABLES `\1`/g' /path/of/database.sql

                or

                you can replace the
                Code:
                INSERT INTO `x` VALUES
                with
                Code:
                REPLACE INTO `x` VALUES
                , which you can do with the following command:
                Code:
                sed -i 's/INSERT INTO `\(.*\)` VALUES/REPLACE INTO `\1` VALUES/g' /path/of/database.sql

                Comment

                • 4rk4n3
                  Junior Member
                  • Mar 2014
                  • 8

                  #9
                  Thank you for the advice, but I found a way yesterday and I forgot to say.

                  Before downloading I drop Zabbix's table and after I import the schema and the table and I don't have the error. This is an extrem way but it works ^^

                  Comment

                  • Pada
                    Senior Member
                    • Apr 2012
                    • 236

                    #10
                    I haven't actually restored my settings data yet using this method, so please let me know if its working or if I missed something!

                    Comment

                    • 4rk4n3
                      Junior Member
                      • Mar 2014
                      • 8

                      #11
                      I execute the script to retrieve the zabbix configuration.
                      To restore first I removed everything, then I joined the scheme with the command

                      mysql --user=user --password=password db_name < /path/of/database-schema.sql
                      And finally I joined the zabbix table with the command

                      mysql --user=user --password=password db_name < /path/of/database.sql
                      server configuration is finally restored.

                      Comment

                      • fsousa
                        Member
                        • Aug 2017
                        • 99

                        #12
                        Hello,

                        I'm trying to copy my entire Zabbix configuration (templates, groups, users, permissions, history, etc) from one Zabbix server to another but without success.

                        I've posted my problem here: https://www.zabbix.com/forum/zabbix-...ver-to-another

                        Any suggestion?

                        Thank you.

                        Comment

                        • andreas.schimanski
                          Junior Member
                          • Jul 2023
                          • 1

                          #13
                          Restoring the Backup with Zabbix 6 provides the Error: ERROR 1359 (HY000) at line 2379085: Trigger 'zabbix.hosts_name_upper_insert' already exists
                          The solution is to skip triggers during the backup
                          The corresponding mysqldump parameter is --skip-triggers

                          Comment

                          Working...