#!/usr/bin/bash
#Go to the mount point and load an ls into a variable to then check if the mount point is "mounted"
#I could probably use df to check but i thought of this first
cd /mnt/d_e6600
fs_mounted=`ls`
#Checking to see if the place we want to save to is mounted.
if [[ $fs_mounted ]]; then
dumpFileNameHolder=("root.dump" "var.dump" "home.dump")
i=0
#CREATION OF THE DUMPS
###############################################################################################################################
#this line will get the date and whack it into the folder to save to
folder_to_save_to=`date | awk '{print $1"_"$2"_"$3"_"$6}'`
#make the folder for containing the dumps and cd to it
mkdir -p "/mnt/d_e6600/SIA_BACKUPS/$folder_to_save_to" && cd "/mnt/d_e6600/SIA_BACKUPS/$folder_to_save_to"
#Begin the loop of checking and creating each dump
for mount_point in "/" "/var" "/home" ; do
size_in_1k_blocks=`df | grep "$mount_point$" | awk '{print $2}'`
#we need to know what time each dump is made so we add the time the file is saved infront of the file name
time_file_saved=`date | awk '{print $4}' | sed s/:/_/g`
file_to_save_to="$time_file_saved.${dumpFileNameHolder[$i]}"
(( i++ ))
#Do a live file system dump for /, otherwise a normal dump for the rest
if [[ $mount_point = "/" ]]; then
dump 0fBL "$file_to_save_to" "$(( size_in_1k_blocks+100000 ))" "$mount_point"
else
dump 0fB "$file_to_save_to" "$(( size_in_1k_blocks+100000 ))" "$mount_point"
fi
#################################################################################
# else
# WRITE A SCRIPT HERE TO EMAIL ME AND TELL ME THAT THE DUMP DIDNT HAPPEN
#
#################################################################################
done
####################################################################################################################################
#REMOVAL OF OLD DUMP
####################################################################################################################################
#go to the main backups folder and check how many back ups exist, incase the folder needs to be cleaned
cd "/mnt/d_e6600/SIA_BACKUPS/"
number_of_folders=`ls -F | grep -c "/$"`
#Check if the backups folder needs to be cleaned by checking if more than 3 dump folders exst. I should add a bit to only look at folders containing dump files
while [ $number_of_folders -gt 3 ]; do
folder_to_remove=`ls -t | tail -1 | sed s,/,,g`
echo $folder_to_remove
rm -r "$folder_to_remove"
number_of_folders=`ls -F | grep -c "/$"`
done
fi
exit 0