Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Configure a Backup of Nagios

In this lab we cover how to create a backup of Nagios files. You might think that you won't need it, but all systems are vulnerable. As time passes, the chances you will need a backup grow. Why risk it? Let's see how it's done and be safe!

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Advanced
Duration
Clock icon 3h 0m
Published
Clock icon Jul 19, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Configure Key-Based Authentication Between the Server and Client

    1. In the Nagios server terminal, generate an SSH key pair.

      sudo su
      
      ssh-keygen
      
    2. Open the key file.

      vim /root/.ssh/id_rsa.pub
      
    3. Select the entire key and copy it to your buffer.

    4. In the Linux client terminal, edit the authorized_keys file.

      vim /home/cloud_user/.ssh/authorized_keys
      
    5. Go to the bottom of the file and paste the key copied from the server.

    6. Save your changes and exit the editor.

    7. Back in the server terminal, exit the editor.

  2. Challenge

    Create a Bash Script Which Will Create a Compressed File and Copy It to the Client, then Perform a Hash Sum Verification and Verify the Integrity of the Copied Backup File

    1. Create the file.

      vim /home/cloud_user/nagiosBackup.sh
      
    2. Add the following text to the file.

      #!/bin/bash
      
      DIR_PATH_1="/usr/local/nagios"
      timeStamp=$(date +%Y-%m-%d-%H-%M-%S-%s)
      serverIP=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print$2}' | cut -f1  -d'/' | tr . -)
      remoteBackupFilePath="/home/cloud_user/"
      
      tar -czvf $timeStamp-Nagios-Core-$serverIP.tar.gz $DIR_PATH_1
      
      HASH=$(md5sum $timeStamp-Nagios-Core-$serverIP.tar.gz | awk -F" " '{print $1}')
      
      fromName="$timeStamp-Nagios-Core-$serverIP.tar.gz"
      toName="$timeStamp-$HASH-Nagios-Core-$serverIP.tar.gz"
      
      mv $fromName $toName
      
      scp $timeStamp-$HASH-Nagios-Core-$serverIP.tar.gz cloud_user@$1:$remoteBackupFilePath
      
      remoteHash=$(ssh cloud_user@$1 "md5sum /home/cloud_user/$timeStamp-$HASH-Nagios-Core-$serverIP.tar.gz")
      
      remoteHash=$(echo "$remoteHash" | awk -F" " '{print $1}')
      
      if [[ $HASH == $remoteHash  ]];
      then
          echo "SUCCESS - FILE: " $timeStamp-$HASH-Nagios-Core-$serverIP.tar.gz "was copied succesfully to:     " $1 >> /home/nagios/customBackupLog.log
          rm -rf $timeStamp-$HASH-Nagios-Core-$serverIP.tar.gz
      else
          echo "FAIL    - FILE: " $timeStamp-$HASH-Nagios-Core-$serverIP.tar.gz "was not copied succesfully to: " $1 >> /home/nagios/customBackupLog.log
      fi
      
    3. Save your changes and exit the editor.

    4. Update the privileges for your script.

      chmod +x /home/cloud_user/nagiosBackup.sh
      
    5. Change the ownership of the script.

      sudo chown root:root /home/cloud_user/nagiosBackup.sh
      
  3. Challenge

    Configure the Script to Automatically Run

    1. Still in the server terminal, log in as root.

      sudo su
      
    2. Open the scheduling file.

      crontab -e
      
    3. Add the following text to the file.

      59 23 * * * /home/cloud_user/nagiosBackup.sh <Client IP Address>
      
    4. Save your changes and exit the editor.

    5. Run the script once manually as root

      sudo su
      
      /home/cloud_user/nagiosBackup.sh <Client IP Address>
      
  4. Challenge

    Perform a Test Run of the Backup Script

    1. Create a key pair for the root user.

      1. In the Nagios server terminal, generate an SSH key pair.

        ssh-keygen
        
      2. Open the key file.

        vim /home/root/.ssh/id_rsa.pub
        
      3. Select the entire key and copy it to your buffer.

      4. In the Linux client terminal, edit the authorized_keys file.

        vim /home/cloud_user/.ssh/authorized_keys
        
      5. Go to the bottom of the file and paste the key copied from the server.

      6. Save your changes and exit the editor.

      7. Back in the server terminal, exit the editor.

    2. Run the script.

    /home/cloud_user/nagiosBackup.sh PUBLIC_LINUXCLIENT_IP
    
    1. In the client, verify the backup worked.
    ll
    
    1. In the server, update the scheduled task.
    crontab -e
    
    1. Add the Linux client public IP to the end of the line after nagiosBackup.sh.

    2. Save your changes and exit the editor.

    3. Check the log file to verify the process.

    tail -f /home/nagios/customBackupLog.log
    
  5. Challenge

    Simulate an Error and Restore from a Backup

    1. In the server terminal, delete localhosts.cfg.
    ```bash
    sudo rm -rf /usr/local/nagios/etc/objects/localhost.cfg
    ```
    
    1. In the client terminal, extract the backup.

      cd /home/cloud_user/
      tar -xvzf backup_file_name
      
    2. Generate an SSH key pair.

      ssh-keygen
      
    3. Open the key file.

      vim /home/cloud_user/.ssh/id_rsa.pub
      
    4. Select the entire key and copy it to your buffer.

    5. In the server terminal, edit the authorized_keys file.

      vim /home/cloud_user/.ssh/authorized_keys
      
    6. Go to the bottom of the file and paste the key copied from the server.

    7. Save your changes and exit the editor.

    8. Back in the client terminal, exit the editor.

    9. Transfer the missing file from the client to the server.

      scp usr/local/nagios/etc/objects/localhost.cfg cloud_user@PUBLIC_NAGIOSSERVER_IP:/home/cloud_user/
      
    10. In the server terminal, verify the file has been transferred.

      ll
      
    11. Move the file to the appropriate location.

      mv localhost.cfg /usr/local/nagios/etc/objects/
      
    12. Transfer to the destination directory.

      cd /usr/local/nagios/etc/objects/
      
    13. Change ownership of the file.

      chown nagios:nagios localhost.cfg
      
    14. Perform a preflight check.

      sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
      
    15. Verify there were no warnings or errors.

    16. Restart the Nagios server.

      sudo systemctl restart nagios
      

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans