Linux Portfolio Project - Backup Files in a Specified Directory into a Tarball (Bash Shell Scripting)

Linux Portfolio Project - Backup Files in a Specified Directory into a Tarball (Bash Shell Scripting)

No alt text provided for this image
Complete shell script for file archiver. The script is titled "backup.sh".

Backups are an important measure to mitigate data loss and data corruption, which can be catastrophic for any Linux system. In the event of a disaster where critical data is lost or corrupted, backups can be used to restore data from a certain point in time. The availability of backups can be essential in recovering lost data, and speed up the timeline for resuming normal operations.

On Linux, the "tar" command is used to archive various files into a single file. Folders are considered to be files which hold files, so they can also be archived through the "tar" program. This shell script is intended to backup files in your present working directory (or a directory you specify) using the "tar" command.

This script creates a tarball through the "tar" program. You will be prompted to enter the full path of the directory you want to archive. Through the "cd" command, the script will change its present working directory to the directory that was entered into the user prompt. At the end of the script, the script will archive and compress the files within the specified directory.

Shell Script Components

No alt text provided for this image
As good practice, we include how shell scripts are to be entered into the Bash shell. Depending on where you are located on your Linux system, two options are provided to execute the script.

Our first prompt will simply ask the user to enter the full, absolute path of the directory we want to archive as a backup. Any relative, path name the user enters as a directory will fail to create an archive. The script will store the directory's full path into the "fullpathlocation" variable:

No alt text provided for this image
Requesting a directory from the user as a absolute path name.

The script will then use the "cd" command to change the current directory to the directory stored in the $fullpathlocation variable. The script will now interpret the $fullpathlocation value as our present working directory. This will take us the second half of the script:

No alt text provided for this image
Code structure for the file archiver in the "backup.sh" script.

This is where the tarball is created. The script will archive and compress all the files located in the directory you specified into a tarball. Tarballs are archive files processed through the "tar" program, which are then compressed into smaller files using a compression program.

Archiving data is not the same as compressing data; archiving collects data and compression condenses data. If data is not compressed, depending on the files we archive, we may end up with large, archive files that take up a lot space and become impractical to store on our system. Compression addresses this problem.

While "tar" handles archiving, there are a few programs like "gzip", "bzip2" and "xz" that handle the compression of files. Since "xz" offers the most compression, we will be incorporating "xz" when compressing our tarball.

No alt text provided for this image
Code syntax for creating tarball.

The script will create a tarball in accordance to the following parameters:

  • sudo tar -cJf >> this invokes the "tar" command with superuser privileges; the (-c) and (-f) options indicates to the shell that we want to create an archive file, with the (J) option indicating for the archive to be compressed through the "xz" program.
  • backup-on-$(date +%d-%m-%Y)-at-$(date +%I-%M_%p).txz >> this looks convoluted, but it's simply the name for the archive file; the tarball cites the day and time the archive was created (using two different formats of the "date" subshell in the name). The ".txz" extension is the result of a tarball compressed through "xz".
  • $PWD/* >> this cites the directory to archive into a tarball. Since the present working directory changed to $fullpathlocation, the $PWD system variable represents your specified directory in the $fullpathlocation variable. The "/*" adds a subdirectory (where the files are located), and tells the shell to archive all the files using the "*" wildcard.
  • 2>/dev/null >> as the tarball is processed, any standard error messages will be sent to "/dev/null", and will not appear in the shell.

Once the tarball is complete, the script will display the full path of the tarball in the shell:

No alt text provided for this image
Code syntax for final display of "backup.sh" in the shell.

The "ls -c" command will sort and list all the ".txz" tarballs in the present working directory ($PWD) from the most recently modified to the oldest by date, and since the newly created tarball is the most recent, it will be listed as the first entry. To make sure the new tarball is the only backup displayed, we piped "ls -c" into "head -n 1", which will only show the new tarball.

In the end, the file archive script will create a dated backup in the directory we specified.

Shell Script (backup.sh) in Action

No alt text provided for this image
Instance of shell script "backup.sh" being used, producing a tarball in the specified directory (/home/mint/scripts).


No alt text provided for this image
Tarball present in the directory specified.

Thank you so much for reading through this portfolio project! If you enjoyed my take with Bash shell scripting, please feel to reach out and connect. I'm always looking to network with people in the industry.

Until next time!

© 2023, Enoch Deboss

To view or add a comment, sign in

More articles by Enoch Deboss

Insights from the community

Others also viewed

Explore topics