Linux Portfolio Project - Backup Files in a Specified Directory into a Tarball (Bash Shell Scripting)
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
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:
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:
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.
Recommended by LinkedIn
The script will create a tarball in accordance to the following parameters:
Once the tarball is complete, the script will display the full path of the tarball 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
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