#!/bin/bash
# Based on a script by Adam Neat, modified in 2003 by Matthew Durnford
# Comments, modifications, improvements to matthew@rosemounttechnology.com
# This script comes with no warranty, use it at your own risk.
# The script that does the work of cleaning the temporary directory, copying
# the data, renaming the directories.
# The variables are $1=computer name, $2=service name, $3=backup directory
# $4=smbclient config file, $5=username%password.
cd ~admin/backup
logfile="/home/admin/logs/backup.log"
echo "------> win_backup initialising for $1 $2" >> "$logfile"
if [ ! -f $4 ]; then
# echo "$4 backup config file exists" >> "$logfile"
#else
echo "FATAL ERROR: No backup config file for today! .." >> "$logfile"
echo "------> win_backup for $1 $2 aborting" >> "$logfile"
exit 1
fi
if [ "$1" = "" ]; then
echo "error in a computer name " >> "$logfile"
exit 1
fi
if [ "$2" = "" ]; then
echo "error in service name" >> "$logfile"
exit 1
fi
if [ "$3" = "" ]; then
echo "error in backup directory on server name" >> "$logfile"
exit 1
fi
#############################################################################
# Main Section
#
#############################################################################
if [ ! -d ~admin/backup/temp ]; then
mkdir -m 760 ~admin/backup/temp
fi
#clean out temp directory before start
cd ~admin/backup/temp
rm -rf *
echo "copying files from $1"
cat $4 | /usr/bin/smbclient //$1/$2 -U $5 || { echo "FATAL ERROR : No connection to $1 $2 - check network and if computer working. Also user name, password and sharing on service $2 " >> "$logfile" ; exit 1; }
if [ ! -d ~admin/backup/current/$3/ ]; then
mkdir -p ~admin/backup/current/$3/
fi
if [ ! -d ~admin/backup/old/$3/ ]; then
mkdir -p ~admin/backup/old/$3/
fi
#clean out old directory
cd ~admin/backup/old/$3/
rm -rf *
#copy files from current directory to old directory
touch "/home/admin/backup/current/$3/current_to_old-$(date +%Y%m%d_%T)"
mv ~admin/backup/current/$3/* ~admin/backup/old/$3/
#clean out the current directory
cd ~admin/backup/current/$3/
rm -rf *
#copy files from temp directory to current directory
cd ~admin/backup/temp
touch "/home/admin/backup/temp/temp_to_current-$(date +%Y%m%d_%T)"
mv ~admin/backup/temp/* ~admin/backup/current/$3/
#clean out the temp directory
cd ~admin/backup/temp
rm -rf *
echo "------> backup of $1 $2 completed" >> "$logfile"