Implementing a Backup Solution with LXD

30 minutes
  • 6 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

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/
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
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/
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
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
Add to Cron

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

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

Additional Resources

You want to take periodic backups of your web01 container's nginx configuration, primarily the /etc/nginx directory, which stores all your important files. Create a script that will copy the directory down, compress it, then re-deploy it on an alternative container to confirm the sanctity of the backup.

When finished, add the script to the crontab so it runs daily at midnight.

What are Hands-on Labs

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.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?