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.


Sunday, August 31, 2014

Folders Updater with Batch

In this post, I present yet another Batch script related to Files and Folders updating but this time with extended functionality. This piece of work was intended to update portable Hard Disk Drives connected to a workstation in a network with a File Server that contains a list of folders with files that are updated and changed oftenly. These hard drives are used by Field Engineers to store important information required for daily field activities. Due to the type of instrument it serves as and the numerous factors affecting the day to day activities of a Field Engineer, it was important for the script to fulfill certain requisites.

First Functionality: Selectable Starting Point

Since said folders contain a collection of large files, the list of folders can take ridiculously long time to fully update (given that much data have been added/updated). Moreover, Field Engineers are often assigned jobs spontaneously, thus requiring them to prepare for the job in a short time span. Therefore, it was imperative that the user can prioritize which folders to update first, depending on the type of job. In light of these, the script has added functionality for the user to be able to choose from which letter to start updating. For example, if the user chooses to start from the letter 'M', then the script will update all folders with names starting with 'M' and onward (in alphabetical order) until the last folder is updated.

Figure 1. Screenshot of CMD at Prompt for Letter
Second Functionality: Auto-Update

Another added key functionality of this script is its ability to auto update itself from the File Server. For instance, a copy of the script must reside in each of the portable Hard Disk Drives. Thus it would be too tedious and redundant to frequently check if there is a newer version in the server and to update the script as necessary. If this functionality had not been there, then this manual process would have been done by whatever amount of users exists. Thankfully, the script is written such that it can look into a location within the File Server and checks whether a newer version exists, then it would update accordingly. This, naturally, saves the users time and work.

New Formatting and Style

Additionally, I will try something new: the code presentation. The code itself will be formatted and presented differently. Hopefully, this will be an improvement over the previously used style and format. At least it is for me; the code formatting is more automatic, because I upload the code to GitHub and let it do the formatting. Throughout the document, I will paste in snippets of code so that it's easier to follow.

So let's get started directly into the code...


Extended File Updater Script: Preparations and Housekeeping

Please keep in mind that the code has been slightly modified to fit into this blog ― not much, only some paths and filenames. So for starters, housekeeping and preparation codes before the real task.


1
@echo off
::Checks if PC is connected to the server. If not exit.
2
3
if NOT EXIST "\\mynetworklocation\mydocuments" exit
4
::Deletes any temp.bat in Z: which could be a local location or removable device.
5
if EXIST "Z:\temp.bat" del /F "Z:\temp.bat"
6
:: Checks the software creation dates.
7
for /F "Delims=" %%I In ('xcopy /DHYL \\where_script_is_located\Generic_Updater.bat Z:\Generic_Updater.bat ^|Findstr /I "File"') Do set /a Newer=%%I 2>Nul
8
9
if %Newer% == 1 goto Update
10
:: T: will be mapped to the folder mydocuments within the server.
11
subst T: "\\mynetworklocation\mydocuments"
12
:: Change directory to our recently mapped T:
13
cd /d T:
14
:: Loops to update the folder tree.
15
for /d %%A in (*) do if NOT EXIST Z:\mydocuments\%%A md "Z:\mydocuments\%%A\1offline_data\"

Here Lines 1-15 are some preparations work before actually executing the main task. Line 1 tells the interpreter not to print out the code to the terminal, while Line 2 checks that a connection to the network resource is possible and the folder is available. Line 5 checks whether the temporary file exists and if so, deletes it. This temporary file, temp.bat, is created by this script to execute additional code that will update the file if there is a newer version in the File Server.

More importantly, Lines 7-9 check if the file residing in the server location is newer or older than the current one in our Hard Drive, which is set to Z:\. As was mentioned, if there is a newer version, then temp.bat is created and executed to update to latest version.



Important: Please notice that I have set all the Hard Drives to be assigned the letter Z:\ in the computers to work with this script. This script assumes the Hard Drives to be in Z:.

Now that we have checked preconditions, we map our network location to T: (Line11-13) and check if all the folders present in T:\ are also in our Hard Drives. If this is not the case, then create said folders (Line 15).

Extended File Updater Script: Some Functions

This part consists of codes written as functions. The first function, Line 17-22, prompts the user for the letter from which he or she wishes to start updating.This function actually makes a call in Line 21 to another function― :toUpper which covers Line 24-35. This small function, taken from the mentioned source, converts lowercase character to uppercase to avoid discrepancies.

