Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Backup automation script in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Backup Automation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this backup script snippet?
Consider this bash script snippet that creates a backup of a directory and logs the result. What will be printed to the console when the script runs successfully?
Bash Scripting
backup_dir="/home/user/documents"
backup_dest="/home/user/backup"

mkdir -p "$backup_dest"
tar -czf "$backup_dest/documents_backup.tar.gz" "$backup_dir" && echo "Backup successful" || echo "Backup failed"
Atar: command not found
BBackup failed
CBackup successful
DPermission denied
Attempts:
2 left
💡 Hint
The tar command creates a compressed archive. If it succeeds, the echo after && runs.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this backup script
Which option shows the correct way to write this line to check if the backup directory exists and create it if not?
Bash Scripting
[ ! -d "$backup_dest" ] mkdir -p "$backup_dest"
Aif [ ! -d "$backup_dest" ]; then mkdir -p "$backup_dest"; fi
B[ ! -d "$backup_dest" ] && mkdir -p "$backup_dest"
Cif [ ! -d "$backup_dest" ]; then mkdir -p "$backup_dest" fi
D[ ! -d "$backup_dest" ]; mkdir -p "$backup_dest"
Attempts:
2 left
💡 Hint
Remember to use 'then' and end the if statement with 'fi'.
🔧 Debug
advanced
2:00remaining
Why does this backup script fail to create the backup file?
This script is intended to create a compressed backup of /var/log but fails silently without creating the backup file. What is the cause?
Bash Scripting
backup_dir="/var/log"
backup_dest="/tmp/backup"

mkdir -p "$backup_dest"
tar -czf "$backup_dest/var_log_backup.tar.gz" "$backup_dir" > /dev/null 2>&1

if [ -f "$backup_dest/var_log_backup.tar.gz" ]; then echo "Backup created"; else echo "Backup missing"; fi
AThe script lacks sudo privileges to read /var/log, so tar creates no archive.
BThe redirection '> /dev/null 2>&1' causes the tar command to fail.
CThe backup directory path is incorrect and mkdir fails.
DThe if condition syntax is wrong and always prints 'Backup missing'.
Attempts:
2 left
💡 Hint
Check permissions for reading system log files.
🚀 Application
advanced
2:00remaining
Which script snippet correctly deletes backups older than 7 days?
You want to automate deleting backup files older than 7 days in /home/user/backup. Which snippet will do this safely?
Afind /home/user/backup -mtime +7 -delete
Bfind /home/user/backup -type f -name '*.tar.gz' -mtime +7 -exec rm {} \;
Crm /home/user/backup/*.tar.gz -mtime +7
Drm -rf /home/user/backup/*.tar.gz --older-than 7d
Attempts:
2 left
💡 Hint
Use find with -mtime and -exec to remove files older than a certain number of days.
🧠 Conceptual
expert
2:00remaining
What is the main benefit of using rsync over tar for backups in scripts?
When automating backups, why might you choose rsync instead of tar?
Arsync creates a single archive file like tar but faster.
Brsync compresses files better than tar, creating smaller archives.
Crsync automatically encrypts backups without extra configuration.
Drsync can efficiently copy only changed files, reducing backup time and storage.
Attempts:
2 left
💡 Hint
Think about incremental backups and copying only differences.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To automatically copy important files to a safe location regularly -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    mkdir backup-$(date +%Y-%m-%d) -> Option A
  4. 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?
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.
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

  1. 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.
  2. 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.
  3. Final Answer:

    Creates a folder named backup-2024-06-15 and copies documents inside it -> Option C
  4. 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:
backup_dir=backup-$(date +%Y-%m-%d)
mkdir $backup_dir
cp -r /home/user/docs $backup_dir
medium
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

  1. 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.
  2. 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.
  3. Final Answer:

    Missing quotes around $backup_dir may cause errors if folder name has spaces -> Option A
  4. 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?
hard
A. backup_dir="backup-$(date +%Y-%m-%d)" mkdir -p "$backup_dir" cp -r /var/log "$backup_dir" find . -name 'backup-*' -delete
B. backup_dir=backup-$(date +%Y-%m-%d) mkdir $backup_dir cp /var/log $backup_dir rm -rf backup-*
C. backup_dir="backup-$(date +%Y-%m-%d)" mkdir -p "$backup_dir" cp -r /var/log "$backup_dir" rm -rf backup-*
D. 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 {} +

Solution

  1. 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.
  2. 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.
  3. 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 D
  4. Quick 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]
Hint: Use find with -mtime +7 and -maxdepth 1 to delete old backups [OK]
Common Mistakes:
  • 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard