-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharms
More file actions
executable file
·145 lines (120 loc) · 3.15 KB
/
Copy patharms
File metadata and controls
executable file
·145 lines (120 loc) · 3.15 KB
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
#!/bin/bash
#
# ARMS Remote Mirror System
# Copyright (C) 2014 SitePoint Pty Ltd
#
# Contact details provided at http://www.sitepoint.com/
#
# 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
# (at your option) 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/>.
## Variables
#
declare -r ionice_cmd="ionice -c 2 -n 7"
declare -r hostName="${1}"
declare -r srcPath="${2}"
declare -r dstPath="${3}"
declare -r lockFile="/var/run/arms.pid"
declare -r bwLimit="0"
# There should be no reason to touch anything below this line.
declare -r scriptName="$(basename ${0})"
declare -ri verbose=0
## Functions
#
function doSafetyChecks()
{
# Check we're running as root.
if [ "${UID}" -ne 0 ]
then
printError "Must be run as root."
exit 1
fi
if [ ! -d "${srcPath}" ]
then
printError "Source directory '${srcPath}' not found."
exit 1
fi
ping -c 1 "${hostName}" >/dev/null
if [ "${?}" -ne 0 ]
then
{
echo "Warning: Cannot ping host '${hostName}'."
echo "We may fail at running rsync later."
} 1>&2
fi
}
function printError()
{
echo "Error: ${@}" 1>&2
}
function setLockFile()
{
declare cmdAtPID
# If the lockfile exists
if [ -f "${lockFile}" ]
then
# If the lockfile points to a process by the same name
cmdAtPID="$(ps -p $(cat "${lockFile}") -o comm=)"
echo "${cmdAtPID}" | grep -q "^${scriptName}$"
if [ "${?}" -eq 0 ]
then
# Wait 10 minutes and try again.
echo "Sleeping, waiting for the lock to free up."
sleep 600
setLockFile
else
# Old lock file. Overwrite it.
echo ${$} > "${lockFile}"
fi
else
echo ${$} > "${lockFile}"
fi
}
function delLockFile()
{
rm -f "${lockFile}"
}
function rsyncCmd()
{
function rsyncFailMsg()
{
{
echo -n "Warning: Rsync from "
echo -n "root@${hostName}:${dstPath} "
echo "failed."
} >&2
}
declare -i rsyncFail=0
declare cmdWrap="${ionice_cmd}"
# Used by the bash time command.
TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
[ "${verbose}" -ne 0 ] && cmdWrap="time ${cmdWrap}"
eval ${cmdWrap} rsync -aH -e \'ssh -q\' --delete \
$([ -n "${bwLimit}" ] && echo --bwlimit=${bwLimit}) \
${srcPath} root@${hostName}:${dstPath} || rsyncFail=1
if [ "${rsyncFail}" -ne 0 ]
then
rsyncFailMsg
fi
# Formatting improvements
if [ "${verbose}" -ne 0 ]
then
echo -e '\n'
fi
}
## Begin execution
#
doSafetyChecks
setLockFile
rsyncCmd
delLockFile
# EOF