Create and Restore a Backup with tar in Linux

CompTIABeginner
Practice Now

Introduction

In this lab, you will learn the fundamental process of creating and restoring a file system backup in a Linux environment using the tar (tape archive) command-line utility. The primary objective is to gain hands-on experience with essential system administration tasks, including managing user permissions and handling file archives. You will use the sudo command to execute operations with elevated privileges when necessary to access and back up system-critical directories, specifically the /home directory, which contains user data.

The lab will guide you through a practical workflow using the default labex user account with sudo for administrative tasks. You will create a compressed archive of the entire /home directory and verify its successful creation. To simulate a restoration scenario, you will navigate to a temporary directory, /tmp, and extract the contents of your backup archive. Finally, you will verify that the files and directory structure have been correctly restored to the new location, confirming the integrity of your backup and restoration process.

Understand the Current Environment

In this step, you will learn about your current environment and how to use sudo for administrative tasks. The labex user has the ability to run commands with elevated privileges using sudo (superuser do), which allows you to execute administrative tasks without switching to the root user account.

Using sudo is a security best practice because it provides temporary elevated privileges for specific commands while maintaining your user context. This approach is safer than switching to the root user entirely, as it reduces the risk of accidentally executing dangerous commands.

First, let's verify your current user identity and location by running the whoami and pwd (print working directory) commands.

Check who you are:

whoami

The output should be labex:

labex

Next, check your current directory:

pwd

The output should show you are in the labex user's project directory:

/home/labex/project

You can test your sudo privileges by running a simple command that requires administrative access. Let's check the /root directory, which normally requires elevated permissions:

sudo ls /root

This command will execute successfully, demonstrating that you have the necessary sudo privileges to perform administrative tasks. The sudo command will temporarily grant you the permissions needed to access restricted areas of the filesystem.

Now that you understand your environment and sudo capabilities, you are ready to create a backup in the next step.

Create a Backup Archive of the /home Directory

In this step, you will create a backup of the /home directory using the tar command with sudo. The tar (tape archiver) utility is a standard tool on Linux for creating, viewing, and extracting archive files. An archive is a single file that contains multiple other files and directories, making it easy to store and transport them.

You are currently logged in as the labex user, and your present working directory is /home/labex/project. You will create the backup file in your home directory, which is accessible to your user account.

The command you will use is sudo tar -cvf ~/backup.tar /home. Let's break down the options:

  • sudo: This grants the necessary privileges to read all files in the /home directory, including those owned by other users.
  • -c (create): This option tells tar to create a new archive.
  • -v (verbose): This makes tar list each file as it is being added to the archive. This is useful for seeing the progress of the operation.
  • -f (file): This option allows you to specify the filename of the archive. It must be immediately followed by the archive name. In our case, it's ~/backup.tar. The ~ is a shortcut for your home directory (/home/labex).
  • /home: This is the source directory that you want to back up.

Now, run the command in your terminal to create the backup archive:

sudo tar -cvf ~/backup.tar /home

Because you used the -v option, you will see a long list of files scrolling on your screen as they are added to the backup.tar archive. The output will look similar to this, listing all the files within the /home directory:

