-
Notifications
You must be signed in to change notification settings - Fork 2
/
cron_lftp.sh
executable file
·104 lines (90 loc) · 2.86 KB
/
cron_lftp.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
#!/bin/sh
set -e
# Chosen action
ACTION=$1
# Options
# Name
NAME=cron_lftp
# Binary: full path only
DAEMON=$PREFIX/bin/lftp
# Current directory
DIRECTORY="`pwd`"
# Check ini file is there
LFTPINI=./lftp.ini
if [ ! -f $LFTPINI ]; then
echo "No LFTP ini file found. Cancel."
exit 1
fi
# Check internet connection OK
wget -q --spider https://google.com
if [ ! $? -eq 0 ]; then
echo "No internet connection !"
exit 1
fi
# Get FTP options from the ini file
FTPACTIVE=$(sed -nr "/^\[lftp\]/ { :l /^active[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $LFTPINI)
FTPPROT=$(sed -nr "/^\[lftp\]/ { :l /^protocol[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $LFTPINI)
FTPHOST=$(sed -nr "/^\[lftp\]/ { :l /^host[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $LFTPINI)
FTPPORT=$(sed -nr "/^\[lftp\]/ { :l /^port[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $LFTPINI)
FTPUSER=$(sed -nr "/^\[lftp\]/ { :l /^user[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $LFTPINI)
FTPLOCA=$(sed -nr "/^\[lftp\]/ { :l /^local_dir[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $LFTPINI)
FTPREMO=$(sed -nr "/^\[lftp\]/ { :l /^remote_dir[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $LFTPINI)
REPEAT=$(sed -nr "/^\[lftp\]/ { :l /^repeat_minutes[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $LFTPINI)
# Check if sync is active
if [ "$FTPACTIVE" != "true" ]
then
echo "Action is not active. Cancel."
exit 0
fi
# Check if current minute of date corresponds to the repeat_minutes given in configuration
current_minute=$(date '+%-M')
REPEAT=$(($REPEAT + 0))
if [ $REPEAT -gt 0 ]
then
modulo="$(( current_minute % REPEAT ))"
modulo=$(($modulo + 0))
if [ $modulo -gt 0 ]
then
echo "Current minute does not correspond to given repeat minutes"
exit 0
fi
else
echo "Wrong value for repeat_minutes: $REPEAT"
fi
# Check media directory has changed
DIRCHANGED=false
DIRSTAT=`du --null -sc $FTPLOCA | sed 's/[^0-9]//g'`
STATFILE="$PREFIX/var/run/$NAME.stat"
if [ ! -f $STATFILE ]
then
DIRCHANGED=true
else
OLD_DIRSTAT=`cat $STATFILE`
if [ "$DIRSTAT" = "$OLD_DIRSTAT" ]
then
DIRCHANGED=false
else
DIRCHANGED=true
fi
fi
if [ "$DIRCHANGED" = false ] && [ "$ACTION" = "start" ]
then
echo "* No change detected in dir $FTPLOCA. Exit"
exit 0
else
echo "* Changes has been detected. Proceed"
fi
# LFTP options file
CONFIGFTP=".config-cron-lftp"
echo "set ssl:verify-certificate no" > $DIRECTORY/$CONFIGFTP
echo "set sftp:auto-confirm yes" >> $DIRECTORY/$CONFIGFTP
echo "set ftp:ssl-allow false" >> $DIRECTORY/$CONFIGFTP
echo "open $FTPPROT://$FTPUSER@$FTPHOST" >> $DIRECTORY/$CONFIGFTP
echo "mirror -R --verbose --use-cache --only-missing --ignore-time $FTPLOCA $FTPREMO" >> $DIRECTORY/$CONFIGFTP
echo "quit" >> $DIRECTORY/$CONFIGFTP
echo "bye" >> $DIRECTORY/$CONFIGFTP
DAEMON_OPTS="-f $DIRECTORY/$CONFIGFTP"
# Run daemon
echo "$NAME: $ACTION"
./run_daemon.sh $ACTION $NAME $DAEMON "$DAEMON_OPTS"
exit 0