-
Notifications
You must be signed in to change notification settings - Fork 0
/
72a-sftpsync
55 lines (44 loc) · 1.22 KB
/
72a-sftpsync
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/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.
timestamp=".timestamp"
tempfile="/tmp/sftpsync.$$"
count=0
trap "`which rm` -f $tempfile" 0 1 15 # Zap tempfile on exit
if [ $# -eq 0 ] ; then
echo "Usage: $0 user@host { remotedir }" >&2
exit 1
fi
user="$(echo $1 | cut -d@ -f1)"
server="$(echo $1 | cut -d@ -f2)"
if [ $# -gt 1 ] ; then
echo "cd $2" >> $tempfile
fi
if [ ! -f $timestamp ] ; then
# If no timestamp file, upload all files.
for filename in *
do
if [ -f "$filename" ] ; then
echo "put -P \"$filename\"" >> $tempfile
count=$(( $count + 1 ))
fi
done
else
for filename in $(find . -newer $timestamp -type f -print)
do
echo "put -P \"$filename\"" >> $tempfile
count=$(( $count + 1 ))
done
fi
if [ $count -eq 0 ] ; then
echo "$0: No files require uploading to $server" >&2
exit 1
fi
echo "quit" >> $tempfile
echo "Synchronizing: Found $count files in local folder to upload."
if ! sftp -b $tempfile "$user@$server" ; then
echo "Done. All files synchronized up with $server"
touch $timestamp
fi
exit 0