Librenix
Headlines | Linux | Apps | Coding | BSD | Admin | News
Information for Linux System Administration 

Scripting: A parallel Linux backup script

Up
vote
Down

This example bash shell script demonstrates a simple method of creating backups of multiple filesystems to multiple tape devices simultaneously. While the script presented writes to four tape drives in parallel, it can easily be modified to write to other device types and to create a different number of backup streams. The script is set up for the bash shell under Linux, but modifying it for another variety of Unix should simply be a matter of changing the locations of utility files such as tar, echo, cp, and sleep.

The script can be downloaded from http://librenix.com/scripts/par.tar.sh. Download the file now and load it into an editor as this article will refer to it frequently. Also, you may want to modify bits of it to match your filesystem names and your devices.

The first line of the script looks like this:
 #!/bin/bash
If the bash shell isn’t in the /bin directory on your system, you’ll need to modify this line. Enter the command which bash now to verify the location of bash. My Fedora Linux system and my Mac OS X system both have bash in /bin, but my FreeBSD system does not. If you have a non-Linux flavor of Unix, you’ll probably need to use the ‘which’ command to verify the locations of each command used in the script. The commands used are:
 bash
cd
sleep
echo
date
tar
wait
ls
wc
Note that ‘wait’ and ‘cd’ are usually implemented as internal shell commands and may not have external commands associated with them. If that is true for your system, leave ‘cd’ and ‘wait’ with no directory prefix just as they are in the original script.

Now, the first command in the script resets the current working directory to ‘/’:
 cd /
Since the script precedes each directory to be backed up with a ‘.’ to represent the current working directory, starting out at ‘/’ is necessary. The reason for this precaution is that some implementations of the tar command will only load files from a tar archive into the exact directory that was specified when the file was backed up. By prefixing the names with a ‘.’ we preserve the ability to recover the files into any subdirectory we want, without overwriting the original files.

Immediately after the ‘cd /’ command is where you would put any commands to shut down all services that must be quieted prior to a backup. The example script has a (commented out) command to initiate an Oracle database shutdown followed by a ‘sleep’ command to allow time for the shutdown to complete. The example database shutdown and the following delay probably don’t apply to your system. Obviously, you’ll have to add commands yourself to stop any applications that might interfere with the backup.

Next, we use the ‘date’ command to create two sets of four tiny files to stick at the start and end of each tape. Note that the presence of a ‘date.#’ file at the beginning of each tape lets you quickly find out when a tape was created and on which drive. The ‘zzzz.#’ files, appended to the end of each tape, only serve to let you easily verify that a backup completed without overrunning the end of the tape.

Next, we start the four actual ‘tar’ backup commands, each with sample directories named ‘./dir1’, ‘./dir2’, etc. Of course, you’ll need to modify the list of directories to match the actual directories you wish to back up. Note that you’ll probably want to balance the directory sizes so that all of the largest directores aren’t on the same tape. Also, note that each ‘tar’ command is run in the background and logs to a tar.#.log file in the /tmp directory. Obviously, you might want to put the logfiles somewhere else.

