Task: Write an automation script
-
Create a script (
backup.sh) that:- Takes a directory as an argument
- Creates a timestamped backup in
/tmp
-
Example script:
Click to view Hint !!
#!/bin/bash backup_dir=$1 timestamp=$(date +%Y%m%d_%H%M%S) backup_file="/tmp/backup_$timestamp.tar.gz" if [[ ! -d "$backup_dir" ]]; then printf "Error: Directory does not exist\n" >&2 exit 1 fi tar -czf "$backup_file" "$backup_dir" printf "Backup created at %s\n" "$backup_file"
-
Make it executable and run it:
Click to view Hint !!
chmod +x backup.sh ./backup.sh ~/Documents -
Bonus: Schedule it in a cron job to run every night at 2 AM.
Tip
Use Hint section whenever stuck, google the command and go through its usage and functionality.