Backup Commands in Linux with Examples

Learn via video courses
Topics Covered

Overview

Backing up data is crucial to avoid the loss of important files or data due to hardware failure, virus or malware attacks, or human error. Linux offers several backup tools and commands that can help users to back up their data and restore it in case of any data loss. This article will cover various backup commands in Linux, Linux Admin Backup and Recovery, the 3-2-1 Backup Strategy, and the rsync command for File Level Backups.

Introduction

Backing up data is a very important process that ensures the safety and security of important files and data. Losing data can create hazards for any kind of work that requires storing information in computer systems. There are several reasons for data loss.

Common reasons are hardware failure, system malfunction, or for improper use by the system handler. Data backup comes into the picture just to eradicate all these hazards. There are several backup commands in Linux with some internal and external tools that can help users to create backups of their data and restore it in case of any data loss.

Backup and Restore Commands in Linux

There are several commands in Linux meant for backing up data. Let's learn what they are and how to execute them on a Linux system.

How to Back Up Linux and Unix Files With Tar Command

tar (Tape Archive) is a command-line tool that is used to create and modify tar archives. A tar archive is a set of files that have been bundled together into a single file. This file can then be easily transferred or stored for backup purposes. tar archives can be compressed using various compression algorithms, such as gzip or bzip2.

Before we get started with creating a backup, you need to create a tar archive.

For that, you need to use the tar command followed by the options and arguments. Let's check an example:

Now, Let's break down this command to understand the specifications of the options used.

tar: the command-line tool

-c: create a new archive

-v: verbose mode (display progress as the archive is being created)

`-ft: specify the filename of the archive

testarchive.tar: the name of the archive

/path/to/files: the path to the files you want to include in the archive

When you run this command, tar will create a new archive named testarchive.tar that contains all the files in the /path/to/files directory. If you want to include multiple directories or files, you can separate them with spaces.

Now, let's learn to create a backup using tar.

Determine What to Back Up

Before you create a backup, you need to determine what files and directories you want to include. This will depend on your specific needs and the type of system you are working with. Some common directories to consider including in your backup are:

/etc : configuration files

/var : log files and other system data

/home : user data

/usr/local: custom software and scripts

Creating a Backup Directory

Next, you will need to create a backup directory to store your backup file. This directory should be located on a different device than the one you are backing up. This can be an external hard drive, a network drive, or a cloud storage service.

To create a new directory, use the mkdir command:

Creating the Backup

Now that you have determined what to back up and created a backup directory, you can create the backup using the tar command.

Let's break down this command to understand the specifications of the options used:

tar : the command-line tool

-c : create a new archive

-z : compress the archive using gzip

-v : verbose mode (display progress as the archive is being created)

-f : specify the filename of the archive

testarchive.tar.gz : the name of the archive

/path/to/backup : the path to the files you want to include in the backup.

How to Back Up Linux and Unix Files With Cpio

cpio is a command-line utility in Unix and Linux operating systems that can be used for creating backups of files and directories. It works by taking a list of files and directories and creating an archive of them.

Let's find out how to use cpio for backing up Unix and Linux files:

Open a terminal window on your Unix or Linux system. Navigate to the directory that contains the files and directories you want to back up. Use the find command to create a list of files and directories that you want to include in the backup. For instance, if you want to back up all the files in the current directory and all its subdirectories.

Use the below-mentioned command:

This will generate a list of all the files and directories in the current directory and its subdirectories. Then Pipe the output of the find command to the cpio command.

The -ov options in the cpio command are given to create a new archive file (-o) and use of the verbose mode (-v) to display the progress of the backup. The output of the cpio command is redirected (\>) to a file named `backup. cpi.

Wait for the backup to complete. This may take some time depending on the size of the files and directories being backed up. Once the backup is complete, you can verify the contents of the backup file using the cpio command again mentioned below.

The -it options in the cpio command stands (-t) to display the contents of the archive file and (-i) as the input mode to read from the file backup.cpio. This will display the list of files and directories that were included in the backup.

This is how you can back up your data using the cpio command in Linux. You can restore these files later by using the cpio command with the -i option to extract the files from the backup archive.

Backup Solaris Files with ufsdump and Restore

ufsdump and restore are two command-line tools that are used for backing up and restoring the Solaris filesystem. These tools are essential for system administrators who need to ensure that critical data is safe and easily recoverable in the event of a system failure or data loss.

Let's learn how to take backup using ufsdump and restore commands for the Solaris filesystem:

ufsdump

ufsdump is a utility tool that creates a backup of a UFS filesystem. The backup can be stored on a local disk, tape, or other backup media. The syntax of the ufsdump command is as follows:

The 'filesystem' argument specifies the path to the filesystem that needs to be backed up. Here are some of the common options used with ufsdump.

OptionsSpecification for Usage
-0Level 0 is for a full backup
-1,-2,-3,-4Levels 1 to 4 are used for incremental backups
-fSpecify the filename and path for the backup file
-LSpecifies the size of the media to be used for the backup
-sSpecifies the size of the dump buffer

Now, Let's understand an example of how to use ufsdump to create a full backup of the root filesystem.

This command creates a full backup of the root filesystem and saves it to the file /backup/root.dump.

restore

restore is a utility tool that restores a backup of a UFS filesystem that was created using ufsdump. The syntax of the restore command is as follows:

When you run the 'restore' command, it reads the backup file and restores the filesystem to the specified location. Here are some of the common options used with restore:

OptionsSpecification for Usage
-iInteractive mode. This prompts you for each file or directory to restore
-fSpecifies the filename and path for the backup file
-sSpecifies the size of the restore buffer

Let's understand an example of how to use restore to restore a backup of the root filesystem:

