Shell Script - Intermediate

Backup Script

  1. Crete Backup directory:
    mkdir /backups

  2. Create a Backup script
    vi backupscript.sh

#!/bin/bash

#Directory of which you need to take
backup src_dir=/home/user1/scripts

#Directory where the backup will be placed.
tgt_dir=/backups

#Storing the Date in a variable
curr_timestamp=$(date "+%Y-%m-%d-%H-%M-%S")

#Defining the Backup path and filename (Where the backup will be stored) backup_file=$tgt_dir/$curr_timestamp.tgz
echo "Taking Backup on:- $curr_timestamp"
#echo "$backup_file"

tar -czf $backup_file --absolute-name $src_dir
echo "Backup Complete"

Disk Check script

#!/bin/bash
df -H | awk {'print $1 "" "" $5 '} | while read output;
do
#echo "Disk details: $output"
usage=$(echo $output | awk '{print $1}'| cud -d'%' -f1)
file_sys=$(echo $output | awk '{print $2}')
#echo $usage
if [ $usage -ge 90 ]
then
echo "CRITICAL for $file_sys"
fi
done

Or

#!/bin/bash
alert=90
df -H | awk {'print $1 "" "" $5 '} | while read output;
do
#echo "Disk details: $output"
usage=$(echo $output | awk '{print $1}'| cud -d'%' -f1)
file_sys=$(echo $output | awk '{print $2}')
#echo $usage
if [ $usage -ge $alert ]
then
echo "CRITICAL for $file_sys"
fi
done