-
Notifications
You must be signed in to change notification settings - Fork 0
/
lrsync.sh
executable file
·164 lines (143 loc) · 4.62 KB
/
lrsync.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
#!/bin/bash
#
# This file is part of LRSync. Copyright © 2011-2012 Christophe Labouisse.
#
# LRSync is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LRSync is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LRSync. If not, see <http://www.gnu.org/licenses/>.
cleanAndExit() {
code=${1-0}
# Unlock repo if locked
if [ -n "$repoLocked" ]; then
msg "Unlocking '${LRS_REPO_FILE}'"
rm -f "${LRS_REPO_FILE}.lock"
repoLocked=""
fi
# Unlock catalog file.
if [ -n "$catLocked" ]; then
msg "Unlocking '$LRS_CAT_FILE'"
rm -f "${LRS_CAT_FILE}.lock"
catLocked=""
fi
exit $code
}
LRS_TRM=$(readlink "$0" || echo "$0") # The Real Me
LRS_BASEDIR=$(dirname $LRS_TRM)
LRS_LIBDIR=$LRS_BASEDIR/lib
# Read options and configuration
# Just in case the init file cannot be read.
set -e
. ${LRS_LIBDIR}/init.sh
# Lock catalog file.
msg "Locking catalog file '$LRS_CAT_FILE'"
if lockfile -! -r 1 -1 "${LRS_CAT_FILE}.lock"; then
echo "Catalog already locked, aborting"
exit 4
fi
catLocked=1
if [ -n "$LRS_LOCKREPO" ]; then
# Lock catalog file.
msg "Locking catalog file '$LRS_REPO_FILE'"
if lockfile -! -r 1 -1 "${LRS_REPO_FILE}.lock"; then
echo "Repo locked, aborting"
cleanAndExit 4
fi
repoLocked=1
fi
if [ "$LRS_COMMAND" = "display" ]; then
msg "The following rootfolders exist in the catalog:"
echo "select absolutePath from AgLibraryRootFolder;" | ${SQLITE} "$LRS_CAT_FILE"
cleanAndExit
fi
# Find out the right direction to use.
if [ "$LRS_COMMAND" = "auto" ]; then
if [ "$LRS_CAT_FILE" -ot "$LRS_REPO_FILE" ]; then
LRS_COMMAND="from"
else
LRS_COMMAND="to"
fi
msg "Changing 'auto' command to '$LRS_COMMAND'"
fi
if [ "$LRS_COMMAND" = "to" ]; then
sourceCat="$LRS_CAT_FILE"
sourceDir="$LRS_CAT_DIR"
sourceVols=("${catVols[@]}")
destCat="$LRS_REPO_FILE"
destDir="$LRS_REPODIR"
destVols=("${repoVols[@]}")
else
sourceCat="$LRS_REPO_FILE"
sourceDir="$LRS_REPODIR"
sourceVols=("${repoVols[@]}")
destCat="$LRS_CAT_FILE"
destDir="$LRS_CAT_DIR"
destVols=("${catVols[@]}")
fi
# Exit if dest catalog is more recent than source catalog
if [ -z "$LRS_FORCE" -a "$sourceCat" -ot "$destCat" ]; then
echo "Source catalog is older than destination catalog, not converting unless -f is supplied" >&2
cleanAndExit 6
fi
# Check if the source catalog has been changed since last synch and exit if not.
if [ -z "$LRS_FORCE" -a -f "${LRS_REPODIR}/${LRS_CATALOG}.shasum" ]; then
if shasum -c -s "${LRS_REPODIR}/${LRS_CATALOG}.shasum"; then
echo "No change in catalog files, skipping synchronization unless -f is supplied" >&2
cleanAndExit
fi
fi
# Create a temporary catalog in the destination directory.
TEMPCAT=$(mktemp "${destDir}/${LRS_CATALOG}.lrsync.XXXXXXXX")
msg "Copying source catalog to $TEMPCAT"
cp -p "${sourceCat}" "${TEMPCAT}"
{
# [ "$LRS_QUIET" ] || echo ".echo on"
for (( i=0 ; i < ${#sourceVols[*]} ; i++ ))
do
cat <<EOF
update AgLibraryRootFolder set absolutePath=replace(absolutePath, '${sourceVols[$i]}', '${destVols[$i]}') where absolutePath LIKE '${sourceVols[$i]}/%';
EOF
done
} | ${SQLITE} "${TEMPCAT}"
if [ $? -ne 0 ]; then
echo "Error while processing catalog '$sourceCat'" >&2
rm -f "$TEMPCAT"
cleanAndExit 5
fi
# Check if the dest catalog does not contain unconverted folders
if [ -z "$LRS_FORCE" ]; then
statement="select name, absolutePath from AgLibraryRootFolder where id_local not in (select id_local from AgLibraryRootFolder where"
for (( i=0 ; i < ${#destVols[*]} ; i++ ))
do
[ $i -gt 0 ] && statement="$statement or"
statement="$statement absolutePath like '${destVols[$i]}/%'"
done
statement="$statement);"
unconvFolders=$(echo "$statement" | $SQLITE "${TEMPCAT}")
if [ -n "$unconvFolders" ]; then
cat >&2 <<EOF
Converted catalog still contains unconverted folders. Won't convert without -f.
Unconverted folders:
$unconvFolders
EOF
rm -f "$TEMPCAT"
cleanAndExit 7
fi
fi
touch -r "${sourceCat}" "${TEMPCAT}"
[ -f "${destCat}" ] && mv -f "${destCat}" "${destCat}.lrsync"
mv "${TEMPCAT}" "${destCat}"
msg "Synchronizing previews"
rsync -a --delete "${sourceDir}/${LRS_CATALOG} Previews.lrdata" "${destDir}"
msg "Creating checksums"
# Create checksums
shasum ${LRS_REPO_FILE} ${LRS_CAT_FILE} > ${LRS_REPODIR}/${LRS_CATALOG}.shasum
cleanAndExit