tar: Removing leading `/' from member names
/home/
/home/labex/
/home/labex/.bashrc
/home/labex/.profile
/home/labex/.zshrc
/home/labex/project/
/home/labex/.zsh_history
...

The message tar: Removing leading '/' from member names is normal. It means tar is storing the paths inside the archive relative to the specified directory, which is standard practice for portability.

In the next step, you will verify that the backup.tar file has been successfully created.

Verify the Creation of the backup.tar File

In this step, you will verify that the backup.tar archive was created successfully in the previous step. After performing a critical operation like a backup, it's always a good practice to confirm that the expected output file exists.

Since you created the backup file in your home directory (~/backup.tar), you need to check your home directory. The simplest way to check for the file is by using the ls (list) command with the full path or by navigating to your home directory.

Execute the ls command to check your home directory:

ls ~

The command will list all files and directories in your home directory. You should see the backup.tar file in the output:

backup.tar

Alternatively, you can check for the file directly using its full path:

ls -l ~/backup.tar

The output will look something like this:

-rw-r--r-- 1 root root 10240 <Date> <Time> /home/labex/backup.tar

This detailed listing gives you more confidence, showing that the file exists and has a non-zero size, which is what you'd expect from a backup archive. Note that the file is owned by root because you created it using sudo, but it's stored in your accessible home directory.

Now that you have successfully created and verified the backup, the next step is to simulate a restoration scenario.

Change to the /tmp Directory for Restoration

In this step, you will change your current directory to /tmp. To practice restoring a backup, it's best to extract the files to a neutral location first. This prevents accidentally overwriting existing files and allows you to inspect the restored data before moving it to its final destination. The /tmp directory is a standard location on Linux systems for temporary files and is perfect for this purpose.

To navigate between directories in the Linux file system, you use the cd (change directory) command. You are currently in the /root directory.

To change to the /tmp directory, execute the following command:

cd /tmp

After running the command, your terminal prompt might not look different, but your location in the filesystem has changed. You can confirm your new location by using the pwd (print working directory) command, which displays the full path of your current directory.

pwd

The output should confirm that you are now in /tmp:

/tmp

Now that you are in the /tmp directory, you are ready to extract the contents of the backup.tar archive you created earlier.

Extract the Backup Archive to the Current Directory

In this step, you will extract the contents of the backup.tar file into your current directory, /tmp. This process is often called "restoring" from a backup. You will use the tar command again, but with different options to perform the extraction.

You are currently in the /tmp directory. The backup file, backup.tar, is located in your home directory (/home/labex). Therefore, you must provide the full path to the archive file when you run the command.

The command to extract the archive is tar -xvf ~/backup.tar. Let's look at the options:

  • -x (extract): This option tells tar to extract files from an archive. This is the key difference from the creation command.
  • -v (verbose): Just like before, this lists each file as it is extracted, showing you the progress.
  • -f (file): This specifies the name of the archive file to process. You use ~/backup.tar to reference the backup file in your home directory.

Note that you don't need sudo for the extraction because you're extracting to /tmp, which is writable by regular users, and you're only reading the backup file (not modifying system files).

Now, execute the command in your terminal to extract the files:

tar -xvf ~/backup.tar

You will see a list of files and directories being created in your current directory (/tmp). The output will be the list of files contained within the archive, which should look familiar from when you created it:

home/
home/labex/
home/labex/.bashrc
home/labex/.profile
home/labex/.zshrc
home/labex/project/
home/labex/.zsh_history
...

This output indicates that tar has created a home directory inside /tmp and has restored all the backed-up files and directories into it. In the final step, you will verify this.

Verify the Files are Restored in /tmp

In this final step, you will confirm that the files from the backup archive were correctly extracted into the /tmp directory. This is a crucial part of any restoration process to ensure data integrity.

You are currently in the /tmp directory. To see the newly created home directory, use the ls command:

ls

You should see a home directory listed among any other temporary files that might be present:

home
...

This confirms that the top-level directory from the archive was created. To be more thorough, let's look inside this new home directory to see if it contains the expected user data.

ls home

The output should show the labex user's home directory, which was the content of the original /home directory you backed up:

labex

This confirms that the backup and restoration process was successful. You have created a backup of /home using sudo privileges, stored it in your home directory (/home/labex), and then successfully restored it to a temporary location at /tmp/home.

You can verify that the restored files match the original by comparing a few files. For example, you can check if your .bashrc file was properly restored:

ls home/labex/.bashrc

The output should confirm the file exists:

home/labex/.bashrc

The backup and restoration process is now complete. You have successfully demonstrated how to use tar with sudo privileges to create a system backup while maintaining your user context, which is the recommended approach for system administration tasks.

Summary

In this lab, you learned how to perform a fundamental system administration task: creating and restoring a directory backup using the tar command in Linux with sudo privileges. The process began with understanding your environment as the labex user and using sudo to execute commands with elevated privileges when necessary. This approach is safer and more secure than switching to the root user entirely, as it provides temporary elevated permissions for specific tasks while maintaining your user context.

You used the tar utility with sudo to create a single archive file, backup.tar, containing all the contents of the /home directory, storing it in your accessible home directory. This demonstrated how to handle system-wide backups while working within user boundaries and avoiding potential conflicts with system functions.

For the restoration part of the lab, you learned the best practice of first changing to a safe, temporary location like the /tmp directory to avoid overwriting live data. From there, you used the tar command to extract the contents from the backup.tar archive into your current directory without requiring elevated privileges, since you were only reading the backup file and writing to a user-accessible location. The final step involved verifying that the files and directories from the backup were correctly restored in /tmp, confirming the integrity of the entire backup and restore cycle.