Prepared by: Anwer Sadath Abdul Muttaliff
This project demonstrates how to create a Bash script that automatically backs up the /var/www/html/index.html
file daily and stores the backup in /tmp/backup_html/
. The script is scheduled using cron to run without manual intervention.
Create a script named backup.sh
:
#!/bin/bash
# Define backup directory
BACKUP_DIR="/tmp/backup_html"
# Create backup directory if it doesn't exist
mkdir -p $BACKUP_DIR
# Create a backup archive of index.html
tar -czf $BACKUP_DIR/backup_html.tar.gz /var/www/html/index.html
$ chmod +x /home/anwer/backup.sh
Open the crontab editor and add the following line to schedule the script to run daily at 11:43 AM:
43 11 * * * /home/anwer/backup.sh
The cron job failed to execute because the script contained the sudo
command, which requires a password. The solution was to remove sudo
from the script and configure passwordless execution for the tar
command in the sudoers file.
Edit the script to remove sudo
:
#!/bin/bash
# Define backup directory
BACKUP_DIR="/tmp/backup_html"
# Create backup directory if it doesn't exist
mkdir -p $BACKUP_DIR
# Create a backup archive of index.html
tar -czf $BACKUP_DIR/backup_html.tar.gz /var/www/html/index.html
The cron job now runs successfully at 11:43 every day, creating a backup file in /tmp/backup_html/backup_html.tar.gz
without requiring a password.