Automated Daily System Log Backup

LinuxLinuxBeginner
Practice Now

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/log directory
  • 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.

  1. Open a terminal and open the cron editor by running the following command:

    crontab -e
  2. Add 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.log

    This 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.tar in the /home/labex/project/backup/ directory, containing the dpkg.log, bootstrap.log, and fontconfig.log files from the /var/log/ directory.
  3. Save and exit the cron editor.

  4. 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.

  1. Wait until the next scheduled backup time (2 a.m.).
  2. After the backup has been created, check the /home/labex/project/backup/ directory to ensure the backup file is present.
  3. 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.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Linux Tutorials you may like