Backup and Restore in Linux

Written by: Emmanuel Oyibo   |   Last updated: May 16, 2024

Data loss can strike at any time. Whether it’s accidental file deletion, a failing hard drive, or a major system outage, the consequences can be severe.

That’s why having a well-thought-out backup strategy is crucial in Linux environments. It’s your insurance policy against lost work, corrupted system files, or even complete disasters.

This article will guide you through essential Linux backup and restore tools.

Basic File-Level Backups

File-level backups give you granular control, allowing you protect specific files and directories. They’re ideal for critical user data, project files, system configurations, and any data you don’t want to lose. Linux offers several powerful tools to achieve this.

The Versatile tar Command

tar is a cornerstone of Linux backup operations. It’s traditionally used for archiving data onto tapes (hence the name). However, modern use focuses on creating compressed file collections.

Now, let’s take a look at the basic syntax:

$ tar [options] <archive_name> <files_to_backup>

Some of the most common options you can use with tar include:

  • -c: creates a new archive
  • -v: shows the list of files being processed (verbose mode)
  • -z: compresses the archive using gzip for space efficiency
  • -f: specifies the archive filename (always the last argument)

For example, let’s create a compressed backup for your home directory:

$ tar -cvzf home_backup.tar.gz /home/username

Also, to extract the files from the archive, we can use tar as follows:

$ tar -xvzf home_backup.tar.gz -C /path/to/restore

Where:

  • -x: extracts files from the the archive
  • -C: changes the extraction directory (to avoid overwriting existing files)

For a quick reference to common tar flags and patterns, check out our tar cheat sheet.

The Efficient rsync Command

rsync is renowned for its ability to synchronize files and directories with incredible efficiency. This makes it particularly well-suited for incremental backups, where you want to capture changes since your last backup without copying everything repeatedly.

Here’s its basic syntax:

$ rsync [options] <source> <destination>

Now, let’s see some of the common options used with rsync:

  • -a: archive mode, which is a combination of options designed to preserve file ownership, permissions, timestamps, etc.
  • -v: verbose mode for increased output
  • -z: enables compression during transfer, saving bandwidth

For example, let’s say we want to back up your website to a remote server:

$ rsync -avz /var/www/html user@remote_server:/backups/website

In addition, rsync can be used to restore files after incidental deletion. By rerunning your rsync command, it’ll efficiently synchronize the changes from your backup to your live system, recovering the missing files.

The cpio Command

cpio offers a versatile way to work with archives in various formats. It’s often used in conjunction with commands like find to provide fine-grained control over what’s included in an archive.

The key modes of this command are:

  • Extract Mode (-i):
    • Extracts files from an archive
    • Basic syntax: cpio -i [options] [pattern ...] < archive
  • Create Mode (-o):
    • Creates a new archive
    • Basic syntax: cpio -o [options] < name-list > archive
  • Pasds-though Mode (-p):
    • Copies files to destination directory
    • Basic syntax: cpio -p [options] dest-dir < name-list

Furthermore, some of the common options used with cpio include:

  • -d: creates missing directories as needed when extracting
  • -v: verbose mode that provides more detailed output

For example, we can create a cpio archive as follows:

$ find /home/username -depth | cpio -ov > home_backup.cpio

Also, you can restore a cpio archive as follows:

$ cpio -idv < home_backup.cpio

Note that cpio can work with various archive formats (e.g., tar, pax, and others). This flexibility can be beneficial in specific backup and restore scenarios.

Block-Level Backups

Block-level backups provide a way to make a complete copy of a disk partition or an entire storage volume. Unlike file-level backups, they operate by copying the raw data blocks that make up your filesystem.

This approach has unique benefits and considerations.

The dd Command

dd is a fundamental, yet extremely potent tool for copying raw data between devices. It’s essential to use dd with great care as a single wrong command can cause unintended data loss.

Here’s the basic syntax:

$ dd if=/dev/[input_device] of=/path/to/output_image.img

Where:

  • if: is the input file. Specifies the disk partition or the entire file you want to back up
  • of: is the output file. It’s where your backup image will be stored

For example, let’s create an image backup of a partition named, /dev/sda1:

$ dd if=/dev/sda1 of=partition1_backup.img

However, it’s often recommended to unmount the filesystem you’re backing up, or to boot from a live CD/USB environment when using ddfor block-level backups. This helps ensure data consistency.

The partclone Utility

partclone provides a more specialized and efficient approach to block-level backups than the raw power of the dd command. It’s designed to intelligently understand the specific filesystems (e.g., NTFS, FAT32) used on your partitions.

This comes with several advantages:

  • Smaller backups: by only copying data blocks that are in use, partclone can often produce smaller backup images, saving storage space.
  • Restore options: partclone offers flexibility for restoring individual files or directories directly from a partition image, which can speed up recovery.
  • Supported filesystems: partclone provides utilities for various filesystems such as partclone.ext4, partclone.ntfs, partclone.fat32, and more.

For example, let’s backup an ext4-formatted partition, /dev/sda1:

$ partclone.ext4 -c -s /dev/sda1 -o partition1_backup.img

Where:

  • -c: create mode
  • -s: specifies the source file (your partition)
  • -o: specifies the output file (your backup image)

Also, it’s orecommended to unmount the filesystem you’re backing up partclone or perform the backup from a live environment (CD/USB) to ensure data consistency.

About The Author

Emmanuel Oyibo

Emmanuel Oyibo

Emmanuel is a Technical Writer and DevOps/Software Engineer with over 4 years of experience in streamlining technical documentation and automating workflows. He specializes in writing about Linux, Python, and DevOps concepts, utilizing his expertise to create clear guides and documentation. Emmanuel holds a B.Sc in Biochemistry and is passionate about the power of technology for problem-solving.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment