I am trying to transfer files to a remote server using SFTP in a bash
script.
I have ran into a problem though.
Part of the script cd to different directories on the remote server
but if it cd to a
directory that doesn't exist then the entire script gets terminated.
I know that I can
use scp which I am already doing but I also want to be able to use SFTP.
Here is my script:
Code:
#################### Begin Local Settings ####################
##### Backup Script Settings Begin #####
homeDIR="/home/xxxxx"
BUScriptLocation="/backupScript"
BULogName="backupLog"
##### Backup Script Settings End #####
##### Logs Backup Settings Begin #####
BUFolder3="/var/log/httpd" # Leave blank to not backup
logsBackupLocation="backupFolder"
encryptLogs="no" # yes or no
##### Logs Backup Settings End #####
#################### End Local Settings ####################
#################### Begin Remote Settings ####################
transferFiles="yes" # yes or no
transferMethod="sftp" # scp, sftp, or ftp
remoteUsername="xxxxxxxxxxxx" # UserName for remote Server
remoteServer="xxxxxxxx.xxxxxx" # Remote Server
remoteFolder="backups" # Remote Folder to save backups to
miscSetting1="/remote/home" # Misc Settings
tempcmds="tempcmdsFile" # File for temp. cmds when using SFTP or FTP
#################### End Remote Settings ####################
myDate=`date +%Y_%a`
dayOfWeek=`date +%w` # 0-6 0 is Sunday
weekDay=`date +%a` # Mon, Tues, Wed, etc...
month=`date +%b` # Jan, Feb, Mar, etc...
day=`date +%d` # 01-31
year=`date +%Y` # 2007
time=`date +%H:%M:%S` # Hour:Minute:Second
time2=`date +%T` # Hour:Minute:Second
time3=`date +%X` # Hour:Minute:Second
timeZone=`date +%Z` # UTC, CST, CDT, etc..
backupDate=`date +%Y-%b-%d-%X-%Z`
#perl $homeDIR/$BUScriptLocation/backup.script $homeDIR/ $logsBackupLocation/ $BUFolder3
if [ "$transferMethod" = "sftp" ];
then
echo "cd $remoteFolder" >> $homeDIR/$tempcmds
echo "mkdir $month" >> $homeDIR/$tempcmds
echo "cd $month" >> $homeDIR/$tempcmds
echo "mkdir $day" >> $homeDIR/$tempcmds
echo "cd $day" >> $homeDIR/$tempcmds
echo "mkdir Logs" >> $homeDIR/$tempcmds
# echo "put $homeDIR/$logsBackupLocation/* $remoteUsername@
$remoteServer:$remoteFolder/$month/$day/Logs" >> $homeDIR/$tempcmds
echo "quit" >> $homeDIR/$tempcmds
sftp -b $homeDIR/$tempcmds $remoteUsername@$remoteServer
# rm -f $homeDIR/$tempcmds
fi
Well I figured out how to keep the bash script from quiting when errors come from sftp using this:
Code:
sftp $remoteUsername@$remoteServer <<**
if [ -d /usr/home3/6134248/backups_business/script/Dec ];
then
cd /usr/home3/6134248/backups_business/script/Dec
else
mkdir /usr/home3/6134248/backups_business/script/Dec
cd /usr/home3/6134248/backups_business/script/Dec
fi
if [ -d /usr/home3/6134248/backups_business/script/Dec/testing ];
then
cd /usr/home3/6134248/backups_business/script/Dec/testing
else
mkdir /usr/home3/6134248/backups_business/script/Dec/testing
cd /usr/home3/6134248/backups_business/script/Dec/testing
fi
put $homeDIR/$logsBackupLocation/folder_files/*.*
exit
**
But now when I run the script I run into the problem where the if, then, else, fi are not
being honored and it is saying Invalid Command.
I am going off of the an example I found here:
CrunchBang ~ Bash Script: MySQL Backup
Any suggestions?