0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 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.