0
0
Bash Scriptingscripting~30 mins

Backup automation script in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Backup automation script
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use echo "Copied $(basename \"$file\")" to print the file name.