X

Shell Script : A Brief Insight

Whoever uses Linux and works with many interactive commands, possibly should have already heard about Shell Script. Shell Script is a good way to put together all the commands that we use frequently, as a sequence, in a single file.

It can facilitate System Admin’s life, with backup routines, custom logs, disk reports, and a thousand of things.

In this article, I will show you a simple Shell Script and I will explain all the details about each line.

Things about Shell Script that we must know, before starting:

 

./myFirstScript.sh (dot and slash: It will run the shell script, even if It is not in the PATH

 sh myFirstScript.sh (It will run the shell script through sh command-line interpreter)

 bash myFirstScript.sh (It will run the shell script thought bash command-line interpreter)

 

Let’s start…

 

Step 1: Create the shell script file

Choose some text editor to create your script file. It’s up to you; you can use vim or nano editor.

We will use the vim editor to create our shell script file, so enter:

vim dailyBackup.sh

 

 

Step 2: Choose a command-line interpreter:

Always, in the first line, we should choose a command-line interpreter. We chose Bash as interpreter.

The first line is the only place that # (hash/number sign) is not interpreted as a comment, so write #! and

the full name of the interpreter :

#!/bin/bash

 

 

Step 3: Add some comments to organize your script . Example:

# This script will take backups from mysql

# Author Katya Longhin

# Version V1.2 – 18/11/2014

 

 

Step 4: Define variables

There are different types of variables. In this case, we are defining simple variables and variables with

commands.

We are using the command date and its parameters. We can use “ to interpret a command or

$(command). So, another way to create the variable $today is: today=$(date +%Y-%m-%d):

today=`date +%Y%m%d`

database=database_name

user=backup_user

password=your_password

persist_days=7

bkp_dir=/backup

dumpfile=dumpfile_$today.sql

 

 

Step 5: Create a database backup

We are displaying a message with echo command and using mysqldump to backup the database.

We are also compressing the dump file using tar command and removing from the directory, old version

of dump file.

echo "Creating database backup..."

mysqldump -u$user -p$password $database > $bkp_dir/$dumpfile

tar -cvzf $bkp_dir/$dumpfile.gz $bkp_dir/$dumpfile

rm -rf $bkp_dir/$dumpfile

 

 

Step 6: Delete old files

We defined the variable $persist_days with a backup persistent. We will keep only backups that we

have created in the last 7 days, so we are using the find command to find all files with the suffix .sql.gz

that were created 7 days before and older.

echo "Deleting old backup files..."

find $bkp_dir -mtime $persist_days -type f -name "*.sql.gz" -delete

 

 

Step 7: Send an e-mail to the administrator, if backup fails

We are creating a conditional method to send a message to the administrator.

If the file is not empty and the file exists (-s), the backup was done, else the script will send a message to

the administrator.

echo “Sending a message to the administrator, if the backup was not done”

if [ -s $dumpfile ]; then

echo "Backup was done..."

else

echo "Backup file was not created or something wrong happened \n\n Message sent by script

dailyBackup.sh \n\n`date`" | mail -s "Backup error" administrator@email.com

fi

 

 

The final result

#!/bin/bash

# This script will take backups from mysql

# Author Katya Longhin

# Version V1.2 – 18/11/2014

today=`date +%Y%m%d`

database=database_name

user=backup_user

password=your_password

persist_days=7

bkp_dir=/backup

dumpfile=dumpfile_$today.sql

echo "Creating database backup..."

mysqldump -u$user -p$password $database > $bkp_dir/$dumpfile

tar -cvzf $bkp_dir/$dumpfile.gz $bkp_dir/$dumpfile

rm -rf $bkp_dir/$dumpfile

echo "Deleting old backup files..."

find $bkp_dir -mtime $persist_days -type f -name "*.sql.gz" -delete

echo “Sending a message to the administrator, if the backup was not done”

if [ -s $dumpfile ]; then

echo "Backup was done..."

else

echo "Backup file was not created or something wrong happened \n\n Message sent by script

dailyBackup.sh \n\n`date`" | mail -s "Backup error" administrator@email.com

fi

 

  • Tags: bash script, linux shell script, mysql backup, script, Shell script,