Repository navigation
Expand file tree
/
Copy pathrsync-host
More file actions
executable file
·146 lines (132 loc) · 3.6 KB
/
Copy pathrsync-host
File metadata and controls
executable file
·146 lines (132 loc) · 3.6 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
##
## Calculates a path relative to the home directory. That path is used to Sync
## folders with a remote host.
##
## Home directories may differ, but the path relative to the home directory
## is the same. Under this configuration, it becomes simple to create and
## use the sync commands based on the current working directory.
##
## Unless you force it, this script will always preview and prompt before
## actually making changes.
##
## Example:
## /home/doej/projects/examples$ rscp host_alias (get|put)
## OR
## /home/doej/projects/examples$ rscp host_alias (get|put) [path/]file
##
## Sync ~/projects/examples from host_alias account with
## current directory (also ~/projects/examples).
##
## host_alias can be some short made-up alias; interactivly prompt and record
## the host and user.
##
## get - sync from server to local directory
## put - sync from local directory to server
##
## -f, force - skip the the dry-run and 'are you sure'
## -e, echo - just echo the command and exit
## -d, delete - delete extraneous files from dest dir
##
## Suffix commands to the main command: get-f, get-e, get-fed
#############
#Environment
#
alias=$1
cmd=$2
path=$3
. $(dirname $0)/prj-env $alias,user,host
user_host=$user${user:+@}$host
if test $# -lt 2 || test ! $(echo $cmd | egrep "get|put")
then
# Show usage above
sh $(dirname $0)/prj-usage $0
exit 1 #1=error
fi
IFS='-' read -ra ARRAY <<< "$cmd"
cmd=${ARRAY[0]} # get-fc becomes just "get"
parms=${ARRAY[1]}
echo $parms | grep "f" > /dev/null && force=
echo $parms | grep "e" > /dev/null && echo=
echo $parms | grep "d" > /dev/null && rsync_parms="$rsync_parms --delete"
if [[ $path == /* ]]
then
# User is providing a more specific path and does not
# want to the relative home dir path.
specific_path=
else
#Example: /home/jcalfee/bin becomes ~/bin
rsync_path=`pwd|sed "s?$HOME?~?g"`
fi
test -f .rsync_includes &&
rsync_parms="$rsync_parms --include-from=.rsync_includes"
test -f .rsync_excludes &&
rsync_parms="$rsync_parms --exclude-from=.rsync_excludes"
test -f .rsync_filters &&
rsync_parms="$rsync_parms --filter='merge .rsync_filters'"
#############
function rsync_run {
rsync_cmd=$1
if test -n "${echo+x}"
then
if test -n "${force+x}"
then
echo rsync $rsync_cmd
else
echo rsync $rsync_cmd --dry-run
fi
return
fi
tmp=`mktemp /tmp/rsync.XXXXXXXX`
trap 'rm $tmp' EXIT
if test -n "${force+x}"
then
echo rsync $rsync_cmd > $tmp
sh $tmp
else
echo rsync $rsync_cmd --dry-run | tee $tmp
sh $tmp
read -p "Proceed (y/n)? " yn
case $yn in
[Yy] )
echo rsync $rsync_cmd > $tmp
sh $tmp
;;
[Nn] ) return 1;;
* ) echo "Please answer y or n.";;
esac
fi
}
#########
# Main
#
case "$cmd" in
get*)
echo DOWNLOAD...
if test -z ${specific_path+x}
then
rsync_run "$rsync_parms --compress -e ssh -va \"$user_host${user_host:+:}$rsync_path/$path\" \"${path:-.}\""
else
rsync_run "$rsync_parms --compress -e ssh -va \"$user_host${user_host:+:}$path\" ."
fi
;;
put*)
if test -n "${echo+x}"
then
echo ${host:+ssh $user_host} mkdir -vp "$rsync_path"
else
# Do not mkdir if a specific path was provided
if test -n ${specific_path+x}
then
${host:+ssh $user_host} mkdir -vp "$rsync_path"
fi
fi
echo UPLOAD...
if test -z ${specific_path+x}
then
rsync_run "$rsync_parms --compress -e ssh -va \"${path:-.}\" \"$user_host${user_host:+:}$rsync_path/$path\""
else
rsync_run "$rsync_parms --compress -e ssh -va . \"$user_host${user_host:+:}$path\""
fi
;;
esac