Skip to content

Commit 6dbcde0

Browse files
Ubuntuoztimpower
Ubuntu
authored andcommitted
Initial commit
1 parent 2b599ef commit 6dbcde0

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DEST_PATH=
2+
DEST_USER=
3+
DEST_HOST=
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/bin/sh
2+
3+
# source env
4+
APP_HOME=`dirname $0`/..
5+
. $APP_HOME/etc/push.conf
6+
7+
# private key
8+
KEY=$APP_HOME/.ssh/transfer-key
9+
10+
# dest server
11+
SERVER=${DEST_USER}@${DEST_HOST}
12+
13+
# path to recordings
14+
BASE=/tmp/sd/record
15+
DAY=`date +%YY%mM%dD`
16+
HOUR=`date +%H`
17+
NOW=${DAY}${HOUR}H
18+
MIN=`date +%M`
19+
RECORD_PATH=$BASE/${DAY}${HOUR}H
20+
if [ $MIN -gt 50 ]; then
21+
# Each minute files are written, wait if at the end of the hour to capture all
22+
sleep $((61-$MIN))m
23+
fi
24+
25+
usage() {
26+
echo
27+
echo "Usage: $0 -f [filepath] -h -t [datepath]"
28+
echo "Where:"
29+
echo " -f = filename path, eg: -f /tmp/log.txt"
30+
echo " -h = publish all for the hour"
31+
echo " -t = target hour, eg: $NOW"
32+
echo
33+
exit 1
34+
}
35+
36+
if [ "_$1" = "_" ]; then
37+
usage
38+
fi
39+
40+
# parse options
41+
file=
42+
target_path=
43+
while getopts "f:t:h" o; do
44+
case "$o" in
45+
f) file="$OPTARG";;
46+
t) file="$BASE/$OPTARG"; target_path="$OPTARG/";;
47+
h) file=$RECORD_PATH; target_path=$NOW/;;
48+
[?]) usage
49+
esac
50+
done
51+
52+
53+
#########################
54+
# functions
55+
#########################
56+
57+
check_exists() {
58+
if [ ! -e $file ]; then
59+
echo
60+
echo "WARN: File [ $file ] does not exist"
61+
echo
62+
exit 1
63+
fi
64+
}
65+
66+
check_filesize() {
67+
local=`ls -l $1 | awk '{print $5}'`
68+
remote=`ssh -i $KEY $SERVER ls -l $DEST_PATH/$target_path/$2 2>/dev/null | awk '{print $5}'`
69+
70+
if [ "_$local" = "_$remote" ]; then
71+
return 0
72+
else
73+
return 1
74+
fi
75+
}
76+
77+
do_batch_scp() {
78+
rm -f /tmp/remote.* /tmp/local.*
79+
ssh -i $KEY $SERVER ls -l $DEST_PATH/$target_path/ 2>/dev/null | awk '{print $5,$9}' > /tmp/remote.$$
80+
if [ $? -ne 0 ]; then
81+
return
82+
fi
83+
ls -l $file | awk '{print $5,$9}' > /tmp/local.$$
84+
85+
for i in `cat /tmp/local.$$ | awk '{print $2}'`; do
86+
local_file=`cat /tmp/local.$$ | grep $i | awk '{print $1}'`
87+
remote_file=`cat /tmp/remote.$$ | grep $i | awk '{print $1}'`
88+
if [ "_$local_file" = "_$remote_file" ]; then
89+
echo "INFO: Skipping [ $i ] File exists on remote"
90+
else
91+
do_scp $file/$i 0
92+
fi
93+
done
94+
}
95+
96+
do_mkdir() {
97+
ssh -i $KEY $SERVER mkdir -p $DEST_PATH/$target_path
98+
}
99+
100+
do_scp() {
101+
if [ "_$2" = "_0" ]; then
102+
echo "INFO: Copying [ $1 ]"
103+
scp -i $KEY $1 $SERVER:$DEST_PATH/$target_path
104+
else
105+
filename=`echo $1 | sed "s/.*\///"`
106+
check_filesize $1 $filename
107+
if [ $? -ne 0 ]; then
108+
echo "INFO: Copying file [ $1 ]"
109+
scp -i $KEY $1 $SERVER:$DEST_PATH/$target_path
110+
else
111+
echo "INFO: Skipping [ $1 ] File exists on remote"
112+
fi
113+
fi
114+
}
115+
116+
# main function
117+
main() {
118+
check_exists $file
119+
if [ -d $file ]; then
120+
do_mkdir
121+
do_batch_scp
122+
else
123+
do_scp $file
124+
fi
125+
}
126+
127+
# run!
128+
main

0 commit comments

Comments
 (0)