Bash. Automation with Bash

You can now use all the bash skills you learned in this previous series of bash articles to create very useful bash scripts that can help you automate boring repetitive administrative tasks.

Automation should really be your ultimate goal whenever you write a bash script.

In this article, we will show you several automation scripts that you can later extend to automate any task. These scripts will use bash arrays, if-else, for loops, and other concepts that you learned in this series.

Automating user management with bash script

Creating a user across multiple servers can be something you, as a sysadmin, will do on a daily basis. This is a tedious task, so let’s create a bash script that automates it.

First, create a text file that includes all the hostnames or IP addresses of the servers where you want to add the user.

For example, here I have created a servers.txt file that includes five different servers:

[email protected]:~$ cat servers.txt
server1
server2
server3
server4
server5

Keep in mind that vswe used the hostnames of the servers as we have already included the IP addresses in our / etc / hosts file. You can also use SSH config file here.

Now take a look at the following bash script adduser.sh:

#!/bin/bash

servers=$(cat servers.txt)

echo -n "Введите имя: "
read name
echo -n "Введите пользователя id: "
read uid

for i in $servers; do
echo $i
ssh $i "sudo useradd -m -u $uid ansible"
if [ $? -eq 0 ]; then
echo "Пользователь $name добавлен $i"
else
echo "Ошибка $i"
fi
done

The adduser.sh script will first ask you for the username and ID of the user you want to add; then it will loop through and connect to all servers in the servers.txt file via SSH and add the requested user.

Let’s run the script and see how it works.

The script ran successfully and user access was added to all five servers. There are several important points you need to understand here:

  • You can use blank ssh passphrases or run ssh-agent to avoid being prompted for a key (or password) while the script is running.
  • You must have a valid account with superuser access (no password required) on all servers.

Imagine adding a user to over 100 different Linux servers! The Adduser.sh script can definitely save you countless hours of work.

Automating backups with a bash script

Backups are something we all do on a regular basis, so why not automate it? Take a look at the following backup.sh script:

#!/bin/bash

backup_dirs=("/etc" "/home" "/boot")
dest_dir="/backup"
dest_server="server1"
backup_date=$(date +%b-%d-%y)

echo "Запуск резервного копирования: ${backup_dirs[@]}"

for i in "${backup_dirs[@]}"; do
sudo tar -Pczf /tmp/$i-$backup_date.tar.gz $i
if [ $? -eq 0 ]; then
echo "$i резервное копирование прошло успешно."
else
echo "$i ошибка резервного копирования."
fi
scp /tmp/$i-$backup_date.tar.gz $dest_server:$dest_dir
if [ $? -eq 0 ]; then
echo "$i передача прошла успешно."
else
echo "$i передача не удалось."
fi
done

sudo rm /tmp/*.gzecho "Резервное копирование сделано."

So, you first created an array called backup_dirs that contains all the directory names we want to keep. Then you created three other variables:

  • dest_dir: to specify the backup destination directory.
  • dest_server: To specify the destination server for the backup.
  • backup_time: Specify the backup date.

Next, for all directories in the backup_dirs array, create a GZIP compressed tar archive in / tmp, and then use the cement command to send / copy the backup to the destination server. Finally, remove all gzip archives from / tmp.

Here’s an example of running the backup.sh script:

[email protected]:~$ ./backup.sh
Запуск резервного копирования: /etc /home /boot
/etc резервное копирование прошло успешно.
etc-Aug-30-20.tar.gz 100% 1288KB 460.1KB/s   00:02
/etc передача прошла успешно.
/home резервное копирование прошло успешно.
home-Aug-30-20.tar.gz 100% 2543KB 547.0KB/s   00:04
/home передача прошла успешно.
/boot резервное копирование прошло успешно.
boot-Aug-30-20.tar.gz 100%  105MB 520.2KB/s   03:26
/boot передача прошла успешно.
Резервное копирование сделано.

You can run backups every day at midnight. In this case, you can schedule the script to run as a cron job:

[email protected]:~$ crontab -e
0	0	*	*	* /home/destroyer/scripts/backup.sh

Monitoring available disk space

Filesystems are destined to run out of space, the only thing you can do is act quickly before your system crashes! You can use the df command to see the remaining space on any filesystem:

[email protected]:~$ df -h / /apps /database
Filesystem Size  Used Avail Use% Mounted on
/dev/sda5 20G  7.9G   11G  44% /
/dev/mapper/vg1-applv 4.9G  2.4G  2.3G  52% /apps
/dev/mapper/vg1-dblv 4.9G  4.5G  180M  97% /database

There is almost no space left in our filesystem / database as it is currently 97% in use. We can only display the usage if we use the awk command to display only the fifth field.

Now take a look at the following bash script disk_space.sh:

#!/bin/bash
filesystems=("https://andreyex.ru/" "/apps" "/database")
for i in ${filesystems[@]}; do
usage=$(df -h $i | tail -n 1 | awk '{print $5}' | cut -d % -f1)
if [ $usage -ge 90 ]; then
alert="Заканчивается свободное место на $i, Usage is: $usage%"
echo "Отправка оповещения о дисковом пространстве по электронной почте."
echo $alert | mail -s "$i is $usage% full" your_email
fi
done

First, you created file system arrays that contain all the file systems that you want to keep track of. Then, for each filesystem, you take a percentage of usage and check if it is greater than or equal to 90. If the usage is over 90%, it sends an email warning that the filesystem is running out of space.

Please note that you need to replace your_email in the script with your real email address.

We ran the script:

[email protected]:~$ ./disk_space.sh
Отправка оповещения о дисковом пространстве по электронной почте.

You can run the disk_space.sh script in about six hours. In this case, you can also schedule the script to run as a cron job:

[email protected]:~$ crontab -e
0	*/6 *		*		* /home/destroyer/scripts/disk_space.sh

All! This brings us to the end of our bash primer series. Hope you enjoyed learning bash scripting. With bash scripts in your arsenal of skills, you can automate any boring tedious task on Linux!

Sidebar