📖 Scenario: You work as a system assistant and want to automate backing up important files from a folder to a backup location. This helps keep your data safe without manual copying every time.
🎯 Goal: Create a bash script that copies files from a source directory to a backup directory, only backing up files larger than a certain size.
📋 What You'll Learn
Create a variable for the source directory with the exact path /home/user/documents
Create a variable for the backup directory with the exact path /home/user/backup
Create a variable called min_size with the value 1000 (bytes)
Use a for loop to check each file in the source directory
Copy only files larger than min_size to the backup directory
Print the name of each file copied
💡 Why This Matters
🌍 Real World
Automating backups saves time and prevents data loss by regularly copying important files without manual effort.
💼 Career
System administrators and IT support staff often write scripts like this to maintain data safety and automate routine tasks.
Progress0 / 4 steps
1
Set up source and backup directories
Create two variables called source_dir and backup_dir. Set source_dir to /home/user/documents and backup_dir to /home/user/backup.
Bash Scripting
Hint
Use = to assign the paths as strings with quotes.
2
Set minimum file size for backup
Create a variable called min_size and set it to 1000 (this means 1000 bytes).
Bash Scripting
Hint
Just assign the number 1000 without quotes.
3
Write loop to check files and copy large ones
Write a for loop using file as the variable to go through all files in $source_dir. Inside the loop, use if to check if the file size is greater than $min_size bytes. If yes, copy the file to $backup_dir.
Bash Scripting
Hint
Use stat -c%s "$file" to get file size in bytes.
4
Print copied files
Add a echo statement inside the if block to print Copied followed by the file name only (not full path). Use basename to get the file name.
Bash Scripting
Hint
Use echo "Copied $(basename \"$file\")" to print the file name.
Practice
(1/5)
1. What is the main purpose of a backup automation script in bash?
easy
A. To delete old files from the system
B. To automatically copy important files to a safe location regularly
C. To speed up the computer by closing programs
D. To change file permissions for security
Solution
Step 1: Understand the goal of backup automation
A backup automation script is designed to save copies of important files automatically to prevent data loss.
Step 2: Identify the correct purpose among options
Only To automatically copy important files to a safe location regularly describes copying files regularly to a safe place, which matches backup automation.
Final Answer:
To automatically copy important files to a safe location regularly -> Option B
Quick Check:
Backup automation = automatic file copying [OK]
Hint: Backup scripts copy files automatically to protect data [OK]
Common Mistakes:
Confusing backup with deleting files
Thinking backup speeds up the system
Mixing backup with changing permissions
2. Which of the following bash commands correctly creates a backup directory with today's date?
easy
A. mkdir backup-$(date +%Y-%m-%d)
B. mkdir backup_date
C. mkdir backup $(date +%d-%m-%Y)
D. mkdir backup-`date +%Y/%m/%d`
Solution
Step 1: Understand date command format for folder naming
The format +%Y-%m-%d gives a date like 2024-06-15, which is safe for folder names.
Step 2: Check each mkdir command for correctness
mkdir backup-`date +%Y/%m/%d` uses backticks (valid) but produces slashes (/), invalid for folder names. mkdir backup_date uses a literal string without date. mkdir backup $(date +%d-%m-%Y) has a space causing two arguments to mkdir ('backup' and date string). mkdir backup-$(date +%Y-%m-%d) uses correct $( ) substitution and safe hyphen-separated format.
Final Answer:
mkdir backup-$(date +%Y-%m-%d) -> Option A
Quick Check:
Safe date format with mkdir = mkdir backup-$(date +%Y-%m-%d) [OK]
Hint: Use $(date +%Y-%m-%d) for safe folder names [OK]
Common Mistakes:
Using slashes in folder names
Not using command substitution syntax
Using underscores instead of date command
3. What will be the output of this script snippet?
Assuming today's date is 2024-06-15 and the documents folder exists.
medium
A. Throws an error because of missing quotes
B. Creates a folder named backup-$(date +%Y-%m-%d) and copies documents inside it
C. Creates a folder named backup-2024-06-15 and copies documents inside it
D. Copies documents to the current directory without creating a folder
Solution
Step 1: Evaluate variable assignment and command substitution
The variable backup_dir will be set to backup-2024-06-15 because $(date +%Y-%m-%d) runs and inserts the date.
Step 2: Understand mkdir and cp commands with quotes
mkdir -p creates the directory if it doesn't exist. cp -r copies the documents folder recursively into the backup directory. Quotes ensure spaces are handled safely.
Final Answer:
Creates a folder named backup-2024-06-15 and copies documents inside it -> Option C
Quick Check:
Command substitution + mkdir + cp = Creates a folder named backup-2024-06-15 and copies documents inside it [OK]
Hint: Quotes and $(date) create dated backup folders safely [OK]
Common Mistakes:
Not using quotes causing errors with spaces
Misunderstanding command substitution syntax
Assuming cp copies files without -r for folders
4. Identify the error in this backup script snippet:
A. Missing quotes around $backup_dir may cause errors if folder name has spaces
B. mkdir command should be mkdir -r instead of mkdir
C. cp command should not use -r for copying directories
D. Date format in variable assignment is incorrect
Solution
Step 1: Check mkdir command usage
mkdir without -p is okay if folder doesn't exist, but missing quotes around $backup_dir can cause issues if the folder name contains spaces.
Step 2: Check cp command and date format
cp -r is correct for copying directories. Date format is valid. The main issue is missing quotes around variable usage.
Final Answer:
Missing quotes around $backup_dir may cause errors if folder name has spaces -> Option A
Quick Check:
Always quote variables with spaces in paths [OK]
Hint: Always quote variables when used as paths [OK]
Common Mistakes:
Using mkdir -r which is invalid
Not using -r with cp for directories
Incorrect date format syntax
5. You want to automate backups of the /var/log directory daily, keeping backups for 7 days only. Which script snippet correctly creates a dated backup folder and deletes backups older than 7 days?
Snippets using backup_dir="backup-$(date +%Y-%m-%d)" (double quotes), mkdir -p "$backup_dir", cp -r /var/log "$backup_dir" correctly create dated folder and copy recursively. The other lacks quotes, -p, -r so may fail.
Step 2: Remove backups older than 7 days safely
rm -rf backup-* and find . -name 'backup-*' -delete delete all backups without age check. Only find . -maxdepth 1 -name 'backup-*' -mtime +7 -exec rm -rf {} + limits to top-level, selects >7 days old, deletes safely.