-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror-sync.sh
executable file
·87 lines (70 loc) · 2.71 KB
/
mirror-sync.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
#!/bin/bash
## Alejandro Suarez Cebrian (Responsable de la Oficina de Software Libre de la Universidad de Zaragoza)
## Copyright (C) 2015 Alejandro Suarez Cebrian
##
## This program 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
## any later version.
##
## This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
##
# Exit on errors and unset variables
set -e
set -u
log () {
echo "$(date --rfc-3339=seconds) [$PID] $@"
}
export PATH='/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin'
if [[ $# -ne 1 ]]
then echo "Error: Missing arguments"
echo
echo " Usage: $0 config-file.conf"
exit 1
fi
source $1
PID=$$
SHARED_LOCK_DIR=$(dirname "$SHARED_LOCK")
# Log all activity to file.
exec >> $LOGFILE 2>&1
#trap 'savelog -c 28 -n $LOGFILE > /dev/null' EXIT
# Creating PID file
if ! ( set -o noclobber; echo "$PID" > "${PIDFILE}") 2> /dev/null; then
log "PID file exists, update already running, stopping."
exit 1
fi
trap 'rm -f $PIDFILE > /dev/null 2>&1; savelog -c 28 -n $LOGFILE > /dev/null' EXIT
# Create SHARED_LOCK directory
if ! ( mkdir -p -m a+w "$SHARED_LOCK_DIR" ) ; then
log "Error while creating SHARED LOCK folder, stopping."
exit 1
fi
# Check if another synchronization proccess is running
if [ "$(ls -A $SHARED_LOCK_DIR )" ] ; then
log "SHARED_LOCK files exists in $SHARED_LOCK_DIR, another update proccess already running, waiting $TIMEOUT seconds."
fi
DATE=$(date +%s)
while [ "$(ls -A $SHARED_LOCK_DIR )" ] && [ $(( $(date +%s) - $DATE )) -lt $TIMEOUT ] ; do
sleep $((60 + $RANDOM/1024))
done
if [ "$(ls -A $SHARED_LOCK_DIR )" ] ; then
log "SHARED_LOCK files still exists in $SHARED_LOCK_DIR, another update proccess already running, we've waited for $TIMEOUT seconds, running anyway."
fi
# Creating SHARED_LOCK file
trap 'rm -f $PIDFILE > /dev/null 2>&1; rm -f $SHARED_LOCK > /dev/null 2>&1; savelog -c 28 -n $LOGFILE > /dev/null' EXIT
echo $PID > "$SHARED_LOCK"
log "Starting to sync from master repository @ '$SRC'..."
if nice -n $NICE_LEVEL ionice -c$IONICE_CLASS -n$IONICE_LEVEL rsync $RSYNC_OPTS $SRC/ $DEST/ ;then
LC_ALL=POSIX LANG=POSIX date -u > $DEST/lastsync
date >> $DEST/lastsync
log "Finished sync succesfuly"
else
log "Error syncing"
fi
exit 0