Backup strategies in Linux CLI - Time & Space Complexity
When we run backup commands, we want to know how long they take as our files grow.
We ask: How does the time to back up data change when we add more files?
Analyze the time complexity of the following backup script snippet.
#!/bin/bash
SOURCE_DIR="/home/user/data"
BACKUP_DIR="/backup/data"
for file in "$SOURCE_DIR"/*; do
cp "$file" "$BACKUP_DIR"
done
This script copies each file from the source folder to the backup folder one by one.
Look for repeated actions in the script.
- Primary operation: Copying each file with
cp. - How many times: Once for every file in the source directory.
As the number of files grows, the script copies more files one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 file copies |
| 100 | 100 file copies |
| 1000 | 1000 file copies |
Pattern observation: The time grows directly with the number of files.
Time Complexity: O(n)
This means the backup time increases in a straight line as you add more files.
[X] Wrong: "Copying many files takes the same time as copying one file."
[OK] Correct: Each file needs its own copy step, so more files mean more time.
Understanding how backup time grows helps you design better scripts and explain your choices clearly.
"What if we used a tool that copies all files in one command instead of looping? How would the time complexity change?"
Practice
Solution
Step 1: Understand what a backup is
A backup is a copy of important data saved separately to protect against loss.Step 2: Match the description to the options
Only To save copies of important files in a safe place describes saving copies in a safe place, which is the purpose of backups.Final Answer:
To save copies of important files in a safe place -> Option DQuick Check:
Backup = Save copies safely [OK]
- Confusing backup with deleting files
- Thinking backup speeds up the system
- Mixing backup with software updates
Solution
Step 1: Identify the correct option to copy directories recursively
The-roption incpcopies directories and their contents recursively.Step 2: Check other options for correctness
The plaincplacks-r, so it won't copy directories properly. The options with-dand-xuse invalid or unrelated flags.Final Answer:
cp -r /home/user/docs /backup/docs -> Option AQuick Check:
Copy directory = cp -r [OK]
- Forgetting the -r flag for directories
- Using wrong flags like -d or -x
- Assuming cp copies folders without options
rsync -av /home/user/data/ /backup/data/
Solution
Step 1: Understand rsync options -a and -v
The-aoption means archive mode (copy recursively and preserve attributes), and-vmeans verbose (show progress).Step 2: Analyze command behavior
Since /backup/data exists and is empty, rsync copies all files and folders from source to destination preserving attributes.Final Answer:
Copies all files and folders from /home/user/data to /backup/data preserving attributes -> Option BQuick Check:
rsync -av copies files with details [OK]
- Thinking rsync deletes files by default
- Confusing verbose with dry-run
- Assuming error if destination exists
#!/bin/bash cp -r /home/user/docs /backup/docs rm -r /home/user/docs/*
What is the main problem with this backup strategy?
Solution
Step 1: Analyze the script commands
The script copies /home/user/docs to /backup/docs, then deletes all files inside /home/user/docs.Step 2: Identify risk in deleting original files
Deleting original files after copying is risky if the copy failed or is incomplete, causing data loss.Final Answer:
It deletes original files after copying, risking data loss -> Option AQuick Check:
Deleting originals after backup = risk [OK]
- Assuming cp misses hidden files (it copies them)
- Thinking /backup/docs must exist (cp creates it)
- Ignoring risk of deleting original data
tar. Which command correctly creates a compressed backup file with the current date in its name?Solution
Step 1: Understand tar options for creating compressed archives
-ccreates an archive,-zcompresses with gzip, and-fspecifies the output file.Step 2: Check command correctness
tar -czf /mnt/backup/projects-$(date +%F).tar.gz /home/user/projects correctly creates a gzip compressed archive named with the current date in /mnt/backup from /home/user/projects.Final Answer:
tar -czf /mnt/backup/projects-$(date +%F).tar.gz /home/user/projects -> Option CQuick Check:
tar -czf + date = compressed dated backup [OK]
- Using -xzf which extracts instead of creates
- Swapping source and destination paths
- Omitting compression flag -z
Ready to prove it?
Pass the test and this pattern moves unknown