After each ‘tar’ command there is an entry like this: ‘TASK=$0’, or ‘TASK=$1’. These arbitrarily-named ‘TASK’ variables are used to store the process ID of each ‘tar’ command so that the script can wait for them with the four ‘wait’ commands that follow in the next block of code. There, we have the four ‘wait’ commands waiting on the $TASK0, etc, variables. (The addition of the ‘$’ to each TASK# shell variable is not a typo -- it’s necessary to read back the contents of the variable.)

Next, after the script has waited for the completion of each of the four ‘tar’ commands, it appends some information to a history file for later reference. It stores the date of the backup, the filesize of the logfile, and the number of files backed up on each tape to each of four history files. While the script will overwrite the logfiles (tar.#.log) each time it is run, it will append these three lines to each of the four history files (tar.#.history).

The final steps in the script are commented out. Those are the commands necessary to restart any applications that were brought down for the backup. Again, in the example we assume an Oracle database needs to be restarted. You’ll need to add the commands necessary to start any applications that were stopped at the beginning of the script.
mail this link | permapage | score:9419 | -Ray, April 10, 2005

Pattern matching in shell scripting

Up
vote
Down

This article is excerpted from the book Beginning Portable Shell Scripting.
Shell programming is heavily dependent on string processing. The term string is used generically to refer to any sequence of characters; typical examples of strings might be a line of input or a single argument to a command. Users enter responses to prompts, file names are generated, and commands produce output. Recurring throughout this is the need to determine whether a given string conforms to a given pattern; this process is called pattern matching. The shell has a fair amount of built-in pattern matching functionality.
read more...
mail this link | permapage | score:9145 | -Ray, January 1, 2009

Scripting: Put a clock in your bash terminal

Up
vote
Down

In the original version, the cursor positioning didn't work on my Mac OS X system. If that happens to you, try this simplified variant:
  #!/bin/bash
while true
do
tput sc
tput cup 0 60
echo -en `date +"%H:%M:%S %F"`
tput rc
sleep 1
done
Also, note that you'll need to run either script in the background to use your terminal.
The script saves the current cursor position with an ANSI escape sequence instruction. Then, using the tput command, the cursor is sent to row 0 (the top of the screen) and the last column minus 19 characters (19 is the length of HH:MM:SS YYYY-MM-DD). The formatted date command is displayed in green inverted color. The cursor is then sent back to its original position with another ANSI sequence that restores the original saved position.
read more...
mail this link | permapage | score:9110 | -Ray, January 22, 2008

Mac Shell Scripting Tutorial

Up
vote
Down

A tutorial on scripting for the Mac, from Apple.
This document assumes that you already have some basic understanding of at least one procedural programming language such as C. It does not assumes that you have very much knowledge of commands executed from the terminal, though, and thus should be readable even if you have never run the Terminal application before.

The techniques in this document are not specific to Mac OS X, although this document does note various quirks of certain command-line utilities in various operating systems. In particular, it includes information about some cases where the Mac OS X versions of command-line utilities behave differently than other commonly available versions such as the GNU equivalents commonly used in Linux and some BSD systems.
read more...
mail this link | permapage | score:9025 | -Ray, October 10, 2006

bash scripting: Looping through a list

Up
vote
Down

This is an example of a Bash shell script used to loop through a list to compare to text strings found in logs. The script is used to detect and block attacks on a web site. read more...
permapage | score:8565 | -aweber, December 26, 2011

Scripting: Bash Array Tutorial

Up
vote
Down

An excellent introduction to bash arrays including 15 examples...
$ cat arraymanip.sh
#! /bin/bash
Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse'

echo ${Unix[1]}

$./arraymanip.sh
Red hat
read more...
permapage | score:8559 | -Ray, June 7, 2010

Monitor a Linux service with watchdog scripting

Up
vote
Down

Monitor your web server, or just possibly, that program that mysteriously disappears occasionally...
Old Unix hands already know this, but new Unix (Linux) users may be asking, ‘What is a “watchdog script”?’ Basically it is a bash or other script that is run via cron periodically to check on a persistent service. The watchdog script takes actions based on the state of the service it monitors.
read more...
permapage | score:8483 | -Ray, May 19, 2010

The Squirrel shell and portable scripting language

Up
vote
Down

If you don't want to commit to the idiosyncrasies of a specific shell running on a particular platform, try the Squirrel Shell. The Squirrel Shell provides an advanced, object-oriented scripting language that works equally well on UNIX, Linux, Mac OS X, and Windows systems. Write a script once, and run it anywhere. read more...
permapage | score:8470 | -solrac, March 19, 2009

Bash Shell Scripting String Functions

Up
vote
Down

This intermediate bash shell scripting article provides some useful functions, in source code of course.
In C, defines strcat(3), strcpy(3), strlen(3), and strcmp(3) for string concatenation, copy, size, and test operations respectively. Such basic operations are needed constantly when programming in any language, and shell scripting is no exception.
read more...
permapage | score:8462 | -Ray, November 12, 2004

Scripting: Bash floating point

Up
vote
Down

Is the lack of floating point math putting a crimp in your scripting style?
When you think about it, it's surprising how many programming tasks don't require the use of floating point numbers. If you're an embedded systems programmer, you'd probably get fired for using "double" in a C program. If you write PHP or JavaScript, quick, do they even support floating point? One language that doesn't support it is Bash, but let's not let that stop us...

The obvious candidate for adding floating point capabilities to bash is bc.
read more...
mail this link | permapage | score:8383 | -Ray, August 5, 2008

The Vim Scripting editor

Up
vote
Down

Vimscript is a mechanism for reshaping and extending the Vim editor. Scripting allows you to create new tools, simplify common tasks, and even redesign and replace existing editor features. Start with Part 1 and get the basic elements of Vimscript. In Part 2 learn how to create and deploy new functions in the Vimscript. In Part 3 Explore Vimscript's support for lists and arrays. read more...
mail this link | permapage | score:8353 | -solrac, February 4, 2010

Learn awk scripting

Up
vote
Down

awk is a wonderfully powerful little Unix scripting language.
The awk utility is a small program that executes awk language scripts, which are often one-liners, but just as easily may be larger programs saved in a text file. For example, to execute an awk script saved in the file prg1.awk and have it process the file data1, you could use a command such as:
read more...
permapage | score:8343 | -Ray, January 24, 2006

Scripting: Convert your gzip files to bzip2

Up
vote
Down

A 'rezip' script that converts your .gz files to .bz2 for better compression and maybe better error recovery -- at a substantial cost in compression time, of course. Rezip...
Uses a simple text file of paths and filenames for input -- so you can save the results of "find" to a file, run rezip, and the files will be re-compressed one at a time, with a running log and no user intervention (as long as there's free space on the destination drive.) Example:

$ find /mnt/bkps -name *.gz > ~/rezipp-files.txt && rezip
read more...
mail this link | permapage | score:8330 | -Ray, February 13, 2006

Scripting: Bash sub shells

Up
vote
Down

Sometimes you need to run scripts in the background...
Creating sub-shells in bash is simple: just put the commands to be run in the sub-shell inside parentheses. This causes bash to start the commands as a separate process. This group of commands essentially acts like a separate script file, their input/output can be collectively redirected and/or they can be executed in the background by following the closing parenthesis with an ampersand.
read more...
permapage | score:8268 | -Ray, November 17, 2008

Shell Scripting: A Low-precision Visual Timer

Up
vote
Down

A low-precision timer script for your Linux / Unix box...
For many years, I've wanted a simple command-line timer program to use in conjunction with short human tasks — the sort of thing my wristwatch timer works for, but I'm not always wearing that watch. I want something that provides some indication of elapsed time or time remaining. Amazingly, the commercial Unixes I'm familiar with do not provide such a utility; nor does one seem to be readily available with the standard X Window tools.
read more...
mail this link | permapage | score:8199 | -Ray, May 10, 2004

Scripting: Looping in Bash

Up
vote
Down

Looping constructs for bash scripts...
The for loop is used to iterate through an action until done. Let's say you have a directory full of images that you want to convert from one format to another. You could use a for loop and ImageMagick's convert (or another program) to convert JPEG images into PNG format, for instance. Or create a script to take a directory full of MP3s or WAV files and convert to Ogg Vorbis.

The while loop is used to perform an action while a condition is true, and until does the opposite — it performs an action until a condition is true. So you might run a counter, for example, up to 10 and perform an action until the counter hits 10. We'll look at this more closely in the examples.
read more...
mail this link | permapage | score:8193 | -Ray, October 27, 2010

Sysadmin Shell Scripting

Up
vote
Down

This three-part series covers basic, intermediate and advanced shell scripting techniques for system administrators.
A working knowledge of shell scripting is vital if someone wants to become good at system administration tasks. Since this tutorial tackles topics that assume a basic understanding of shell scripting, we strongly urge you to take a look at our Shell Scripting: The Basics article first…

One important aspect of shell scripting is file-oriented utility. A file-oriented utility is basically used as a filter in a pipe.

We can add a ‘-’ to get a more useful result. That is, when we have ‘file -’, the shell waits for the user input and analyses it.
read more...
mail this link | permapage | score:7980 | -Ray, December 17, 2010

Scripting: wait vs. sleep

Up
vote
Down

When coordinating parallelized processes, wait is the essential command.
The wait command man page simply states that it pauses script execution until a specified process ID completes. The man page for the sleep command is a single line stating that it causes a script to pause for specified period of time.

How do you decide which is appropriate for the script at hand? In some cases it won't make any difference to the successful completion of the script. Other times it can be the difference between success and total failure to produce the expected results.
read more...
mail this link | permapage | score:7951 | -Ray, March 29, 2005

Book Review: Mastering Shell Scripting

Up
vote
Down

Mastering Shell Scripting sounds like a useful resource for the scripting system administrator.
Despite the above criticisms, the scripts presented in this book are well written and easy to follow. An example that concludes the "Basics" chapter is the excellent test_string.ksh script. Pass an argument to this script, and it checks whether the argument is numeric, alpha, lower or uppercase, hex or mixed case, etc. All the regular expressions typically used with a case statement are included.
read more...
mail this link | permapage | score:7839 | -Ray, November 20, 2003

Scripting ps and grep

Up
vote
Down

A scripted approach to grepping what you want out of the ps command...
If you've worked in Unix for any length of time, you're probably familiar with psg or some such variant. Psg stands for "ps through grep" (essentially a ps query whose result is piped grep). It works and works well, but sometimes you need more than just a process string; sometimes you want the process string AND the process ID (PID) — separated. Oh yes, and since we're compiling a wish list here, how about the operation simply telling me how many processes match a given target string. I'll show how to do all of that — and do it without having to run multiple ps commands and filtering those results through several piped operations.
read more...
mail this link | permapage | score:7800 | -Ray, February 9, 2006
More articles...
Large Abstract Art with Fine Details and Textures

Selected articles

Graffiti Server Download Page

Tutorial: Introduction to Linux files

Missing the point of the Mac Mini

Testing the Digital Ocean $5 Cloud Servers with an MMORPG

Linux dominates Windows

Closed Source Linux Distribution Launched

Scripting: A parallel Linux backup script

Space Tyrant: A threaded game server project in C

VPS: Xen vs. OpenVZ

Space Tyrant: A threaded C game project: First Code

The Real Microsoft Monopoly

The short life and hard times of a Linux virus

Linux vs. Windows: Why Linux will win

The Network Computer: An opportunity for Linux

Microsoft to push unlicensed users to Linux

Why Programmers are not Software Engineers

Space Tyrant: A multiplayer network game for Linux

Programming Language Tradeoffs: 3GL vs 4GL

No, RMS, Linux is not GNU/Linux

Why software sucks

Shadow.sh: A simple directory shadowing script for Linux

Apple to Intel move no threat to Linux

The Supreme Court is wrong on Copyright Case

Space Tyrant: Multithreading lessons learned on SMP hardware

Apple DIY Repair

MiniLesson: An introduction to Linux in ten commands

Beneficial Computer Viruses

How to install Ubuntu Linux on the decTOP SFF computer

Librenix T-Shirts and Coffee Mugs!

Mono-culture and the .NETwork effect

Download: Linux 3D Client for Starship Traders

Hacker Haiku

The life cycle of a programmer

 

Firefox sidebar

Site map

Site info

News feed

Features

Login
(to post)

Search

 
Articles are owned by their authors.   © 2000-2012 Ray Yeargin