Featured resource
Forrester Wave Report 2025
Pluralsight named a Leader in the Forrester Wave™

Our tech skill development platform earned the highest scores possible across 11 criteria.

Learn more
  • Labs icon Lab
  • Cloud
Google Cloud Platform icon
Labs

Implementing a Backup Solution with LXD

One major benefit to Linux containers is the ability to spin up the containers quickly. This makes them an ideal solution for providing a throwaway testing environment. For example, we may want to take, and then verify, backups of any one of our important configuration directories. In this hands-on lab we'll do just that, and create a temporary Alpine Linux 3.11 environment that will extract our backups, then check it against the original files to ensure the backup is accurate.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 30m
Published
Clock icon Apr 03, 2020

Contact sales

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

Table of Contents

  1. Challenge

    Create a Backup of /etc/nginx

    Create the script file:

    vim web-backups.sh
    

    We're first going to create an initial log file:

    #! /bin/bash
    
    touch /home/cloud_user/web-bu-log
    

    Then pull down our /etc/nginx directory from the provided server and compress it:

    echo "Creating nginx backups" >> /home/cloud_user/web-bu-log
    
    lxc file pull web01/etc/nginx /tmp/ -r --create-dirs
    tar -czvf /tmp/nginx.tar.gz /tmp/nginx/
    
  2. Challenge

    Create a Container for Backups Testing

    We now want to create a container that is using the same distribution as the web01 containers, Alpine 3.11:

    echo "Lauching test container" >> /home/cloud_user/web-bu-log
    
    lxc launch images:alpine/3.11 web-bu-test
    lxc exec web-bu-test -- apk update
    
  3. Challenge

    Extract the Backups on the Container

    From here, we want to add our nginx.tar.gz file, and extract it:

    lxc file push /tmp/nginx.tar.gz web-bu-test/tmp/nginx.tar.gz
    lxc exec web-bu-test -- tar -xf /tmp/nginx.tar.gz -C /tmp/
    
  4. Challenge

    Compare the Backup to the Original

    Let's now pull down the same file from both the original and the backup server, and compare them to see if they match. If they do match, we'll consider it a success and clean our our environment:

    lxc file pull web01/etc/nginx/conf.d/default.conf /tmp/default.conf-original
    lxc file pull web-bu-test/tmp/tmp/nginx/conf.d/default.conf /tmp/default.conf-backup
    
    echo "Testing backups to original" >> /home/cloud_user/web-bu-log
    
    if diff /tmp/default.conf-original /tmp/default.conf-backup > /dev/null;
    then
     echo "Success!" >> /home/cloud_user/web-bu-log
     lxc stop web-bu-test
     lxc delete web-bu-test
     rm -rf /tmp/nginx
     rm /tmp/default.conf-*
    

    If it does not match, we'll make a copy of the container and leave it up for review:

    else
    
     echo "!!BACKUP TEST FAILED!!" >> /home/cloud_user/web-bu-log
     echo "!!SEE web-bu-fail_* CONTAINER!!" >> /home/cloud_user/web-bu-log
     lxc snapshot web-bu-test fail
     lxc copy web-bu-test/fail web-bu-fail_$(date +%Y%m%d)
     lxc stop web-bu-test
     lxc delete web-bu-test
     rm -rf /tmp/nginx
     rm /tmp/default.conf-*
    fi
    
  5. Challenge

    Test Script

    Save and exit the completed file, which should look like:

    #! /bin/bash
    
    touch /home/cloud_user/web-bu-log
    
    echo "Creating nginx backups" >> /home/cloud_user/web-bu-log
    
    lxc file pull web01/etc/nginx /tmp/ -r --create-dirs
    tar -czvf /tmp/nginx.tar.gz /tmp/nginx/
    
    echo "Lauching test container" >> /home/cloud_user/web-bu-log
    
    lxc launch images:alpine/3.11 web-bu-test
    lxc exec web-bu-test -- apk update
    lxc file push /tmp/nginx.tar.gz web-bu-test/tmp/nginx.tar.gz
    lxc exec web-bu-test -- tar -xf /tmp/nginx.tar.gz -C /tmp/
    lxc file pull web01/etc/nginx/conf.d/default.conf /tmp/default.conf-original
    lxc file pull web-bu-test/tmp/tmp/nginx/conf.d/default.conf /tmp/default.conf-backup
    
    echo "Testing backups to original" >> /home/cloud_user/web-bu-log
    
    if diff /tmp/default.conf-original /tmp/default.conf-backup > /dev/null;
    then
     echo "Success!" >> /home/cloud_user/web-bu-log
     lxc stop web-bu-test
     lxc delete web-bu-test
     rm -rf /tmp/nginx
     rm /tmp/nginx.tar.gz
     rm /tmp/default.conf-*
    else
    
     echo "!!BACKUP TEST FAILED!!" >> /home/cloud_user/web-bu-log
     echo "!!SEE web-bu-fail_* CONTAINER!!" >> /home/cloud_user/web-bu-log
     lxc snapshot web-bu-test fail
     lxc copy web-bu-test/fail web-bu-fail_$(date +%Y%m%d)
     lxc stop web-bu-test
     lxc delete web-bu-test
     rm -rf /tmp/nginx
     rm /tmp/nginx.tar.gz
     rm /tmp/default.conf-*
    fi
    

    Now test it:

    chmod +x web-backups.sh
    ./web-backups.sh
    

    To confirm that it was successful, review the log:

    cat web-bu-log
    
  6. Challenge

    Add to Cron

    Add the script to your crontab so it runs at midnight, daily:

    crontab -e
    
    0 0 * * * /home/cloud_user/backups.sh
    

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

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.