Backing up files automatically saves your important data without needing to remember to do it yourself.
Backup automation script in Bash Scripting
#!/bin/bash # Define source and backup folder SOURCE="/path/to/source" BACKUP="/path/to/backup" # Create backup folder if it doesn't exist mkdir -p "$BACKUP" # Copy files from source to backup cp -r "$SOURCE"/. "$BACKUP"/ # Print success message echo "Backup completed successfully."
Use mkdir -p to create the backup folder if it does not exist.
The cp -r command copies all files and folders recursively.
# Backup Documents folder to external drive SOURCE="$HOME/Documents" BACKUP="/mnt/usbdrive/backup_documents" mkdir -p "$BACKUP" cp -r "$SOURCE"/. "$BACKUP"/ echo "Documents backup done."
# Backup Pictures folder with date in folder name DATE=$(date +"%Y-%m-%d") SOURCE="$HOME/Pictures" BACKUP="$HOME/Backups/Pictures_$DATE" mkdir -p "$BACKUP" cp -r "$SOURCE"/. "$BACKUP"/ echo "Pictures backup for $DATE done."
This script copies everything from your Documents folder to a backup folder inside your home directory. It creates the backup folder if it doesn't exist and then copies all files and folders inside Documents.
#!/bin/bash # Simple backup script SOURCE="$HOME/Documents" BACKUP="$HOME/Backup/Documents_backup" # Create backup folder if missing mkdir -p "$BACKUP" # Copy all files and folders cp -r "$SOURCE"/. "$BACKUP"/ # Confirm backup echo "Backup completed successfully."
Make sure you have permission to read the source and write to the backup folder.
Running this script regularly can be automated using cron jobs on Linux.
Always test your backup script with sample files before relying on it.
Automate backups to save time and protect your data.
Use mkdir -p and cp -r to create folders and copy files.
Add dates to backup folders to keep multiple versions.
Practice
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 BQuick Check:
Backup automation = automatic file copying [OK]
- Confusing backup with deleting files
- Thinking backup speeds up the system
- Mixing backup with changing permissions
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 AQuick Check:
Safe date format with mkdir = mkdir backup-$(date +%Y-%m-%d) [OK]
- Using slashes in folder names
- Not using command substitution syntax
- Using underscores instead of date command
backup_dir="backup-$(date +%Y-%m-%d)" mkdir -p "$backup_dir" cp -r /home/user/documents "$backup_dir"Assuming today's date is 2024-06-15 and the documents folder exists.
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 CQuick Check:
Command substitution + mkdir + cp = Creates a folder named backup-2024-06-15 and copies documents inside it [OK]
- Not using quotes causing errors with spaces
- Misunderstanding command substitution syntax
- Assuming cp copies files without -r for folders
backup_dir=backup-$(date +%Y-%m-%d) mkdir $backup_dir cp -r /home/user/docs $backup_dir
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 AQuick Check:
Always quote variables with spaces in paths [OK]
- Using mkdir -r which is invalid
- Not using -r with cp for directories
- Incorrect date format syntax
Solution
Step 1: Create dated backup folder and copy logs
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.Final Answer:
backup_dir="backup-$(date +%Y-%m-%d)" mkdir -p "$backup_dir" cp -r /var/log "$backup_dir" find . -maxdepth 1 -name 'backup-*' -mtime +7 -exec rm -rf {} + -> Option DQuick Check:
Safe find with -maxdepth and -mtime +7 = backup_dir="backup-$(date +%Y-%m-%d)" mkdir -p "$backup_dir" cp -r /var/log "$backup_dir" find . -maxdepth 1 -name 'backup-*' -mtime +7 -exec rm -rf {} + [OK]
- Deleting all backups instead of only old ones
- Missing -p in mkdir causing errors
- Using cp without -r for directories
- Not limiting find depth risking unwanted deletions
Ready to prove it?
Pass the test and this pattern moves unknown
