Step 4: Create a Simple Backup Script
Table of Contents
Objective: Create a .sh script to backup directories in ~/.
Resources
Backing up Data
It is easy to delete a Docker container and the volume data accidentally.
The difference is using -v flag when you call docker-compose down.
docker-compose downmeans that your volume data is saved
docker-compose down -vmeans that your volume data is deleted
It is easy to remove the wrong folder.
For example, you have two folders:
my-docker-project
my-docker-project-tempWhat happens if you want to remove a directory using
rm -rand you select the wrong folder?
You want to do this:
rm -r my-docker-project-tempBut, you accidentally push Enter before using autocomplete to list the full path, which is
rm -r my-docker-project-temp.
What happens if your project database becomes corrupt? Or, you are doing database maintenance, and you realized that you just removed the wrong table.
Create a Backup Script
Your task is to write a script that copies specific folders from
~/ to /var/user-backups.
First, make a list of which folders you want to backup
Use
ls -lhto view the folders in your home directory
Next, create the
user-backupsdirectoryCopy the files manually to verify that the command work
Use the
cpcommand to copy the folders.
cp -rfv source destinationcpcopies files or folders from the source to the destination.-rflag copy recursively (directories).-fflag does not prompt the user when overwriting existing data.-vflag enables verbosity and displays the results.sourceis the source directory or file.destinationis the destination directory or file.
Hint
Use the full path in the script: use
/root/my-projectinstead of~/my-projectFor example, you might want to use this command to backup your VPN data:
cp -rfv /root/docker-openvpn /var/user-backups/docker-openvpn
Verify that the data copied
Copy them again manually to verify that the commands work without user interaction.
Then, add the commands to the script.
Execute that script to verify that the files were copied to the specified directory.