This command reads the backup file /backup/root.dump and restores the root filesystem to its original location.

To be precise, ufsdump and restore are powerful tools for creating backups and restoring the Solaris filesystem. These commands are essential for maintaining data integrity and ensuring that critical data is safe and recoverable in the event of a system failure or data loss.

Backup Files with Dump and Restore Commands

The dump and restore commands are backup utilities available on Unix-based systems. The dump command is used to create backups of files and filesystems. while the restore command is used to restore data from backups.

dump

To create a backup using the dump command, the user needs to specify the filesystem or files to be backed up, along with the backup device or file where the backup will be saved.

Let's check out the same with an example, to back up the '/home' filesystem to a backup file named backup\_home.dmp, the following command needs to be used.

In this command, the -0 option specifies the backup level as level 0, meaning a full backup of the filesystem will be performed. The -u option indicates that only files that have been updated since the last backup will be backed up. The -f option specifies the backup file name and location, and /home is the filesystem that will be backed up.

restore

Restoration of data from a backup using the restore command, the user needs to specify the backup device or file, along with the target location where the data needs to be restored. Let's understand by an example, to restore the /home filesystem from the backup\_home.dmp backup file, the below-mentioned command needs to be used.

In this command, the -r option specifies that data should be restored from the backup file, and the -f option specifies the name and location of the backup file. The restore command will prompt the user to select which files to restore and where to restore them to.

Overall, the dump and restore commands are simple and reliable backup tools and can be useful for creating and restoring backups of essential data on Unix-based systems.

Linux Sysadmin Backup and Recovery

Linux Admin Backup and Recovery involves creating and maintaining backups of critical data on a Linux system. It is necessary to make certain that backups are regularly created and tested for data integrity. In case of data loss, backup restoration can be utilized to re-establish the system as it was before.

3-2-1 Backup Strategy

For data backup and recovery, many people use the 3-2-1 backup strategy. It involves making three copies of the data, storing them in two different kinds of media, and keeping one copy away from the original location. Because it offers a comprehensive approach to data recovery and protection, this tactic is frequently used in Linux environments.

The three copies of data that are produced are referred to as the "3" in the 3-2-1 backup strategy. The original data as well as two additional copies are included in this. The two distinct types of media that the data is stored on, such as tapes and hard drives, or local and cloud-based storage, are referred to as the "2" in the strategy. Last but not least, the one copy that is stored off-site, such as in a secure data center or cloud storage provider, is referred to as the "1" in the strategy.

rsync for File Level Backups

rsync in Linux is a file-level backup tool for synchronizing files and directories between two systems. It is used for backing up data and and restoration of data in cases of data loss.

rsync is effective for backups because it copies only the differences between the source and destination files. Additionally, it has a feature that is used widely to secure the data backup file by compressing and encrypting it.

Local Backup With rsync

To execute a local backup with rsync, the following steps need to be followed:

Installation of rsync

rsync is usually pre-installed in most Linux distributions. However, if it is not installed, it can be installed using the package manager of the distribution. For example, in Ubuntu or Debian, run the below-mentioned command in the terminal.

Determine the Source and Destination Directories

Determine the directories to be backed up and the backup location. For example, to backup the home directory to an external hard drive mounted at /mnt/backup, the source directory would be /home and the destination directory would be /mnt/backup. rsync Command follows the below syntax.

Now to execute the command for local back use the below-given command:

Let's understand the specification of the options used in the command., the options used are:

-a: archive mode, preserves permissions, ownership, and timestamps

-v : verbose output

-z compress file data during the transfer

--delete : delete files from the destination if they are deleted from the source

The source directory, /home, and the destination directory, /mnt/backup, are specified at the end of the command.

Note: It is recommended to run rsync as root or with sudo privileges to ensure proper permissions are preserved during the backup.

Differential Backups With rsync

Differential backups are a type of backup plan that involves copying only the files that have changed since the last full backup. Differential backups can be more efficient than full backups since they require less storage space and take less time to complete. One way to perform differential backups in Linux is to use the rsync command.

Let's find out the steps to perform differential backups with rsync.

Determine the Source and Destination Directories.

The source directory is the directory that contains the files to be backed up, and the destination directory is the directory where the backup files will be stored. For example, if you want to backup the /home directory, you should use /home as the source directory and the destination directory would be the backup directory of your choice.

Perform Initial Backup

The initial backup should be a full backup of the source directory. This can be done using the rsync command with the --archive option. The --archive option is used to preserve permissions, ownership, and timestamps of the files being backed up. For example, to perform the initial backup of the /home directory, you should use the below-mentioned command.

Specifications of the options used in the command are -a, -v, and -z options are used to enable archive mode, verbose output, and compression, respectively. The --delete option is used to delete files from the destination directory that have been deleted from the source directory.

Perform Differential Backup

To perform a differential backup using rsync, you can use the -u option, which stands for the update. This option tells rsync to copy only the files that have changed since the last backup. Let's check out an example, to perform a differential backup of the /home directory.

In this command, option -u is used to perform a differential backup, in addition to the other options used in the initial backup.

Note: The backup directory must be identical to the one used for the initial backup when using rsync to perform differential backups. Without differential backups, rsync will copy all files, eliminating the purpose.

Conclusion

  • Backing up and restoring data in Linux is a crucial process to ensure the safety and security of important files and data.

  • Linux offers several backup tools and commands that can help users to create backups of their data and restore it in case of any data loss.

  • This article covers various backup commands in Linux and also discusses Linux Admin Backup and Recovery, the 3-2-1 Backup Strategy, and rsync for File Level Backups.

  • It is essential to create backups regularly and test them for data integrity to ensure that data can be restored in case of any data loss.