Linux restore Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux restore command and learn how to use it to recover files and directories from backup archives. The lab covers the purpose and usage of the restore command, as well as step-by-step examples on restoring a specific file and restoring an entire directory structure from a backup archive.

The restore command is a powerful tool in the Linux ecosystem, allowing users to recover data in case of data loss or system failures. It is typically used in conjunction with backup utilities like the dump command to extract and restore files or directories from backup archives.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/CompressionandArchivingGroup(["`Compression and Archiving`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/CompressionandArchivingGroup -.-> linux/tar("`Archiving`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") subgraph Lab Skills linux/tar -.-> lab-422889{{"`Linux restore Command with Practical Examples`"}} linux/ls -.-> lab-422889{{"`Linux restore Command with Practical Examples`"}} end

Understand the Purpose and Usage of the restore Command

In this step, we will explore the purpose and usage of the restore command in Linux. The restore command is a powerful tool used to recover files or entire directory structures from backup archives.

First, let's understand the purpose of the restore command. The restore command is primarily used to extract and recover data from backup archives, such as those created by the dump command or other backup utilities. It allows you to selectively restore individual files, directories, or the entire file system from a backup archive.

Now, let's explore the basic usage of the restore command. The general syntax for the restore command is:

restore [options] [file or directory]

Some common options used with the restore command include:

  • -i: Interactive mode, which allows you to browse and select files or directories to restore.
  • -r: Restore the entire file system from the backup archive.
  • -t: List the contents of the backup archive without restoring anything.
  • -x: Extract a specific file or directory from the backup archive.

Example usage:

## Restore a specific file from a backup archive
sudo restore -x -f /path/to/backup.archive /path/to/file.txt

## Restore an entire directory structure from a backup archive
sudo restore -r -f /path/to/backup.archive

The restore command is a valuable tool in the Linux ecosystem, as it allows you to recover data from backup archives in case of data loss or system failures.

Restore a Specific File from a Backup Archive

In this step, we will learn how to restore a specific file from a backup archive using the restore command.

First, let's create a sample file to be backed up:

echo "This is a sample file." > ~/project/sample_file.txt

Next, we will create a backup archive using the dump command:

sudo dump -0Lf ~/project/backup.archive ~/project/sample_file.txt

Now, let's say we accidentally deleted the sample_file.txt file. We can use the restore command to recover it from the backup archive:

sudo restore -x -f ~/project/backup.archive ~/project/sample_file.txt

The -x option tells restore to extract a specific file or directory from the backup archive, and the -f option specifies the path to the backup archive file.

Example output:

Verify tape position.
Restoring from tape image.
Extracting sample_file.txt

To verify that the file has been restored, you can check the contents of the ~/project directory:

ls -l ~/project

Example output:

total 4
-rw-r--r-- 1 labex labex 22 May 29 12:34 sample_file.txt

As you can see, the sample_file.txt has been successfully restored from the backup archive.

Restore an Entire Directory Structure from a Backup Archive

In this step, we will learn how to restore an entire directory structure from a backup archive using the restore command.

First, let's create a sample directory structure and some files to be backed up:

mkdir -p ~/project/backup_dir/subdir1 ~/project/backup_dir/subdir2
touch ~/project/backup_dir/file1.txt ~/project/backup_dir/subdir1/file2.txt ~/project/backup_dir/subdir2/file3.txt

Now, let's create a backup archive of the entire backup_dir directory:

sudo dump -0Lf ~/project/backup.archive ~/project/backup_dir

Suppose we accidentally delete the entire backup_dir directory. We can use the restore command to restore the entire directory structure from the backup archive:

sudo restore -r -f ~/project/backup.archive

The -r option tells restore to restore the entire file system from the backup archive.

Example output:

Verify tape position.
Restoring from tape image.
Extracting backup_dir
Extracting backup_dir/file1.txt
Extracting backup_dir/subdir1
Extracting backup_dir/subdir1/file2.txt
Extracting backup_dir/subdir2
Extracting backup_dir/subdir2/file3.txt

To verify that the directory structure has been restored, you can check the contents of the ~/project directory:

ls -R ~/project/backup_dir

Example output:

~/project/backup_dir:
file1.txt  subdir1  subdir2

~/project/backup_dir/subdir1:
file2.txt

~/project/backup_dir/subdir2:
file3.txt

As you can see, the entire backup_dir directory structure, including the subdirectories and files, has been successfully restored from the backup archive.

Summary

In this lab, you learned the purpose and usage of the restore command in Linux, which is used to recover files or entire directory structures from backup archives. You explored the basic syntax and common options of the restore command, such as the -i interactive mode, -r to restore the entire file system, -t to list the contents of the backup archive, and -x to extract a specific file or directory. You then practiced restoring a specific file from a backup archive created using the dump command, demonstrating the restore command's ability to selectively recover data.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like