Zabbix backup using Zaf

How to backup data in Zabbix automaticaly useable way ?

It is nightmare for Zabbix administrator to miss some data in Zabbix. If somebody changes data in template or host, it is hard process to get them back. It is possible to use database backup but it is very slow process. And even more, it is not tuneable. So you have to recover everything or nothing.

Zaf is the solution

Even if Zaf means "Zabbix agent framework", it has one big advanatage. It can communicate directly with Zabbix API and get informations from there. It is side effect of Zaf (primary mission of this feature will be discused in another article) but it is useable for backup. Result of Zaf backup will be directory with XML files. Each file belongs to exported host and exported template. This is very flexible and useable way. Any time you can look into directory, get template or host from backup and apply it back to Zabbix. You can even backup more sophisticaly if you use more directories per days, weeks or months. And, what is great, Zaf has no dependencies at all. It needs only curl. Everything is shell based script. Example of backup structure:

ls /var/backups/zabbix-xml-hosts/


ls /var/backups/zabbix-xml-templates/

Template App ICMP ping.xml
Template App ISC DHCP.xml

Preparing Zaf

First of all, we have to prepare Zaf. It is quite easy with installation over network, described in project pages. In our example, we will install Zaf directly on Zabbix server or you can use some backup server which has API access to your Zabbix server. To be able to backup everything, you must create Zabbix user with required permissions to read all hosts and templates and give him access to API. Next to this, you have to be root to install zaf. You must have zabbix agent already installed. If you do not have zabbix agent on system, installation will fail! Do not forget to install curl.

curl -k | \
 sh -s auto \
 Z_Server=<your_zabbix_hostname> \
 Z_ServerActive=<your_zabbix_hostname> \
 ZAF_ZBXAPI_URL=<your_url_to_api_json_rpc.php> \
 ZAF_ZBXAPI_USER=<your_user> \

# Example to install zaf directly on Zabbix server
curl -k | \
 sh -s auto \
 Z_Server=localhost \
 Z_ServerActive=localhost \
 ZAF_ZBXAPI_URL=http://localhost/zabbix/api_jsonrpc.php \
 ZAF_ZBXAPI_USER=zaf_backup \

Yes, it is not secure, but easy way :) If you want to use packaged version of Zaf, follow instructions in project page. Next to this, you can import zaf template for your host into Zabbix. After applying, you will see installed zaf version and its plugins. If you have some questions or problems, you can use github project page to contact me.

After install, please do not forget to remove zaf cache directory. Zaf will create another one after you switch to another user.

rm -rf /tmp/zafc/

Until now, you had to be root. Now switch to some another account. We will need only rights to export directories.

To test zaf installation, type

zaf version

It should print zaf version. Next to this, we will check access to the API.

zaf api get-host-ids

If everything was wrong. you should see all your hostids accessible by zaf user. If something is wrong, please check permissions of your user, check your access to the API and all related settings. You can find Zaf config at /etc/zaf.conf.

Now it is very easy. To backup all templates, use next command. Please note that it will take long time to run. And please take care of cleanup zaf cache directory because zaf unfortunately caches credentials (will be repaired in master).

mkdir -p zabbix-xml-templates
zaf cache-clean
zaf api export-templates zabbix-xml-templates/
zaf cache-clean

mkdir -p zabbix-xml-hosts
zaf cache-clean
zaf api export-templates  zabbix-xml-hosts/
zaf cache-clean

You can find more info about api commands by entering next command. It is even possible to export only hosts or templates  belonging to some group.

zaf api

/usr/bin/zaf api command [parameters]
get-host-id host                        Get host id                                                                     
get-byid-host id [property]             Get host property from id. Leave empty property for JSON                        
get-template-id template                Get template id                                                                 
get-byid-template id [property]         Get template property from id. Leave empty property for JSON                    
get-map-id map                          Get map id                                                                      
get-byid-map id [property]              Get map property from id. Leave empty property for JSON                         
get-inventory host [fields]             Get inventory fields [or all fields]                                            
get-host-ids [hg]                       Get all hostids or hostids in group hg                                          
get-template-ids [hg]                   Get all templateids or templateids in group hg                                  
get-map-ids                             Get all mapids                                                                  
export-hosts dir [hg]                   Backup all hosts [in group hg] (get their config from zabbix and save to dir/hostname.xml)
export-host host                        Backup host (get config from zabbix to stdout)                                  
import-template {plugin|file}           Import template for plugin or from file                                         
export-template name                    Export template to stdout                                                       
export-templates dir                    Export all template to dir

You are done! You have all your hosts and templates exported in useable way and you can recover it host by host or template by template withou affecting Zabbix. You can put this script into cron to do it daily. Even more, you can have more directories for more versions in history.

Poslední komentáře

Bez komentářů.

Kdo je nový

  • radimk
  • limo