Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Tutorials»Backing Up Your Linux System with Rsync: A Step-by-Step Guide

    Backing Up Your Linux System with Rsync: A Step-by-Step Guide

    By RahulJanuary 17, 20233 Mins Read

    For many computer users, the most stressful part of working with a Linux system is having to back up their data. The good news is that there is a simple solution to this problem: set up an automatic rsync backup script that will automatically keep your data safe. In this article, we will go over the tools and steps that you need to take to set up an automated backup system on a Linux system with rsync. You will learn how to use rsync to automatically create backups of files, how to keep these backups up-to-date, and how to restore them in the event of data loss or corruption.

    Advertisement

    If you regularly perform backups on your Linux system, chances are you already know about rsync, a command-line utility that can be used to back up and synchronize files and directories. However, if you’re new to rsync, it might come as a surprise that this simple command is capable of backing up your entire Linux system. In this guide, we’ll show you how to use rsync to back up your Linux system using different strategies.

    Steps to Backup Your Linux System

    1. Prepare a Backup Device
    2. To make a full backup of the system, you need a device that has much space to keep all files. A backup device can be a locally attached drive, network device, or cloud storage like Amazon S3, Azure Spaces, etc.

      Create a directory to store the backup on the backup device. Assuming you have attached a separate disk in your local machine mounted at /mnt directory.

      mkdir /mmnt/full-backup 
      

    3. Install Rsync Utility
    4. Rsync is a command line utility that helps to synchronize content between two directories. They either exist on the local system or one can be the remote. You can quickly install on using the default package manager on most modern Linux distributions. To install Rsync on Debian-based systems, type:

      sudo apt install rsync 
      

    5. Backup Your System
    6. You can run the command directly to make a backup of the complete system. For example, to create a backup of the system to the “/mnt/full-backup” directory, run the following command.

      sudo rsync -aAXv / --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /mnt/full-backup 
      

      The above command will backup the entire root (/) directory, excluding /dev, /proc, /sys, /tmp, /run, /mnt, /media, and /lost+found directories, and save the data in /mnt/full-backup folder. Here:

      The `-aAXv` options are used so that the files are transferred in “archive” mode, which ensures that symbolic links, devices, permissions, ownerships, modification times, ACLs, and extended attributes are preserved.

    7. Automate the Backup
    8. It’s good practice to schedule automatic backups. You can simply add the above command in crontab, or write them in a shell script and then schedule the script.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      #!/usr/bin/evn bash
       
      BACKUP_PATH="/mnt/full-backup"
      EXCLUDE_DIR='{"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"}'
      SOURCE_DIR="/"
       
      sudo rsync -aAXv ${SOURCE_DIR} --exclude=${EXCLUDE_DIR} ${BACKUP_PATH}
       
      if [ $? -eq 0 ]; then
          echo "Backup completed successfully"
      else
          echo "Some error occurred during backup"
      fi

      You can schedule the script to run automatically using a tool such as cron. This will allow you to create regular backups of your system without having to manually run the script.

      To schedule the script, edit the crontab:

      crontab -e 
      

      Append the following entry. Make sure to set the correct script name and path. The below schedule will run the script at 02 AM every day.

      1
      0  2  *  *  *  bash backup.sh >> backup.log

      Save and close the editor.

      Conclusion

      Now that you know how to use rsync, you may want to take advantage of its advanced features. For instance, you can use rsync to efficiently copy files from one directory to another. You can also generate incremental backups that allow you to quickly recover files at any time. If you want even more control over your backup process, you can even schedule backups.

    backup
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    10 Most Popular Linux Shells

    10 Most Popular Open Source Linux Shells

    How to Create Bash Aliases with Arguments

    How to Create Bash Aliases with Parameters

    5 Practical Examples to Check If a Port is Open

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Backing Up Your Linux System with Rsync: A Step-by-Step Guide
    • Sort Command in Linux with Practical Examples
    • How to Install and Use Flask in Fedora 37/36
    • Sed Command in Linux with 15 Practical Examples
    • How to Print a List without Brackets in Python
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.