Introduction
In this project, you will learn how to set up a cron job to automatically back up the system's daily log files. As a system administrator, you may frequently need to perform log backups to ensure the availability and recoverability of important system data.
🎯 Tasks
In this project, you will learn:
- How to set up a cron job for the "labex" user
- How to create a tar archive of specific log files from the
/var/logdirectory - How to save the backup file in the
/home/labex/project/backup/directory with a date-based filename
🏆 Achievements
After completing this project, you will be able to:
- Automate the daily backup of system log files using a cron job
- Ensure the consistent and reliable backup of critical system logs
- Verify the successful creation of the log backup files
Set Up the Cron Job
In this step, you will learn how to set up a cron job for the user "labex" to run the log backup script.
Open a terminal and open the cron editor by running the following command:
crontab -eAdd the following line to the cron editor:
0 2 * * * mkdir -p /home/labex/project/backup/ && tar -zcf /home/labex/project/backup/$(date +\%Y-\%m-\%d).tar /var/log/dpkg.log /var/log/bootstrap.log /var/log/fontconfig.logThis cron job will run at 2 a.m. every day and perform the following actions:
- Create the
/home/labex/project/backup/directory if it doesn't already exist. - Create a tar archive named
YYYY-MM-DD.tarin the/home/labex/project/backup/directory, containing thedpkg.log,bootstrap.log, andfontconfig.logfiles from the/var/log/directory.
- Create the
Save and exit the cron editor.
Restart the cron service to ensure the new cron job is active:
sudo service cron restart
Verify the Backup
In this step, you will learn how to verify that the log backup is being created as expected.
- Wait until the next scheduled backup time (2 a.m.).
- After the backup has been created, check the
/home/labex/project/backup/directory to ensure the backup file is present. - If the backup file is present and the contents are as expected, the log backup process is working correctly.
Congratulations! You have successfully set up a cron job to automatically back up the system's daily log files.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



