#!/bin/bash                                                    

# Title: host_backup.bash
# By: john@stilen.com    
# Purpose: Copy myhost to file server for nearline backup
#                         
# Procedure:              
# Run once per night, via crontab:
#   1 2 * * * /root/bin/backup_disk_myhost.bash &>  /amandatapes/myhost/backup_log.txt
# Copy all of / to /amandatapes/myhost/                                        
# Then make an rrdtool graph

echo "#################################################"
echo "# Variables:"                                     
BackupHost="myhost"                                       
BackupDir="/amandatapes/$BackupHost"                    
Email="fu@stilen.com,bar@stilen.com"
Start=`date +%H:%M:%S`                                  
Date=`date +%Y%m%d`                                     
Day=`date +%d`                                          
SIZE=''                                                 
RRDtool="/usr/bin/rrdtool"                              
echo "BackupHost:$BackupHost"                           
echo "BackupDir: $BackupDir"                            
echo "Start:     $Start"                                
echo "Date:      $Date"                                 
echo "Day:       $Day"                                  

echo "#################################################"
echo "[--] Mount backup drive /amandatapes"             
#                                                       
mount /amandatapes                                      
if [[ $? != 0 ]]                                        
then                                                    
  echo "[!!] failed."                                   
  #  exit 1                                             
else                                                    
  echo "[OK] success."                                  
fi                                                      
echo "#################################################"
echo "[--] Make directory if it doesn't exist: $BackupDir"
if [ ! -d $BackupDir ]                                    
then                                                      
  mkdir $BackupDir                                        
  if [[ $? != 0 ]]                                        
  then                                                    
    echo "[!!] failed."                                   
    #  exit 1                                             
    else                                                  
    echo "[OK] success."                                  
  fi                                                      
fi                                                        
echo "#################################################"  
echo "[--] Destory any iso's over 3 days old"             
Files=`find $BackupDir -maxdepth 1 -name "${BackupHost}_*" -ctime +4 -print`
echo "Found:"                                                               
for File in $Files; do                                                      
  echo "Removing: $File"                                                    
  rm -rf $File                                                              
done                                                                        
if [[ $? != 0 ]]                                                            
then                                                                        
  echo "[!!] failed."                                                       
  #  exit 1                                                                 
else                                                                        
  echo "[OK] success."                                                      
fi                                                                          
echo "#################################################"                    
echo "[--] Copy root@${BackupHost}:/ to here: ${BackupDir}/${BackupHost}_${Date}"
time rsync -e ssh \                                                              
  -azuRh \                                                                       
  --exclude '/mnt/*' \                                                           
  --exclude '/sys/*' \                                                           
  --exclude '/proc/*' \                                                          
  --exclude '/media/*' \                                                         
  root@${BackupHost}:/ \                                                         
  ${BackupDir}/${BackupHost}_${Date}                                             
if [[ $? != 0 ]]                                                                 
then                                                                             
  echo "[!!] Rsync / failed."                                                    
  #exit 1                                                                        
else                                                                             
  echo "[OK] success."                                                           
fi                                                                               
End=`date +%H:%M:%S`                                                             
echo "#################################################"
echo "[--] Size of backup."
Size=`du -s ${BackupDir}/${BackupHost}_${Date}|awk  '{print $1}'`
echo "[OK] The size of backup is: $Size"
echo "#################################################"
echo "[--] Send EMAIL report."
cat $BackupDir/backup_log.txt |mailx -s "Backup $BackupHost:$Date from $Start to $End"  "$Email"
if [[ $? != 0 ]]
then
  echo "[!!] failed."
  #  exit 1
else
  echo "[OK] success."
fi
echo "#################################################"
echo "[--] Add size of backup to rrd database ${RRD_File}"
RRD_File="${BackupDir}/backup_size.rrd"
if [ ! -f ${RRD_File} ]; then
  # Create rrd database
  # starting date = yesterday
  # Stepping 86400 seconds (24 hours)
  # Data Store named='size', type=GUAGE, and
  # if it isn't updated every  87000 seconds (a little over 24 hours), the recorded value will be 'unknown'.
  # With "XFiles Factor"=0.5, only half the members of each RRA need to be present, or value will be 'unknown'.
  # The first RRA uses one PDP (primary data point) and stores 365 records (1 year).
  # The second RRA uses 30 PDP, and stores 60 records (5 years).
  ${RRDtool} create \
    "${RRD_File}" \
    --start N-86400 \
    --step 86400 \
    "DS:size:GAUGE:89000:U:U" \
    "RRA:LAST:0.5:1:365" \
    "RRA:LAST:0.5:30:60"
fi
${RRDtool} update "${RRD_File}" "N:${Size}"
echo "#################################################"


