Skip to main content

Zabbix Database Backup Script for `postgres` and `mysql`.

After the zabbix setup is done, we would like to take backup of the database. Here we are doing a complete backup. But if we are looking for only configuration backup then we can ignore the history* tables from the database, but for now we are taking complete backup. Backups are taken everyday and last 3 days backups are stored and rest for removed.
What does the script do.
  1. Take backup of the database zabbix, given by variable BASE='zabbix'
  2. Archive the backup and Remove old backups (older than 3 days).
Here is the script flow.
Create a directory if it does not exist, this will run only once for the first time.
# Check if directory exsists.
if [ ! -d $BACKUP_DIR ];
then
    mkdir -p $BACKUP_DIR
fi
If the directory was created successfully, then take backup.
# Check if directory exsists.
if [ -d $BACKUP_DIR ];
then
    # Take a DB backup
    backup_postgres_db

    # Creating Archives.
    create_archive
fi

Setting up the scripts in postgres.

Logon to the server and su to postgres user. $HOME for postgres user is /var/lib/pgsql.
  1. Create/Copy the postgres script to this location /var/lib/pgsql.
  2. Change permission to execute chmod 777 /var/lib/pgsql/zabbix_db_backup_postgres.sh
  3. Create a directory for the backup. Similar as /zabbix_db_backup/postgres_complete_backup/.
  4. Set owner to postgres which will be running the script for backup location in step 3.
  5. Update script with new path BACKUP_DIR=/zabbix_db_backup/postgres_complete_backup/
  6. Update script with below information.
    1. Database username : USERDB='zabbix',
    2. Database password : PASSWD='zabbix',
    3. Database : BASE='zabbix'
  7. Create a file called .pgpass. To have the script run without the password, we need to have the .pgpass file in the $HOME of the postgres user. Format of the file is a below. (: seperated values)
  8. Set permission to .pgpass file as 600
Here is a template for the .pgpass file.
::::
Example: one database in one line. (Set file permissions to 0600)
localhost:5432:zabbix_db_name:zabbix_user_name:zabbix_password

Here are the command output.

[Zubair.ahmed@nms2 /]$ sudo su postgres
[sudo] password for Zubair.ahmed:
bash-4.1$ pwd
/
bash-4.1$ cd ~
bash-4.1$ pwd
/var/lib/pgsql
bash-4.1$ ls -la
total 36
drwx------.  3 postgres postgres 4096 Jan  6 00:27 .
drwxr-xr-x. 43 root     root     4096 Jan  5 03:09 ..
drwx------.  4 postgres postgres 4096 Oct  6 20:42 9.4
-rw-------.  1 postgres postgres 1690 Jan  6 00:27 .bash_history
-rwx------.  1 postgres postgres  267 Nov 30 16:12 .bash_profile
-rw-------   1 postgres postgres   38 Dec 17 00:01 .pgpass
-rw-------.  1 postgres postgres  547 Dec 17 00:23 .psql_history
-rw-------   1 postgres postgres 3100 Jan  6 00:27 .viminfo
-rwxrwxrwx   1 postgres postgres 2186 Jan  5 23:19 zabbix_db_backup_postgres.sh
bash-4.1$
Here is how the .pgpass file looks like.
bash-4.1$ cat .pgpass
localhost:5432:zabbix:zabbix:VeryD!f1cultPassw0rd
bash-4.1$

Update crontab postgres

Updating the crontab with below command.
crontab -e
Update the crontab with below contents.
# Crontab information on how it works.
# +----------------> minute (0 - 59)
# |  +-------------> hour (0 - 23)
# |  |  +----------> day of month (1 - 31)
# |  |  |  +-------> month (1 - 12)
# |  |  |  |  +----> day of week (0 - 6) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *  command to be executed

# Execute backuo of Zabbix Database Every Week.
0 0 * * * /var/lib/pgsql/zabbix_db_backup_postgres.sh
Script will run everyday at 00:00 hrs, and keeps backup for 3days.

Setting up the scripts in mysql.

