-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_folder_copy.sh
executable file
·119 lines (109 loc) · 2.8 KB
/
auto_folder_copy.sh
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
function store_vars(){
varsfile=/tmp/${0}_vars
[[ -f ${varsfile} ]] && rm ${varsfile}
for svar in wf_server wf_dest watchfolder
do
printf "%s=%s\n" "${svar}" "${!svar}" >> ${varsfile}
done
}
function recall_vars(){
varsfile=/tmp/${0}_vars
[[ -f ${varsfile} ]] && source ${varsfile}
}
function a_usage(){
cat <<EOF
Usage: autoscp2 [-x <n>]
-x redefine a script variable, where
1) watch folder
2) target server
3) destination folder
4) everything
EOF
}
function getargs() {
#log "Switch: ${o} ${OPTARG}"
unset wf_run
while getopts ":vhxV" o; do
case ${o} in
x)
case ${OPTARG} in
1)
unset watchfolder
;;
2)
unset wf_server
;;
3)
unset wf_dest
;;
4)
unset watchfolder
unset wf_server
unset wf_dest
;;
esac
;;
*)
a_usage
wf_run=y
;;
esac
done
}
function autoscp2() {
# utility script that copies changed files to a chosen destination until ctrl-c is pressed
recall_vars
unset i2
if [[ -z ${wf_run} ]]
then
[[ -z ${wf_server} ]] && printf "Server :" && read wf_server
if [[ -z ${watchfolder} ]]
then
printf "Folder:"
read wf_tmp
watchfolder=${wf_tmp}
else
printf "(Current Folder: %s) : " "${watchfolder}"
read wf_tmp
[[ ! -z $wf_tmp ]] && watchfolder=${wf_tmp}
fi
[[ -z ${wf_dest} ]] && printf "Remote destination :" && read wf_dest
[[ -z ${wf_dest} ]] && wf_dest=.
[[ -z ${autouser} ]] && autouser=schsup
printf "Using var [autouser] -> %s. Use -x to reset\n%s\n" ${autouser} "watch ${watchfolder} ${autouser}@${wf_server}:${wf_dest}"
while [ 0 ]
do
for tfile in "${watchfolder%\/}/"*
do
fname="${tfile//*\/}"
statname="/tmp/${fname}.stat"
# save current inode to a file and assign to i1
i1=$(stat --printf "%Y" ${tfile})
echo $i1 > "${statname}"
# check if an 'old inode' file is around and assign the value to i2
[[ -e "${statname}2" ]] && i2=$(cat "${statname}2")
if [[ $i1 -ne $i2 ]]; then
# inode are not the same; copy file to destination; copy i1 to i2
i2=$i1;
echo $i2 > "${statname}2"
printf "The file %s has changed\n" "${fname}"
# if the path ends with modules, save as .fn
if [[ ${tfile%\/*} =~ /modules$ ]] ; then
fn_name="${fname%\.*}"
destname="${fn_name}.fn"
printf "%s is a module. Renaming to %s\n" "${fn_name:u}" "${destname:u}"
else
destname="${fname}"
fi
if [[ ! "${wf_dest}" = "." ]]; then
destname="${wf_dest%\/}/${destname}"
fi
scp "{tfile}" "${autouser}@${wf_server}:${destname}"
fi
done
sleep 2
done
fi
}
getargs
autoscp2