Backup Logs In Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this challenge, you will learn how to back up log files in the Linux platform. Includes how to package log files locally and how to transfer the packaged backup files to the backup server.

Tip: We will complete the entire challenge on a single server.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/CompressionandArchivingGroup(["`Compression and Archiving`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux/SystemInformationandMonitoringGroup -.-> linux/crontab("`Job Scheduling`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/BasicSystemCommandsGroup -.-> linux/xargs("`Command Building`") linux/CompressionandArchivingGroup -.-> linux/tar("`Archiving`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/FileandDirectoryManagementGroup -.-> linux/find("`File Searching`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/RemoteAccessandNetworkingGroup -.-> linux/ifconfig("`Network Configuring`") linux/VersionControlandTextEditorsGroup -.-> linux/vim("`Text Editing`") linux/SystemInformationandMonitoringGroup -.-> linux/date("`Date/Time Displaying`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/str_manipulation("`String Manipulation`") shell/VariableHandlingGroup -.-> shell/param_expansion("`Parameter Expansion`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_ops("`Arithmetic Operations`") 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-1654{{"`Backup Logs In Linux`"}} linux/pipeline -.-> lab-1654{{"`Backup Logs In Linux`"}} linux/xargs -.-> lab-1654{{"`Backup Logs In Linux`"}} linux/tar -.-> lab-1654{{"`Backup Logs In Linux`"}} linux/grep -.-> lab-1654{{"`Backup Logs In Linux`"}} linux/mkdir -.-> lab-1654{{"`Backup Logs In Linux`"}} linux/find -.-> lab-1654{{"`Backup Logs In Linux`"}} linux/ls -.-> lab-1654{{"`Backup Logs In Linux`"}} linux/rm -.-> lab-1654{{"`Backup Logs In Linux`"}} linux/sudo -.-> lab-1654{{"`Backup Logs In Linux`"}} linux/ifconfig -.-> lab-1654{{"`Backup Logs In Linux`"}} linux/vim -.-> lab-1654{{"`Backup Logs In Linux`"}} linux/date -.-> lab-1654{{"`Backup Logs In Linux`"}} shell/shebang -.-> lab-1654{{"`Backup Logs In Linux`"}} shell/comments -.-> lab-1654{{"`Backup Logs In Linux`"}} shell/quoting -.-> lab-1654{{"`Backup Logs In Linux`"}} shell/variables_decl -.-> lab-1654{{"`Backup Logs In Linux`"}} shell/str_manipulation -.-> lab-1654{{"`Backup Logs In Linux`"}} shell/param_expansion -.-> lab-1654{{"`Backup Logs In Linux`"}} shell/cond_expr -.-> lab-1654{{"`Backup Logs In Linux`"}} shell/arith_ops -.-> lab-1654{{"`Backup Logs In Linux`"}} shell/cmd_substitution -.-> lab-1654{{"`Backup Logs In Linux`"}} shell/subshells -.-> lab-1654{{"`Backup Logs In Linux`"}} shell/globbing_expansion -.-> lab-1654{{"`Backup Logs In Linux`"}} linux/wildcard -.-> lab-1654{{"`Backup Logs In Linux`"}} end

Preparation

In this Step, We will do some prep work, mainly creating a local backup directory and a storage directory on the backup server.

Target

You aim to create the /backup and /scripts directories on the server. The /backup directory is used to store backup files, and the /scripts directory is used to store backup scripts.

Result Example

Here's an example of what you should be able to accomplish by the end of this challenge:

  1. Create a new directory called /backup.

    challenge-linux-backup-log-file-1-1
  2. Create a new directory called /scripts.

    challenge-linux-backup-log-file-1-2

Requirements

To complete this challenge, you will need:

  • An account with sudo privileges.
  • Know how to create directories on Linux

Packing System Log

In this step, We will pack the log files into the local backup directory.

We do not back up all log files, only the following logs:

  • /var/log/dmesg - record kernel buffer information.
  • /var/log/faillog - record user login failure information.
  • /var/log/lastlog - record the most recent information of all users.

Target

Your goal is to compress and package the three log files mentioned earlier in the format 192.168.100.100_2022-10-11_linux.tar.gz. You will need to remove the packed files from 7 days ago to save disk space.

Result Example

Here's an example of what you should be able to accomplish by the end of this challenge:

  1. Get the host's IP address and save it to the HOST_IP variable.

    challenge-linux-backup-log-file-2-1
  2. Get the current time and save it to the NOW_TIME variable.

    challenge-linux-backup-log-file-2-2
  3. Pack the above log file into a zip file.

    challenge-linux-backup-log-file-2-3
  4. Delete the backup file from 7 days ago.

    challenge-linux-backup-log-file-2-4

Requirements

To complete this challenge, you will need:

  • Know how to configure environment variables in Linux.
  • Know how to compress files.

Develop Backup Scripts

It would be very cumbersome to use separate commands to back up the logs each time, and the best way to do this is to organize them into scripts.

Target

Your goal is to organize the log backup operations in step 2 into a script with the name backup-linux-log.sh, and test that the script works correctly.

Result Example

Here's an example of what you should be able to accomplish by the end of this challenge:

  1. Create the backup-linux-log.sh file in the /scripts directory and write the actions from step 2.

    challenge-linux-backup-log-file-3-1
  2. Check the script syntax for problems.
    challenge-linux-backup-log-file-3-2

  3. Execute the script to see if creating the backup file is successful.

    challenge-linux-backup-log-file-3-3

Requirements

To complete this challenge, you will need:

  • Know the specific steps to backup log files.
  • Know how to develop Shell scripts and test them.

Add Cron Job

Backing up logs is a routine operation that must be performed weekly or even daily, for which we must add the script as an automatic task.

Target

Your goal is to add the backup script developed in step 3 to a timed task that requires the backup script to be executed every morning at 5am.

Result Example

Here's an example of what you should be able to accomplish by the end of this challenge:

  1. Create a timed task that performs a backup daily at 5 am.

    challenge-linux-backup-log-file

Requirements

To complete this challenge, you will need:

  • Know how to add Cron Job in Linux.

Summary

Congratulations, you have completed all the challenge content. However, this is only the beginning, not the end.

In practice, offsite backups are often used to ensure redundancy, so you can think about what to do.

Other Linux Tutorials you may like