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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/CompressionandArchivingGroup(["`Compression and Archiving`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux/SystemInformationandMonitoringGroup -.-> linux/crontab("`Job Scheduling`") linux/BasicSystemCommandsGroup -.-> linux/logical("`Logic Operations`") linux/CompressionandArchivingGroup -.-> linux/tar("`Archiving`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/date("`Date/Time Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/service("`Service Managing`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills linux/crontab -.-> lab-301479{{"`Automated Daily System Log Backup`"}} linux/logical -.-> lab-301479{{"`Automated Daily System Log Backup`"}} linux/tar -.-> lab-301479{{"`Automated Daily System Log Backup`"}} linux/mkdir -.-> lab-301479{{"`Automated Daily System Log Backup`"}} linux/sudo -.-> lab-301479{{"`Automated Daily System Log Backup`"}} linux/date -.-> lab-301479{{"`Automated Daily System Log Backup`"}} linux/service -.-> lab-301479{{"`Automated Daily System Log Backup`"}} shell/comments -.-> lab-301479{{"`Automated Daily System Log Backup`"}} shell/cmd_substitution -.-> lab-301479{{"`Automated Daily System Log Backup`"}} shell/subshells -.-> lab-301479{{"`Automated Daily System Log Backup`"}} shell/globbing_expansion -.-> lab-301479{{"`Automated Daily System Log Backup`"}} linux/wildcard -.-> lab-301479{{"`Automated Daily System Log Backup`"}} end

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.

Summary

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

Other Linux Tutorials you may like