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

Installing Zabbix Version 2.4 Offline (Zabbix Server without Internet).

There might be situations where you have a remote/zabbix server which does not have internet connectivity, due to security or other reasons. So we create a custom repo on the remote/zabbix server so that we can install zabbix using rpms Here is how we are planning to do this. Download all the dependency rpms on a machine which has internet connection, using yum-downloadonly or repotrack . Transfer all the rpms to the remote server. Create a repo on the remote server. Update yum configuration. Install. NOTE: This method can be used to install any application, but here we have used zabbix as we had this requirement for a zabbix server. Download dependent rpms . On a machine which has internet connection install the package below. And download all the rpms . Make sure the system are similar (not required to be identical - At-least the OS should be of same version) mkdir /zabbix_rpms yum install yum-downloadonly Downloading all the rpms to location /zabbix_rpms/ ,...

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...

Setting up ssl https On Nagios XI Server

HTTPS is a protocol for secure communication over a computer network which is widely used on the Internet. HTTPS consists of communication over Hypertext Transfer Protocol (HTTP) within a connection encrypted by Transport Layer Security or its predecessor, Secure Sockets Layer. The main motivation for HTTPS is authentication of the visited website and protection of the privacy and integrity of the exchanged data. Intro Courtesy Wikipedia Full SSL support requires Nagios XI version 2011R1.6 or later. Before we start. Check if the below packages are install, they should be if you are using latest Nagios XI, but check them anyways. yum install mod_ssl openssl Creating Key and Certificate Lets generate the key for the server. openssl genrsa -out ca.key 2048 Output for the command. [ahmed@nagiosserver ~]$ openssl genrsa -out ca.key 2048 Generating RSA private key, 2048 bit long modulus ...........................................................................................