| by TheIneptOne | 1 comment

Backing Up Nagios to Github Automagically

I wanted to backup my Nagios config files without having to backup the whole system as well as keep a revision history. So, I setup a repo on github and created a bash script to automatically push changes to the repo once a day.

Git Setup

First thing, before we can run the script, we need to create a repo on Github and setup a personal access token. We’ll also need to setup our Git user info

git config --global user.name "Inept Tech"
git config --global user.email "inepttech@domain.com"

I want my credentials with access token to be store

git config --global credential.helper store

I keep my Nagios config in /opt/nagios. Since Git want’s an empty folder for the initial clone, I renamed my folder to nagios-old temporarly so I can clone the repo to /opt/nagios

sudo mv -f /opt/nagios /opt/nagios-old

Now we can clone the repo

git clone https://github.com/inepttech/nagios-backup.git nagios

When prompted, enter your github username and enter the access token for the password

Move the Nagios config back to the /opt/nagios folder

sudo mv -f /opt/nagios-old /opt/nagios

Bash Script

The bash script will do a number of things. It will will verify Git’s installed location, URL, and branch. It will then add all files, commit, and push any changes to the repo. Everything is commented and should be pretty straight forward

#!/bin/bash

#
# This script will check if any changes have been made
# to the Nagios files in the last 24 hours, and if so,
# will do a git add, commit, and push to gitlab.
#
# Created Jan 22, 2022 - IneptTech.com
#

# Changes to the Nagios config directory
cd /opt/nagios

# Gets the location of the bin file
gitbin=`which git`

# Location of the repo
repo_dir="/opt/nagios"

# URL of the repo
git_repo=https://github.com/inepttech/nagios-backup.git

# Branch of the repo
current_branch=$(git branch | grep "*" | cut -b 3-)

# Get's todays date and formats YYYYMMDD
# as well as echo's the date for the log file
todays_date=`date +"%Y%m%d %I:%M%P"`
echo $todays_date

# Compares the saved GIT URL to the repo URL before pushing
git_repo_check=`git config --get remote.origin.url`

if [ "$git_repo_check" == "$git_repo" ]; then
    # If everything is good, git will add, commit, and push
    ${gitbin} add -A
    ${gitbin} commit -m "Auto Backup - $todays_date"
    ${gitbin} push origin $current_branch
else
    # If there's an error, the script will exit
    exit 0
fi

# Add task completed text for log file and exit script
printf "Task completed. Exiting \n\n"
exit 0

Cron Setup

To run the script automatically, we’ll use crontab

crontab -e

In crontab, we’ll set the script to run every day at 6PM and output any terminal output to /opt/nagios-backup.log

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

# Nagios Backup Script
0 18 * * * /bin/bash /opt/nagios-backup.sh 1>> /opt/nagios-backup.log

That’s it. You should automatic pushes to Github for any of your nagios config changes

Done!

1 Comment

Nagios 4.4.6 on Ubuntu 20.04 – Inept Tech

February 3, 2022 at 6:39 pm Reply

[…] Backing Up Nagios to Github Automagically […]

Leave a Reply