#!/bin/bash # sftpsync--Given a target directory on an sftp server, makes sure that # all new or modified files are uploaded to the remote system. Uses # a timestamp file ingeniously called .timestamp to keep track. setvar timestamp = "".timestamp"" setvar tempfile = ""/tmp/sftpsync."$$" setvar count = '0' trap "$(which rm) -f $tempfile" 0 1 15 # Zap tempfile on exit if test $Argc -eq 0 { echo "Usage: $0 user@host { remotedir }" >&2 exit 1 } setvar user = "$(echo $1 | cut -d@ -f1)" setvar server = "$(echo $1 | cut -d@ -f2)" if test $Argc -gt 1 { echo "cd $2" >> $tempfile } if test ! -f $timestamp { # If no timestamp file, upload all files. for filename in * { if test -f $filename { echo "put -P \"$filename\"" >> $tempfile setvar count = $(( $count + 1 )) } } } else { for filename in $(find . -newer $timestamp -type f -print) { echo "put -P \"$filename\"" >> $tempfile setvar count = $(( $count + 1 )) } } if test $count -eq 0 { echo "$0: No files require uploading to $server" >&2 exit 1 } echo "quit" >> $tempfile echo "Synchronizing: Found $count files in local folder to upload." if ! sftp -b $tempfile "$user@$server" { echo "Done. All files synchronized up with $server" touch $timestamp } exit 0