Understanding Bash Scripting Through a Backup Script

Understanding Bash Scripting Through a Backup Script

Introduction

Bash scripting is a powerful tool every software engineer should possess. It allows users to automate tasks and manage systems efficiently. This article aims to explore various bash scripting concepts through the lens of a backup script.

Concepts

Shebang (#!/bin/bash)

The shebang #!/bin/bash at the beginning of the script specifies the interpreter for executing the script. In this case, It's the Bash shell

Comments

Comments in the bash are denoted by #, and are used to annotate code.They make the script easier to understand and maintain. They do not affect the execution of the script in any way.

# This is a comment

Variables

Varaibles in bash scripting just like any other programming language are containers for storing data. In this script name is a variable that stores my name.

name="Steve"
echo "Hello $name" # prints out Hello Steve

Conditional Statements

Conditional statements control the flow of the script. The if statement in the script checks the condition executes statements either in the if or else block based on the condition. There are various comparison operators:

  1. -eq : is equal to

  2. -ne : is not equal to

  3. -ge : is greater than or equal to

  4. -lt : is less than

  5. -le : is less than or equal to

var=5
if [ $var -eq 5 ]
then
    echo "Hello There"
else
    echo "Goodbye"
fi
# prints out Hello There

Command substitution

Command subsitution, denoted $(command), runs a command and substitutes it's output. In this script, it's used to assign the output of the date and which commands to variables.

current_date=$(date +%Y-%m-%d)
echo "$current_date" # prints out date in YYYY-MM-DD format

Exit Status

The exit status of a command, given by exit, indicates success or failure. In this script exit 1 is used to terminate the script if the conditions are not met. This sends a status code back to the operating system indicating the program did not work successfully.

var=1
if [ $var -ne 1 ]
then
    echo "Condition not met"
    exit 1
fi

Loops

Loops, such as for and while, are used to repeat a section of code until a certain condition is met.

for i in {1..5}
do
    echo "Welcome"
done
# prints out Welcome 5 times

Functions

Functions are reuseable pieces of code. They can make scripts more modular and easier to maintain.

function greet() {
    echo "Hello, $1"
}

greet 'World'
# prints out Hello, World

Arrays

Arrays are variables that can hold multiple values. They can be useful for storing lists of similar items.

fruits=('apple' 'banana' 'cherry')
echo ${fruits[0]} # prints out 'apple'

The Backup Script

We are going to bring some of these concepts together and create a backup script for linux.

This script is designed to backup a directory using the rsync command. it checks the number of arguments and whether rsync is installed, then performs the backup.

The script starts by checking if the user has entered exactly two arguments. If not, it displays a usage message and exits. Next, it checks if rsync is installed. If not, it asks the user to install it and exits. Then, it captures the current date and stores it in a variable. Finally, it runs the rsync command with the specified options to perform the backup.

How to Use the Script

To use the script, you would run it with two arguments: the source directory and the target directory. For example: ./backup.sh /home/user/documents /home/user/backup

#!/bin/bash

# Check to see if user has entered exactly two arguments

if [ $# -ne 2 ]
then
    echo "Usage: backup.sh <source_directory> <target_directory>"
    echo "Please try again."
    exit 1
fi

# Check if rsync is installed

if ! command -v rsync > /dev/null 2>&1
then
    echo "This script requires rsync to be installed."
    echo "Please use your distribution's package manager to install."
    exit 2
fi

# Capture the current date, and store it in the format YYYY-MM-DD

current_date=$(date +%Y-%m-%d)
rsync_options="-avb --backup-dir $2/$current_date --delete"
$(which rsync) $rsync_options $1 $2/current >> backup_$current_date.log

echo "Your directory has been backed up"

Understanding these bash scripting concepts can help you write more complex scripts and automate tasks more efficiently. By examining this backup script, we've seen how these concepts come together in a practical application.

I hope you found this article helpful!, If you have any questions or need further clarification, feel free to ask.

Acknowledgements

This backup script was inspired by the content provided by Learn Linux TV.