Step 4: Create a Simple Backup Script

Objective: Create a .sh script to backup directories in ~/.

Resources

Backing up Data

It is easy to accidentally delete a Docker container and the volume data. The difference is using -v flag when you call docker-compose down.

  • docker-compose down means that your volume data is saved

  • docker-compose down -v means 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-temp

What happens if you want to remove a directory using rm -r and you select the wrong folder?

  • You want to do this: rm -r my-docker-project-temp

  • But, 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.

  1. First, make a list of which folders you want to backup

    • Use ls -lh to view the folders in your home directory

  2. Next, create the user-backups directory

  3. Copy the files manually to verify that the command work

    • Use the cp command to copy the folders.

cp -rfv source destination
  • cp copies files or folder from the source to the destination.

  • -r flag copy recursively (directories).

  • -f flag does not prompt the user when overwriting existing data.

  • -v flag enables verbosity and display the results.

  • source is the source directory or file.

  • destination is the destination directory or file.

Hint

Use the full path in the script: use /root/my-project instead of ~/my-project

For example, you might want to use this command to backup your VPN data:

cp -rfv /root/docker-openvpn /var/user-backups/docker-openvpn
  1. Verify that the data copied

  2. Copy them again manually to verify that the commands work without user interaction.

  3. Then, add the commands to the script.

  4. Execute that script to verify that the files copied.