-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefresh-SyncFilesConfig.sh
164 lines (125 loc) · 9.67 KB
/
refresh-SyncFilesConfig.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/opt/bin/bash
# -------------------------------------------------------------------------------
# Name: refresh-SyncFilesConfig.sh
# Description: It updates the NoResync configuration file used by sync-files.sh
# It is useful when you do manual updates to the last .uniques file
# Author: Carlos Veira Lorenzo - cveira [at] thinkinbig.org
# Version: 0.3b
# Date: 2014/05/29
# -------------------------------------------------------------------------------
# Usage: refresh-SyncFilesConfig.sh <ConfigProfileName>
# -------------------------------------------------------------------------------
# Dependencies: ls, cp, mv, rm, cat, awk, grep, sed, tr, tee, mkfifo
# sync-files-<ConfigProfileName>.conf
# -------------------------------------------------------------------------------
# Notes:
# Tested under Bash + BusyBox
# Works under Synology NAS
# -------------------------------------------------------------------------------
export PATH=/opt/bin:/opt/sbin:/opt/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/syno/sbin:/usr/syno/bin:/usr/local/sbin:/usr/local/bin
InstallDir=/volume1/jobs/scripts
LogsDir=/volume1/jobs/logs
TmpDir=/volume1/jobs/tmp
WORKAREA="_workarea"
OriginalDir="$PWD"
ConfigurationProfile="$1"
SourcePath="SourcePath"
DestinationPath="DestinationPath"
UserName="UserName"
Password="Password"
ConnectionSettingsName="ConnectionSettingsName"
RemotePath="RemotePath"
LocalPath="LocalPath"
TargetUrl="TargetUrl"
ParallelJobs="ParallelJobs"
ParallelChunksPerFile="ParallelChunksPerFile"
BandwidthRateLimit="BandwidthRateLimit"
TimeOut="TimeOut"
MaxRetries="MaxRetries"
ReconnectIntervalBase="ReconnectIntervalBase"
ReconnectIntervalMultiplier="ReconnectIntervalMultiplier"
ReconnectIntervalMax="ReconnectIntervalMax"
VerifyTransfer="VerifyTransfer"
VerifySslCertificate="VerifySslCertificate"
OnStart="OnStart"
OnCompletion="OnCompletion"
CurrentDate=$(date +%Y%m%d)
CurrentSequenceId=$(ls -1AB $LogsDir/*$CurrentDate* 2> /dev/null | wc -l)
CurrentSessionId="$CurrentDate-$CurrentSequenceId"
function BuildExclusionSettings {
if [ -f "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques" ] ; then
# let's exclude file types we always want to exclude
echo "--exclude \"^_.*$|^.*\\.nfo$|^.*\\.diz$|^.*\\.lnk$|^.*\\.db$|^.*watchdir_rtorrent$\"" >> "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.conf"
# let's build the lftp exclusion settings file. Characters that also happen to be elementso of RegEx syntax must be escaped.
sed -r "s/\./\\\./g" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques" >> "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques0"
sed -r "s/\[/\\\[/g" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques0" >> "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques1"
sed -r "s/\]/\\\]/g" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques1" >> "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques2"
sed -r "s/\(/\\\(/g" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques2" >> "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques3"
sed -r "s/\)/\\\)/g" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques3" >> "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques4"
sed -r "s/\{/\\\{/g" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques4" >> "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques5"
sed -r "s/\}/\\\}/g" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques5" >> "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques6"
sed -r "s/\|/\\\|/g" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques6" >> "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques7"
sed -r "s/\^/\\\^/g" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques7" >> "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques8"
sed -r "s/\\\$/\\\\$/g" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques8" >> "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques9"
sed -r "s/\+/\\\+/g" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques9" >> "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques10"
mv "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques10" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.patterns"
rm -f "$SourcePath/$WORKAREA/$ConfigurationProfile"/*.uniques[0-9]
cat "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.patterns" | awk '{ print "--exclude \"^.*" $0 ".*$\"" }' >> "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.conf0"
cat "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.conf0" | tr "\n" " " >> "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.conf"
rm -f "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.conf0"
fi
}
echo
echo "-------------------------------------------------------------------------------------------"
echo "Refresh-SyncFilesConfig "
echo "Carlos Veira Lorenzo - [http://thinkinbig.org] "
echo "-------------------------------------------------------------------------------------------"
echo "Sync-Files v0.3b, Copyright (C) 2014 Carlos Veira Lorenzo. "
echo "This software come with ABSOLUTELY NO WARRANTY. This is free "
echo "software under GPL 2.0 license terms and conditions. "
echo "-------------------------------------------------------------------------------------------"
echo
if [ ! -f $InstallDir/"import-newmedia-$ConfigurationProfile.conf" ] ; then
echo "+ ERROR: Can't find a Configuration Profile named $ConfigurationProfile"
exit 1
fi
if [ ! -f $InstallDir/"sync-files-$ConfigurationProfile.conf" ] ; then
echo "+ ERROR: Can't find a Configuration Profile named $ConfigurationProfile"
exit 1
fi
echo "+ Loading configuration ..."
echo
mkfifo $TmpDir/pipe-ImportMedia-$CurrentSessionId
cat $InstallDir/"import-newmedia-$ConfigurationProfile.conf" | grep -v "#" | grep . > $TmpDir/pipe-ImportMedia-$CurrentSessionId &
while read ConfigurationItem ; do
PropertyName=`echo $ConfigurationItem | awk -F "=" '{ print $1 }'`
PropertyValue=`echo "$ConfigurationItem" | awk -F "=" '{ print $2 }'`
if [ $PropertyName == $SourcePath ] ; then SourcePath="$PropertyValue" ; fi
if [ $PropertyName == $DestinationPath ] ; then DestinationPath="$PropertyValue" ; fi
done < $TmpDir/pipe-ImportMedia-$CurrentSessionId
rm -f $TmpDir/pipe-ImportMedia-$CurrentSessionId
echo "+ Saving previous state ..." | tee -a "$LogsDir/RefreshSyncFilesConfig-$ConfigurationProfile-$CurrentSessionId.log"
if [ -f "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.raw" ] ; then
echo " + Saving last list of RAW elements to exclude from future transfers ..." | tee -a "$LogsDir/RefreshSyncFilesConfig-$ConfigurationProfile-$CurrentSessionId.log"
cp "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.raw" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.raw.$CurrentSessionId"
fi
if [ -f "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-ForceResync.raw" ] ; then
echo " + Saving last list of RAW elements that failed last time ..." | tee -a "$LogsDir/RefreshSyncFilesConfig-$ConfigurationProfile-$CurrentSessionId.log"
cp "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-ForceResync.raw" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-ForceResync.raw.$CurrentSessionId"
fi
if [ -f "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques" ] ; then
echo " + Saving last list of unique elements to exclude from future transfers ..." | tee -a "$LogsDir/RefreshSyncFilesConfig-$ConfigurationProfile-$CurrentSessionId.log"
cp "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.uniques.$CurrentSessionId"
fi
if [ -f "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.patterns" ] ; then
echo " + Saving last list of patterns to exclude from future transfers ..." | tee -a "$LogsDir/RefreshSyncFilesConfig-$ConfigurationProfile-$CurrentSessionId.log"
mv "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.patterns" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.patterns.$CurrentSessionId"
fi
if [ -f "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.conf" ] ; then
echo " + Saving last exclusion settings ..." | tee -a "$LogsDir/RefreshSyncFilesConfig-$ConfigurationProfile-$CurrentSessionId.log"
mv "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.conf" "$SourcePath/$WORKAREA/$ConfigurationProfile/sync-files-NoReSync.conf.$CurrentSessionId"
fi
echo " + Generating new exclusion settings ..." | tee -a "$LogsDir/RefreshSyncFilesConfig-$ConfigurationProfile-$CurrentSessionId.log"
BuildExclusionSettings
# Make one copy of the session log available to users
cp "$LogsDir/RefreshSyncFilesConfig-$ConfigurationProfile-$CurrentSessionId.log" "$LocalPath/$WORKAREA/$ConfigurationProfile/logs" 2> /dev/null