Log on to the user which will be used for taking backups.
  1. Create/Copy the postgres script to user home location $HOME.
  2. Change permission to execute chmod 777 $HOME/zabbix_db_backup_postgres.sh
  3. Create a directory for the backup. Similar as /zabbix_db_backup/postgres_complete_backup/.
  4. Set owner to the user which will be running the script for backup location in step 3.
  5. Update script with new path BACKUP_DIR=/zabbix_db_backup/postgres_complete_backup/
  6. Update script with
    1. Database username : USERDB='zabbix',
    2. Database password : PASSWD='zabbix',
    3. Database : BASE='zabbix'

Update crontab mysql

Updating the crontab with below command.
crontab -e
Update the crontab with below contents.
# Crontab information on how it works.
# +----------------> minute (0 - 59)
# |  +-------------> hour (0 - 23)
# |  |  +----------> day of month (1 - 31)
# |  |  |  +-------> month (1 - 12)
# |  |  |  |  +----> day of week (0 - 6) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *  command to be executed

# Execute backuo of Zabbix Database Every Week.
0 0 * * * /var/lib/pgsql/zabbix_db_backup_mysql.sh
Script will run everyday at 00:00 hrs, and keeps backup for 3days.

Comments

  1. Hi, Your github repo is not available. Please upload the zabbix_db_backup_postgres.sh in github

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I found it.

    https://github.com/ahmedzbyr/zabbix-automations

    ReplyDelete
  4. can you please create the history_table_clean_up_db_script.sh for postgresql as well ?

    ReplyDelete

Post a Comment

Popular posts from this blog

Cloudera Manager - Duplicate entry 'zookeeper' for key 'NAME'.

We had recently built a cluster using cloudera API’s and had all the services running on it with Kerberos enabled. Next we had a requirement to add another kafka cluster to our already exsisting cluster in cloudera manager. Since it is a quick task to get the zookeeper and kafka up and running. We decided to get this done using the cloudera manager instead of the API’s. But we faced the Duplicate entry 'zookeeper' for key 'NAME' issue as described in the bug below. https://issues.cloudera.org/browse/DISTRO-790 I have set up two clusters that share a Cloudera Manger. The first I set up with the API and created the services with capital letter names, e.g., ZOOKEEPER, HDFS, HIVE. Now, I add the second cluster using the Wizard. Add Cluster->Select Hosts->Distribute Parcels->Select base HDFS Cluster install On the next page i get SQL errros telling that the services i want to add already exist. I suspect that the check for existing service names does n

Zabbix History Table Clean Up

Zabbix history table gets really big, and if you are in a situation where you want to clean it up. Then we can do so, using the below steps. Stop zabbix server. Take table backup - just in case. Create a temporary table. Update the temporary table with data required, upto a specific date using epoch . Move old table to a different table name. Move updated (new temporary) table to original table which needs to be cleaned-up. Drop the old table. (Optional) Restart Zabbix Since this is not offical procedure, but it has worked for me so use it at your own risk. Here is another post which will help is reducing the size of history tables - http://zabbixzone.com/zabbix/history-and-trends/ Zabbix Version : Zabbix v2.4 Make sure MySql 5.1 is set with InnoDB as innodb_file_per_table=ON Step 1 Stop the Zabbix server sudo service zabbix-server stop Script. echo "------------------------------------------" echo " 1. Stopping Zabbix Server &quo

Access Filter in SSSD `ldap_access_filter` [SSSD Access denied / Permission denied ]

Access Filter Setup with SSSD ldap_access_filter (string) If using access_provider = ldap , this option is mandatory. It specifies an LDAP search filter criteria that must be met for the user to be granted access on this host. If access_provider = ldap and this option is not set, it will result in all users being denied access. Use access_provider = allow to change this default behaviour. Example: access_provider = ldap ldap_access_filter = memberOf=cn=allowed_user_groups,ou=Groups,dc=example,dc=com Prerequisites yum install sssd Single LDAP Group Under domain/default in /etc/sssd/sssd.conf add: access_provider = ldap ldap_access_filter = memberOf=cn=Group Name,ou=Groups,dc=example,dc=com Multiple LDAP Groups Under domain/default in /etc/sssd/sssd.conf add: access_provider = ldap ldap_access_filter = (|(memberOf=cn=System Adminstrators,ou=Groups,dc=example,dc=com)(memberOf=cn=Database Users,ou=Groups,dc=example,dc=com)) ldap_access_filter accepts standa