Ivy Consultants Inc.

Consulting Services for Security, Networking, Wi-Fi and Windows Server

 In this lesson, you will learn how to create executable bash scripts in Linux. Bash scripts are a powerful tool for automating tasks, running a series of commands, and managing system operations. By the end of this lesson, you will understand the basics of script creation, how to make a script executable, and how to run it. This skill is vital for anyone looking to streamline their workflow in a Linux environment.

Understanding Bash Scripts

A Bash script is a plain text file containing a series of commands that the bash shell can execute. These scripts can range from simple to complex, depending on the tasks they are designed to perform. The beauty of bash scripts lies in their ability to automate repetitive tasks, thereby saving time and reducing the potential for human error.

Creating a Basic Bash Script

To create a bash script, you first need a text editor. For beginners, nano is a user-friendly choice. Let’s create a simple script that prints “Hello, World!” to the terminal. Open your terminal and type:

nano hello_world.sh

This command opens the nano editor and creates a file named hello_world.sh. In the editor, write the following script:

#!/bin/bash

echo “Hello, World!”

The first line, #!/bin/bash, is called the shebang. It tells the system that this script should be run in the bash shell. The second line is the command that prints “Hello, World!” to the screen.

Making the Script Executable

After saving the file, the script is not yet ready to run. You need to make it executable. To do this, use the chmod command:

chmod +x hello_world.sh

This command changes the script’s permissions, allowing it to be executed.

Running the Bash Script

Now that your script is executable, you can run it directly from the terminal. Navigate to the directory where your script is located and type:

./hello_world.sh

You should see “Hello, World!” printed in your terminal.

Verifying Your Script Works

To verify that your script runs correctly, you can look for the output in the terminal. After running the ./hello_world.sh command, if you see “Hello, World!” displayed, your script is working correctly.

In the video above we also added a sub expression operator to the echo command like so:

echo “Hello, world! The current date is $(date)”

Bash script for automating the backup of your home directory to a specific folder

you will learn how to create a bash script that automates the backup of your home directory to a specific folder, /backups, and manages these backups by deleting any that are older than 30 days. This script will help you maintain a regular backup regimen and manage disk space effectively.

Importance of Automated Backups

Automating backups is crucial for data safety. Regular backups protect against data loss due to hardware failure, accidental deletions, or corruption. Automating this process ensures backups are done consistently without manual intervention.

Setting Up the Backup Directory

Before we start writing the script, ensure that the /backups directory exists. This directory will store all our backups. You can create it using:

sudo mkdir /backups

Writing the Backup Script

Open a Text Editor: We’ll use nano for this task. Open it with the following command:

nano backup_script.sh

Script Content: In the nano editor, you will write the script below. I have broken the script up into several parts and will explain each part.

The script starts by setting up the destination for our backups and the naming convention for the backup files. We choose /backups as our storage directory. The backup file name includes the current date for easy tracking.

#!/bin/bash

# This script backs up the /home directory to /backups

# Define the backup file path

BACKUP_DIR=”/backups”

BACKUP_FILE=”$BACKUP_DIR/home-$(date +%Y%m%d-%H%M%S).tar.gz”

Explanation:

  • #!/bin/bash is the shebang line, telling the system this script should be run with Bash.
  • BACKUP_DIR and BACKUP_FILE are variables. We use $(date +%Y%m%d-%H%M%S) to insert the current date and time into the filename, making each backup unique.

Next, we create the backup. The script compresses the user’s home directory and saves it to the location we defined earlier. It then prints a message to indicate successful completion.

# Create a backup of the home directory

tar -czvf $BACKUP_FILE /home

echo “Backup of home directory completed successfully”

Explanation:

  • tar -czvf is a command to create (c) a compressed (z) archive, with verbose output (v), and specifies the filename (f).
  • /home is where our file system stores the users home directories excluding the root user. So backing up this directly will backup all non-root users home files.

Finally, the script handles the deletion of old backups. It searches for and deletes backups older than 30 days in our designated backup directory. This is a crucial step in managing storage space effectively.

# Delete backups older than 30 days

echo “Deleting backups older than 30 days”

sudo find $BACKUP_DIR -type f -name ‘home-*.tar.gz’ -mtime +30 -exec rm -f {} \;

Explanation:

  • find is a command to search for files. -type f looks for files, and -name ‘home-*.tar.gz’ specifies the filename pattern.
  • -mtime +30 finds files modified more than 30 days ago.
  • -exec rm {} \; tells the script to execute the rm (remove) command on each file found, where {} is a placeholder for each file name.

For testing, you can change the find command to the example below which will delete backups older than 5 minutes… just don’t forget to put it back 😀

sudo find $BACKUP_DIR -type f -name ‘home-*.tar.gz’ -mmin +5 -exec rm -f {} \;

This script does the following:

  • Defines the backup directory and file name, including a date stamp.
  • Uses tar to create a compressed archive of your home directory.
  • Uses find to delete backups older than 30 days.

Make the Script Executable: Save the file and exit nano. Then, change the script’s permissions to make it executable:

chmod +x backup_script.sh

Run the script