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