📖 Scenario: You have a Raspberry Pi that stores important files. To keep your data safe, you want to create a simple backup system that copies files to a backup folder. Later, you want to recover files from the backup if needed.
🎯 Goal: Build a Python program that backs up files from one folder to another and can restore files from the backup folder back to the original folder.
📋 What You'll Learn
Create a list of filenames representing files on the Raspberry Pi
Create a backup folder path as a string
Write code to copy files from the original folder to the backup folder
Write code to restore files from the backup folder to the original folder
Print the list of files after backup and after recovery
💡 Why This Matters
🌍 Real World
Backing up important files on a Raspberry Pi helps protect data from accidental loss or hardware failure.
💼 Career
Understanding backup and recovery is essential for system administrators, developers, and anyone managing devices or servers.
Progress0 / 4 steps
1
Create the list of files and original folder path
Create a list called files with these exact filenames: 'config.txt', 'data.csv', 'image.png'. Also create a string variable called original_folder with the value '/home/pi/data'.
Raspberry Pi
Hint
Use square brackets to create the list and assign the folder path as a string.
2
Create the backup folder path
Create a string variable called backup_folder and set it to '/home/pi/backup'.
Raspberry Pi
Hint
Assign the backup folder path as a string to the variable backup_folder.
3
Write code to back up files
Use a for loop with variable file to iterate over files. Inside the loop, create a string source by joining original_folder and file with a slash. Similarly, create a string destination by joining backup_folder and file with a slash. Then, print Backing up {source} to {destination} using an f-string.
Raspberry Pi
Hint
Use string concatenation with '/' to build file paths. Use an f-string to print the message.
4
Write code to recover files and print final output
Use a for loop with variable file to iterate over files. Inside the loop, create a string backup_path by joining backup_folder and file with a slash. Similarly, create a string restore_path by joining original_folder and file with a slash. Then, print Restoring {backup_path} to {restore_path} using an f-string.
Raspberry Pi
Hint
Repeat the loop for recovery. Use f-strings to print the restore messages.
Practice
(1/5)
1. What is the main purpose of creating backups on a Raspberry Pi?
easy
A. To install new software automatically
B. To speed up the Raspberry Pi's processing power
C. To save copies of important files in case of data loss
D. To increase the screen resolution
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 C
Quick Check:
Backup = Save copies [OK]
Hint: Backups protect data by saving copies elsewhere [OK]
Common Mistakes:
Confusing backup with speeding up the device
Thinking backup installs software
Assuming backup changes display settings
2. Which command is commonly used on Raspberry Pi to create a full image backup of the SD card?