MSRIT
BootCamp-2025
Session-3
Lab-Setup-and-Troubleshooting
Setup Cron

Setting Up Automated Backups with Cron

1. Check if the Cron Service is Running

Before scheduling tasks, ensure that the cron service is active on your system. You can check this with:

sudo service cron status

If it’s not running, start it with:

sudo service cron start

2. Create a Backup Directory (Optional)

If you don’t already have a directory to store backups, create one:

sudo mkdir -p /backup
sudo chmod 777 /backup  # Adjust permissions as needed

3. Check the Paths for Files

Make sure you know the path to the directories or files you want to back up. In this case, it’s /home/student*. You can list the directories by running:

ls /home/student*

4. Edit the Crontab File

To schedule tasks, edit the crontab file using:

crontab -e

This will open the cron file in the default text editor (e.g., nano or vim).

5. Write the Cron Job for Backup

To back up student data at regular intervals, add the following cron jobs:

Daily Backups at Midnight

0 0 * * * cp -r /home/student* /backup/student_data_$(date +\%F)

Explanation:

  • 0 0 * * * - Runs at midnight every day.
  • cp -r /home/student* /backup/student_data_$(date +\%F) - Copies student data to /backup/ and appends the current date (YYYY-MM-DD format) to the backup directory name.

Weekly Backups on Sundays at Midnight

0 0 * * 0 cp -r /home/student* /backup/student_data_$(date +\%F)

Explanation:

  • 0 0 * * 0 - Runs every Sunday at midnight.

Hourly Backups

0 * * * * cp -r /home/student* /backup/student_data_$(date +\%F)

Explanation:

  • 0 * * * * - Runs every hour on the hour.

Once added, save and exit the editor.

6. Ensure Cron Jobs Have the Right Permissions

If backing up to /backup/, ensure the cron job runs under a user with the necessary permissions.

7. Check the Cron Logs for Debugging (Optional)

If the backup doesn’t seem to be running, check the cron logs:

grep CRON /var/log/syslog

or

tail -f /var/log/cron.log

8. Verify Your Backup

Check if the backup was created:

ls /backup/

You should see backup directories with the current date (e.g., student_data_2025-02-17).

Optional: Using Rsync for Better Performance

For efficient backups (only copying new or changed files), use rsync instead of cp:

0 0 * * * rsync -av --progress /home/student* /backup/student_data_$(date +\%F)

Summary of Cron Syntax

* * * * * <command_to_run>
│ │ │ │ │
│ │ │ │ └── Day of the week (0 - 7) (Sunday is 0 or 7)
│ │ │ └──── Month (1 - 12)
│ │ └────── Day of the month (1 - 31)
│ └──────── Hour (0 - 23)
└────────── Minute (0 - 59)

Adjust the timing as per your needs.

Let me know if you need further details or adjustments for your cron setup!