Backup and Archive Nagios
This is a shell script that can be used to backup a Nagios configuration (or any group of files/directories in Linux or UNIX) locally, and to sync the backups to a remote location. This script works perfectly when there are two different Nagios instances in different locations, and this script can be used on both servers to backup and archive, and then rsync the files to the remote side – just by changing the three variables at the top of the script. Logging and emailing results of each job can be added in to the script as well.
A best practice that I implemented is to use SSH shared keys for the rsync. Use a non-root account and send the traffic along a trusted VLAN. This allows for the SSH to not prompt for a password every time the script is run, which should be automated through a cron job.
#!/bin/bash #################################### ###### Local System Variables ###### #################################### NAGIOS=usr/local/nagios LOCAL=/local/directory/path REMOTE=user@server:/local/directory/path #################################### ####### DO NOT CHANGE BELOW ######## #################################### BACKUP=$LOCAL/nagios-backup.tgz DATE=`date +"%F-%T"` export LOCAL export REMOTE ### check to see if current backup file exists ### if [ -f $BACKUP ] then echo "Backup file exists." mv $BACKUP $BACKUP-$DATE tar czf $BACKUP -C / $NAGIOS else echo "Backup file does not exist...creating." tar czf $BACKUP -C / $NAGIOS exit fi ### remove files older than seven days ### find $LOCAL -type f -mtime +7 -exec rm {} \; ### change the permissions of the file to the backups user ### chown -R backups:backups $LOCAL ### change to backups user to run the rsync script ### su backups -c /home/backups/rsync-files.sh ### rsync the files to the DR backup site ### rsync -avz --delete $LOCAL/ $REMOTE