Directory Backup Strategies

ShellBeginner
Practice Now

Introduction

Imagine taking a photo every day of a growing plant. If you name every photo plant.jpg, the new photo will constantly overwrite the old one, and you'll lose the history. To keep them all, you need to name them plant_2023-01-01.jpg, plant_2023-01-02.jpg, and so on.

The same problem happens with system backups. If you always write to the same filename, each new backup can overwrite the previous one and erase your recovery history.

In this lab, you will learn how to automate this naming process. Timestamped backups are a very common real-world practice because backup files are only useful if you can tell when they were created and keep multiple generations.

You will learn to:

  1. Ask Linux for today's date in a specific format.
  2. Store that date in a "box" (variable).
  3. Use that date to create a unique, timestamped backup file.

Get Today's Date

In this step, you will learn how to use the date command to format the current date. This is the first ingredient for our dynamic filename.

The standard date command prints the current date and time in a long, wordy format. But for filenames, we usually want a clean, sortable format like YYYY-MM-DD. Linux allows you to customize the output using the + symbol followed by a format code. The %F code is a handy shortcut for "Full Date" (Year-Month-Day).

This format is not just pretty. It is operationally useful because filenames sorted alphabetically will also sort in chronological order when you use YYYY-MM-DD.

First, verify that you are in your project folder:

cd ~/project

Now, ask the system for the date in our preferred format:

date +%F

You should see an output like 2023-10-27 (but with today's actual date).

To use this result in a filename, we need to capture it. We can do this using Command Substitution. Think of this as putting a command inside a bubble $(...). The shell runs the command inside the bubble first, grabs the result, and replaces the bubble with that text.

Command substitution is one of the core shell ideas that begins to connect simple command-line use with automation. It lets command output become data for another command.

Let's try to store this date into a Variable. A variable is just a named storage box. We will create a box named current_date and put the output of our command inside it.

current_date=$(date +%F)

Important: Notice there are NO spaces around the = sign. This is a strict rule in the shell. variable = value will fail; variable=value works.

Now, let's peek inside our box to make sure the date is there. We use the echo command with a $ sign before the variable name to "unlock" the box:

echo $current_date

If you see today's date printed back, you have successfully captured time effectively!

Create a Backup File

Now that we have the date stored in a variable, we can use it to create a smartly named backup file.

We have prepared a file named project_plan.txt in your directory. Verify it's there:

ls -l project_plan.txt

Our goal is to create a copy of this file named project_plan_YYYY-MM-DD.bak. This effectively "stamps" the copy with today's date.

We will use the standard cp (copy) command. But instead of typing the date manually, we will let the shell insert it for us using our variable $current_date.

Run the following command:

cp project_plan.txt "project_plan_$current_date.bak"

The shell sees the $current_date inside the double quotes and immediately replaces it with the actual date stored inside (e.g., 2023-10-27) before running the copy command.

This is called variable expansion. The shell expands variables before it actually launches the cp command, which is why the command feels dynamic even though cp itself does not understand shell variables.

Pro Tip: You can actually skip the variable step and do it all in one line! This is extremely common in automation scripts. You can put the command bubble $(...) directly into the cp command:

cp project_plan.txt "project_plan_$(date +%F).bak"

This tells the shell: "Run the date command, paste the result right here, and then run the copy command." Both methods achieve the exact same result.

Verify the Backup

Let's confirm that our magic worked. We should now see two files: the original and the timestamped copy.

List the files in the directory:

ls -l

You should see something like this:

-rw-rw-r-- 1 labex labex 65 Oct 27 10:00 project_plan.txt
-rw-rw-r-- 1 labex labex 65 Oct 27 10:05 project_plan_2023-10-27.bak

Notice the new file has the date embedded in its name.

Finally, let's make sure the content inside is safe. We can use the cat command to read the new file. Since we might not want to type the full date again, we can reuse our command substitution trick:

cat project_plan_$(date +%F).bak

You should see the original text:

Phase 1: Initialization
Phase 2: Development
Phase 3: Deployment

Success! You have created a perfect, timestamped backup. If you ran this same command tomorrow, it would create a new file with tomorrow's date, keeping your history safe and organized.

This simple naming habit scales surprisingly far. Many backup rotation schemes begin with exactly this idea: encode time in the filename so you can keep generations instead of overwriting the latest copy.

Summary

Congratulations! You have mastered a key automation skill.

In this lab, you learned:

  • Formatting Time: Using date +%F to get a computer-friendly date string.
  • Command Substitution: Using $(...) to feed the result of one command into another.
  • Dynamic Naming: Creating files that name themselves based on the current date.

This seemingly simple trick is the backbone of professional backup strategies. You can now ensure your files are never accidentally overwritten again!