- Joined
- Jan 24, 2006
- Location
- South Dakota
HowTo: Dealing with SFTP (push-pull bash scripts)
Well I took some time to deal with a Windows 2k8 SFTP server with linux/unix clients and I ended up writing a couple of scripts to help syncing the two.
Keep in mind that dealing with Windows as the sftp server forced me to use some less than conventional scripting methods. Anyways here they are incase they are of use to anyone (or even the Overclockix distro... who knows?)
Currently they are set to syncing xml files. As usual you will have to adjust the variables to suit your environment
The Pull Script
The Push Script
NOTE the push script expects to be run within the folder that you want to sync. If you are running it in a crontab you will have to adjust the 'ls' command to reflect your proper path
Well I took some time to deal with a Windows 2k8 SFTP server with linux/unix clients and I ended up writing a couple of scripts to help syncing the two.
Keep in mind that dealing with Windows as the sftp server forced me to use some less than conventional scripting methods. Anyways here they are incase they are of use to anyone (or even the Overclockix distro... who knows?)
Currently they are set to syncing xml files. As usual you will have to adjust the variables to suit your environment
The Pull Script
Code:
#!/bin/bash
#Sets the IP address of the ftp server
SERVER=192.168.56.50
#this grabs the list of files available on the server
FILE_LIST=`sftp $SERVER << EOF
cd sftp
ls
quit
EOF`
echo $FILE_LIST > temp_file_list
#this is used to remove everything except xml files
for DOWNLOAD_FILE in `cat temp_file_list |sort`
do
echo $DOWNLOAD_FILE |grep .xml >> download
done
#ensure that the files are sorted properly
sort download > download_list
#check to see if each file in the server's list exists
for SERVER_FILE in `cat download_list`
do
if [ -f "$SERVER_FILE" ];
then
#if the file exists on the local machine
echo $SERVER_FILE Exists on local machine
else
#if the file does not exist, grab it
sftp $SERVER << EOF
get sftp/$SERVER_FILE
exit
EOF
fi
done
#remove the temporary file lists
rm temp_file_list
rm download
rm download_list
The Push Script
Code:
#!/bin/bash
#Sets the IP address of the ftp server
SERVER=192.168.56.50
FOLDER=/root/sftp
#this grabs the list of files available on the server
FILE_LIST=`sftp $SERVER << EOF
cd sftp
ls
quit
EOF`
echo $FILE_LIST > temp_file_list
#this is used to remove everything except xml files
for DOWNLOAD_FILE in `cat temp_file_list`
do
echo $DOWNLOAD_FILE |grep .xml >> download
done
#ensure that the files are sorted properly
sort download > download_list
#get the local list
ls |grep .xml | sort > local_list
# checks to see which files are on the server that are not
# on the local machine
DIFF=`diff local_list download_list | cut -c3- |grep .xml`
#check to see if each file in the server's list exists on the local maching
if [ `echo $DIFF` =="" ]
# If there are no differences in the folder assume there is nothing
# new to transfer
then
echo No New Files to transfer
else
for SERVER_FILE in $DIFF
do
#if the file does not exist, grab it
sftp $SERVER << EOF
cd sftp
put $SERVER_FILE
exit
fi 2>/dev/null
#remove the temporary file lists
rm local_list
rm temp_file_list
rm download
rm download_list
NOTE the push script expects to be run within the folder that you want to sync. If you are running it in a crontab you will have to adjust the 'ls' command to reflect your proper path
Last edited: