Backup and recovery strategies in Raspberry Pi - Time & Space Complexity
When working with backup and recovery on a Raspberry Pi, it is important to understand how the time needed grows as the amount of data increases.
We want to know how long the backup or recovery process will take when the data size changes.
Analyze the time complexity of the following backup code snippet.
files = get_all_files('/home/pi/data')
backup = []
for file in files:
data = read_file(file)
backup.append(data)
save_backup(backup)
This code collects all files from a folder, reads each file, and saves the backup data.
Look for loops or repeated steps in the code.
- Primary operation: Reading each file one by one.
- How many times: Once for every file in the folder.
As the number of files grows, the time to read and backup grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files | 10 reads and 1 save |
| 100 files | 100 reads and 1 save |
| 1000 files | 1000 reads and 1 save |
Pattern observation: The time grows directly with the number of files; doubling files doubles the work.
Time Complexity: O(n)
This means the backup time grows in a straight line with the number of files to process.
[X] Wrong: "Backing up a few extra files won't affect the time much."
[OK] Correct: Each file adds more work, so even a few extra files increase the total time noticeably.
Understanding how backup time grows helps you design better systems and explain your choices clearly in real projects.
"What if we only back up files that changed since the last backup? How would the time complexity change?"
Practice
Solution
Step 1: Understand the concept of backup
Backups are copies of important data saved separately to prevent loss.Step 2: Identify the main purpose on Raspberry Pi
On Raspberry Pi, backups protect files from accidental deletion or corruption.Final Answer:
To save copies of important files in case of data loss -> Option CQuick Check:
Backup = Save copies [OK]
- Confusing backup with speeding up the device
- Thinking backup installs software
- Assuming backup changes display settings
Solution
Step 1: Identify commands for backup
rsync copies files, dd creates exact disk images, cp copies files, mv moves files.Step 2: Choose command for full SD card image
dd is used to create a full image backup of the SD card on Raspberry Pi.Final Answer:
dd -> Option BQuick Check:
Full image backup = dd [OK]
- Using rsync for full disk image instead of file sync
- Confusing cp or mv with disk imaging
- Not knowing dd creates exact disk copies
rsync -av --delete /home/pi/data/ /backup/data/
Solution
Step 1: Understand rsync options
-a means archive (copy recursively with attributes), -v means verbose, --delete removes files in destination not in source.Step 2: Analyze source and destination paths
Files are copied from /home/pi/data/ to /backup/data/ with deletion of extra files in destination.Final Answer:
Copies files from /home/pi/data/ to /backup/data/ and deletes files not in source -> Option AQuick Check:
rsync -av --delete = sync with deletion [OK]
- Confusing source and destination order
- Thinking rsync moves files instead of copying
- Ignoring --delete option effect
dd if=/dev/mmcblk0 of=backup.img but get a 'Permission denied' error. What is the likely fix?Solution
Step 1: Understand permission error cause
Accessing /dev/mmcblk0 requires admin rights, normal user lacks permission.Step 2: Fix by running with elevated rights
Using sudo runs the command as admin, allowing access to device files.Final Answer:
Run the command with sudo to get admin rights -> Option AQuick Check:
Permission denied = need sudo [OK]
- Changing file extension does not fix permission
- Switching to rsync won't fix device access
- Restarting doesn't change permissions
Solution
Step 1: Identify automation tools on Raspberry Pi
rsync efficiently copies files; cron schedules tasks automatically.Step 2: Choose best method for daily automated backup
Writing a script with rsync and scheduling with cron automates daily backups reliably.Final Answer:
Write a script using rsync and schedule it with cron -> Option DQuick Check:
Automate backups = rsync + cron [OK]
- Relying on manual copying is not automated
- Using dd hourly wastes space and time
- Installing unrelated software won't backup files
Ready to prove it?
Pass the test and this pattern moves unknown
