Code:
root@Washington:~/bin # bash -version
GNU bash, version 4.3.42(0)-release (amd64-portbld-freebsd10.1)
Copyright.........
Code:
root@Washington:~/bin # uname -K
1001000
The code runs every night without question, the ONLY problem I have is that for some reason the mail won't send.
When I run the line in question (highlighted below) with the variable filled in it runs great. This is after I added the line to
Code:
chmod 774 $lf
This is what I run in shell:
Code:
mail -s "Snapshot for XX on Box X" [my email] < /var/log/SNAPSHOTS/[log.file]
I have the entire code below.
Any help would be much appreciated.
PS: I am not a very good coder. I'm sure it's hard to read, but it works. Go easy on my
Code:
root@Washington:~/bin # cat snapshot.sh
#!/usr/local/bin/bash
seed="SNAPSHOTS" # Start of it all
bd=/var/$seed # Backup Dir - Where the files go
perform_backups="YES" # Turns on Backup feature "(YES|NO)"
uid="liberty.roofing" # Username SCP uses to log in
rdir="/usr/home/$uid/$seed" # remote Dir
email="tim.falardeau@gmail.com" # Where emails go
email_log="YES" # Email log feature "(YES|NO)"
rsys="192.168.1.20" # System to store files on
rsa="/$uid/.ssh/id_rsa.pub" # rsa key location
lcopies=7 # number of local copies to keep
bcopies=30 # number of copies to keep on backup machine
##############################################
# DO NOT BACKUP DIR YOUR BACKUPS ARE IN !!!! #
##############################################
declare -a files=(/etc/passwd \
/etc/master.passwd \
/root/bin \
/usr/local/etc \
/etc/rc.conf \
/etc/hosts \
/etc/resolv.conf \
/usr/share/skel \
/var/QB \
/var/LibertyRoof)
###############################################################################
numf=${#files[@]}
date=`date +%m-%d-%y`
time=`date '+TIME: %H:%M:%S'`
lf=$bd/snap_$date.log
slf=$bd/snap.log
box=`hostname -s`
tmp=$box"_"$date
tarf="snap_$tmp.tar"
zipf=$bd/$tarf.gz
rzipf=$tarf.gz
filename="$rzipf"
###### BEGIN #######
# Email log file if enabled
sys.summary ()
{
totaldu="$(df -H)"
usrdu="$(du -hs /usr | tail -n1 | cut -f1)"
lrdu="$(du -hs /var/LibertyRoof | tail -n1 | cut -f1)"
sndu="$(du -hs /var/$seed | tail -n1 | cut -f1)"
echo "============================ System Summary ==========================" >> $lf
echo "$totaldu" >> $lf
echo " " >> $lf
echo "Disk Usage broken down by major idex" >> $lf
echo "------------------------------------" >> $lf
echo "/usr uses $usrdu of disk space" >> $lf
echo "/LibertyRoof uses $lrdu of disk space" >> $lf
echo "/var/$seed uses $sndu of disk space" >> $lf
echo "------------------------------------" >> $lf
echo "Your local dir $bd has $numlc snapshots stored" >> $lf
echo "Your backup system located at $rsys has $numbc stored" >> $lf
echo "======================================================================" >> $lf
}
email.log ()
{
if [[ "$email_log" = "YES" ]]; then
chmod 774 $lf
mail -s "Snapshot for $date on $box" $email < $lf
echo "logfile emailed to $email" >> $slf
else
echo "### EMAILING OF LOG TURNED OFF ###" >> $slf
fi
}
# Close out log and quit gracefully
quit ()
{
cat $lf >> $slf
sys.summary
email.log
rm $lf
exit
}
# test for and set up Backup Dir
if [ ! -d $bd ]; then
mkdir $bd
chmod -R 770 $bd
chown root:$uid $bd
echo "--- File created $date $time ---" >> $lf
touch $bd/starter.file
(tar -pcf $bd/$tarf $bd/starter.file) &>> $lf
rm $bd/starter.file
fi
echo "================================================================" >> $lf
echo " Snapshot log entry for $date on machine $box " >> $lf
echo "================================================================" >> $lf
# if Directory already has a backup for the day
# rename it. If it already has an ORG file, delete it
if [ -e $zipf ]; then
if [ -e $zipf.ORG ]; then
echo "$filename.ORG already exists" >> $lf
echo "$filename deleted to make room for a new one" >> $lf
rm $zipf.ORG
fi
echo "$filename already exists for date:$date" >> $lf
echo "$filename renamed $filename.ORG" >> $lf
mv $zipf $zipf.ORG
fi
echo "Snap shot process started at: $time" >> $lf
echo "Adding files to Tar Backup ($tarf)" >> $lf
# Write array to tape archiver utility
# and preserve permissions
for (( c=0; c<$numf; c++))
do
wkg_file=${files[$c]}
if [ -e "$wkg_file" ]; then
(tar -prf $bd/$tarf $wkg_file) 2>/dev/null
if [ $? -eq "0" ]; then
echo "$wkg_file ---> Successful" >> $lf
else
echo "$wkg_file ---> FAILED" >> $lf
fi
fi
done
# Gzip
echo "Compressing Snapshot to $filename" >> $lf
gzip -fq $bd/$tarf
if [[ $? -eq 0 ]]; then
echo "Compression ---> Succesful" >> $lf
chown root:$uid $zipf
chmod 770 $zipf
if [[ ! -z "$(ls $bd | grep ORG)" ]]; then
echo "removing obsolete .ORG files" >> $lf
rm $bd/*.ORG 2>> $lf
fi
else
echo "Compression ---> FAILED" >> $lf
echo "Retaining .ORG files" >> $lf
echo "Backup NOT Attempted !!" >> $lf
quit
fi
# Clean and Maintain Local Backup Dir
numlc="$(ls $bd | grep snap | grep -v log | wc -l)"
if [[ "$numlc" -gt "$lcopies" ]]; then
oldfile="$(ls -tr $bd | grep -v log | head -n1)"
echo "local copies of backups exceeds $lcopies" >> $lf
echo "removing $oldfile to maintain sanity" >> $lf
rm $bd/$oldfile
fi
# Perform Backups to local machine
if [[ "$perform_backups" == "NO" ]]; then
echo "### Uploads Disabled ###" >> $lf
echo "============================================" >> $lf
quit
fi
echo "beginning upload of $filename to $rsys" >> $lf
# Check and/or install remote dir for upload
dirchk="$(su $uid -c "ssh $rsys ls /usr/home/$uid | grep $seed")" 2>> $lf
if [[ -z "$dirchk" ]]; then
su $uid -c "ssh $rsys mkdir $seed" 2>> $lf
if [[ $? -ne 0 ]]; then
echo "couldn't create remote dir $seed...exiting" 2>> $lf
quit
fi
su $uid -c "ssh $rsys chmod 770 $seed" 2>> $lf
fi
file="$(su $uid -c "ssh $rsys ls /usr/home/$uid/$seed | grep $filename")"
if [[ ! -z "$file" ]]; then
echo "Remote dir already contains $filename" >> $lf
su $uid -c "ssh $rsys mv $rdir/$filename $rdir/$filename.ORG"
echo "renamed $filename to $filename.ORG" >> $lf
fi
su $uid -c "scp $zipf $uid@$rsys:$rdir/$rzipf" 2>> $lf
if [[ $? -ne 0 ]]; then
echo "Upload ---> FAILED" >> $lf
quit
else
echo "Upload ---> Succesful" >> $lf
if [[ ! -z "$(su $uid -c "ssh $rsys ls $rdir | grep ORG")" ]]; then
echo "removing obsolete .ORG files" >> $lf
su $uid -c "ssh $rsys rm $rdir/*.ORG" 2>> $lf
fi
fi
# Cleaning up Backup Server to maintain sanity
numbc="$(su $uid -c "ssh $rsys ls $rdir | grep snap | grep -v log | wc -l")"
if [[ "$numbc" -gt "$bcopies" ]]; then
oldfile="$(su $uid -c "ssh $rsys ls -Ur $rdir | grep -v log | head -n1")"
echo "number of backup copies exceeds $bcopies" >> $lf
echo "removing $oldfile to maintain sanity" >> $lf
su $uid -c "ssh $rsys rm $rdir/$oldfile" 2>> $lf
echo "=========================== EOF ============================" >> $lf
fi
quit