16
::Added functionality to ask for which letter to start.
set input = a
17
18
set /P input=Please enter which letter you want to start updating:
19
set letter=%input:~0,1%
20
echo %letter%
21
call :toUpper letter
22
goto F1
23
24
:toUpper str -- converts lowercase character to uppercase
25
::           -- str [in,out] - valref of string variable to be converted
26
:$created 20060101 :$changed 20080219 :$categories StringManipulation
27
:$source http://www.dostips.com
28
if not defined %~1 EXIT /b
29
for %%a in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I"
30
            "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R"
31
            "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z" "ä=Ä"
32
            "ö=Ö" "ü=Ü") do (
33
    call set %~1=%%%~1:%%~a%%
34
)
35
EXIT /b
36
:F1





















The following snippet Line 39-43 is part of the main body with a very specific task: to loop through and update the folders in our Hard Drive, Z:\. Then Line 44 serves as flow control and redirects to the next line of code it should execute.

38
:: Loops through all folders inside Foreigner and update the folders in Z: (hard drive)
39
setlocal EnableDelayedExpansion
40
for /D %%C in (*) do (
41
        CALL :FUNC1 "%%C"
42
)
43
Endlocal
44
GOTO FOLLOW1








Notice that this piece of code makes call to :FUNC1, which is presented in Lines 47-52, below.

45
::This function will extract the first letter and compare it against the letter input by the user. Then update the folders with that starting letter.
46
47
:FUNC1
48
set "theword=%~1"
49
set theletter=!theword:~0,1!
50
echo %theletter%
51
if "%theletter%" GEQ "%letter%" robocopy /Z /E /MIR "%~1\1offline_data" "z:\mydocuments\%~1\1offline_data"
52
EXIT /b








As the comments describe, the above code checks if the letter entered by the user is "greater or equal to" the first letter of current folder's name. If it is not, then skip it and continue until we find folders with names that fulfill such condition.

Extended File Updater Script: Some Final Checks

We also have to make sure that folders deleted in our File Server has to be reflected in our Hard Drives. For instance, if the administrator deletes a folder in the File Server, then the script would not know that this change took place. So the following code takes care of that along with other finishing tasks.

54
:: Loops through HDD to check if Server has all the folders inside HDD mydocuments. If not, then it has been changed/deleted and should be removed from HDD as well.
55
56
:FOLLOW1
57
Z:
58
cd mydocuments
59
for /d %%B in (*) do if NOT EXIST T:\%%B rd /Q /S "Z:\mydocuments\%%B\"
60
:: Free up the mapping.
61
cd /d C:
62
subst T: /d
63
:: We are done at this point.
64
echo Update performed succesfully... Now program will exit...
65
Pause
66
Exit













At this point you might ask, where is the code for the temp.bat that actually updates the script from the server? Well, here below. Remember that this piece of code is executed only if the condition in Line 9 is true. In other words, %Newer% == 1.

67
:Update
68
::If File in server is newer, create a separate batch file to update the script and reexecute.
69
echo start /wait robocopy \\where_script_is_located\ Z:\ Generic_Updater.bat>Z:\temp.txt
70
echo attrib -h Z:\Generic_Updater.bat>>Z:\temp.txt
71
echo touch -d "+1 minutes" Z:\Generic_Updater.bat>>Z:\temp.txt
72
echo attrib +h Z:\Generic_Updater.bat>>Z:\temp.txt
73
echo Z:\Generic_Updater.bat>>Z:\temp.txt
74
Z:
75
ren temp.txt temp.bat
76
::Execute the newly written batch file.
77
Z:\temp.bat
78
Pause













In short, this code basically creates a file named temp.bat in Z:\ (our Hard Drive) with code that will copy the newer script from the File Server and paste it in our Hard Drive replacing the old script file. Finally, temp.bat is executed and at the very end, it executes our main script (Generic_Updater.batagain as shown in Line 73.

Notice: The Echo and the ">>" actually redirect text into whatever is to the right of >>. For example, Line 70 actually writes attrib -h Z:\Generic_Updater.bat into the textfile temp.txt. This is later renamed to temp.bat in Line 75.

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.

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 issue with it. Check out SanDisk Ultra 32GB USB 3.0 Flash Drive if you are interested.

And Concluding...

As usual, I am not responsible for any damage that this script may cause. So from here you can Copy/Paste all the snippets and place them in the order described by the line numbers. Numbers that are skipped are empty lines, so don't panic. This long piece of work exercises many useful concepts that can be used in other scripting projects. Unfortunately, you will have to understand the code fairly well in order to be able to adapt it to your environment. The way this is written is not particularly aimed to be flexible to change into other working environment. Nonetheless, you can always ask me. See you till next one!!!

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