Thursday, December 25, 2014

Portable Hard Disk Drive Updater with Bash

Welcome to yet another (hopefully informative) post of the JVL blog!

This time we will delve into something new: Bash Scripting. This edition of the blog engages you into the basics of Bash Scripting. So, what exactly is Bash? Basically, Bash, or "Bourne Again Shell" is a Command Interpreter of the Unix System. Bash is a shell equivalent to Batch (in Windows OS) but for Linux, a derivation of Unix. A shell is an interface that interacts with the user and interprets commands typed by users to execute tasks directly on services of the Operating System. In other words, it is the interpreter between the user and the Operating System. Many of the Linux distributions use Bash as the default shell interpreter including the well-known Ubuntu. First, let's look at a screenshot of the terminal from Ubuntu.


Figure 1. Screenshot of Ubuntu's Terminal

As we have seen in previous posts, command interpreters can be very powerful and useful in performing repetitive tasks that users may not want to perform manually. To better explain the basics of Bash Scripting, I will walk you through a simple script I have written to update a Portable Hard Disk connected to my Linux system through USB. The Portable Hard Disk updates files from "Secondary", a datastore partition. "Secondary" stores all my data (Music, Videos, Documents, Installers, etc...) in my computer. As many would agree, it is a good idea to backup the datastore partition frequently to avoid unnecessary catastrophic losses... This script does exactly that!! Now let's take a look at the code...

The Hard Disk Drive Updater

The script has a total of less than 30 lines of code.  Although the code is quite simple, I have divided them in 3 blocks of codes so that they are are easier to follow and understand, In addition, this layout keeps consistency and uniformity. Let's take a look at the first block.



1
#!/bin/sh
2
3
#This script is to mirror the content of Secondary into the corresponding portable hard disk drive.
4
5

6
7
CURRENT_DIR=/media/user/Secondary
8
UPDATE_DIR1=/media/user/Seagate
9
UPDATE_DIR2=/media/user/Passport

The first line is quite important as it tells the shell what kind of interpreter to use (in this case I have specificed Bash) The following line (starting with #) is simply a comment line which the interpreter ignores. Lines 7-9 define some variables. That is, they save the directory path into symbolic representations. For example, CURRENT_DIR represents the path to /media/user/Secondary and so forth. Next, we have the main algorithm in the following block:

12
#Check if the Seagate drive is present. In other words, mounted.
13
if [ -d "$UPDATE_DIR1" ] && [ -d "$CURRENT_DIR" ]
14
then
15
        echo "Seagate Backup Drive was found correctly!"
16
        #updates the folder with the rsync function while excluding certain folders.
17
        sudo rsync -gloptvru --delete --exclude '*.tib' --exclude 'System Volume Information' --exclude '*RECYCLE*' $CURRENT_DIR/* $UPDATE_DIR1
18
19
fi

Observe Lines 13-19 for its syntax and structure. It is a big conditional block of code, the if-then. Similar to the conditional if-then from other programming languages, the code inside the "then" block is executed if the statement after "if" is evaluated to true. The "if" in Line 13 checks whether we have both the source and destination folders present and mounted in our Linux system. This implies that our Portable Hard Drive partition is mounted correctly and present when the statement is evaluated to true.

Lines 17-18 use the rsync program along with its plethora of features and options to transfer files. These options can greatly speed up and enhance the transfer process. For instance, the code copies source files from CURRENT_DIR to UPDATE_DIR1 preserving file attributes such as group owners and users owners, updating and skipping files that already exist in the destination folder, transferring files in verbose and recursive mode, and more.

Also notice that files with '*.tib' file extensions, 'System Volume Information', and 'RECYCLE' are excluded  from this command. The '*.tib' is used to omit files containing such extension since I know such files do not need to be updated. The other parameters refer to files that contain important information about the partition. Therefore, we want to skip them to avoid being overwritten. Please refer to the rsync manual page for detailed information. Be aware that you use this at your own risk.

22
#Check if the Jams' Passport Drive is present. In other words, mounted.
23
if [ -d "$UPDATE_DIR2" ] && [ -d "$CURRENT_DIR" ]
24
then
25
        echo "Jams Passport was found correctly!"
26
        #updates the folder with the rsync function while excluding certain folders.
27
        sudo rsync -gloptvru --delete --exclude '*.tib' --exclude 'System Volume Information' --exclude '*RECYCLE*' $CURRENT_DIR/* $UPDATE_DIR2
28
29
fi

As you may have already noticed, this block of code Lines 22-29 is exactly the same as the previous block, Lines 12-19, except that they differ in their destination folders: UPDATE_DIR2 vs UPDATE_DIR1. What does it mean then? Simple. As I had 2 different Portable Hard Drives, I check which of the 2 drives is connected to the computer and update accordingly. So UPDATE_DIR1 and UPDATE_DIR2 represent 2 different Portable Hard Drives partitions. In another words, the interpreter only executes the code that corresponds to the connected drive.

To tweak this code to fit your needs, you have to change the paths defined in Lines 7-9 to your own paths. If you have a 3rd Portable Hard Drive, then just add a 3rd variable, say UPDATE_DIR3, specify its path, and replicate the if-then block with the adapted 3rd variable. Indeed, it is as simple as described. What if you only have 1 Portable Hard Drive? What should you do?

Products Recommendations

If you think this might be a method you could use to backup files, then I suggest you get a reliable portable hard drive. I personally have a Seagate and a Western Digital and have found them to be quite reliable (5+ years old). You could check this Seagate Backup Plus Slim 2TB Portable External Hard Drive USB 3.0 or this other WD 1TB Elements Portable External Hard Drive - USB 3.0 out.



If you wanted to use something a bit more compact or start off with a less expensive device, USB drives could actually work as well. I have personally had SanDisk's USB drives  and never had any issues with them. Check out SanDisk Ultra 32GB USB 3.0 Flash Drive if you are interested.

And some final words...

As we have explored a bit of the Bash and learned some of its basics, we can progress to discover more functionalities and features that it can offer. This little script serves as an initial step to discover a lot more possibilites for this powerful and versatile tool... Beware though! You use this solely at your own risk. Again, I am not responsible for the damages that this may cause. Misuse can lead to loss of data. Refrain yourself from testing this script on important data. As customary, I welcome comments and suggestions! And hope this was somewhat useful to you!!! Don't hesitate to contact me for questions!

Important: You use this at your own risk as I will not be held responsible for any damage you may cause.


No comments:

Post a Comment