-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirsync
More file actions
102 lines (77 loc) · 2.22 KB
/
dirsync
File metadata and controls
102 lines (77 loc) · 2.22 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
#!/bin/bash
# vim: sw=5
###########################################################################
#
# Program name: dirsync
# File name: dirsync.sh
# Author: Peter Blajev
# First revision: 06/17/2012
#
# Description:
# Rsyncs local directories.
#
###########################################################################
emailTO="[email protected]"
PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin"
self="`basename $0`"
# Uncomment this line for debugging
#set -x
HOSTNAME=`hostname | sed 's/\..*//'`
USAGE="NO"
# Use some files
tempFile="/tmp/${self}.run-`date +%Y%m%d`-$$"
# Directory to exclude
EXCLUDE="--exclude '*.backup' --exclude '*.writing'"
# Collect some parametters
#
sDir=$1
if [ ! -n "$sDir" ] || [ ! -d $sDir ]; then
echo "ERROR: Directory $sDir doesn't exit."
USAGE="YES"
fi
dDir=$2
if [ ! -n "$dDir" ] || [ ! -d $dDir ]; then
echo "ERROR: Directory $dDir doesn't exit."
USAGE="YES"
fi
shouldRun=$3
if [ -n "$shouldRun" ] && [ "$shouldRun" != "run" ]; then
USAGE=YES
fi
if [ "$USAGE" = "YES" ]; then
echo "
${self}:
Usage: $self sourceDirectory destinationDirectory [run]
The command will show what commands will be run to synchronize the
directories.
Add "run" at the end to excecute the commands.
NOTE: When in "run" mode the output will be sent via email.
"
exit 1
fi
emailTemp ()
{
cat $tempFile | mail -s "$1" $emailTO
rm -f $tempFile
}
blogIt ()
{
if [ "$shouldRun" == "run" ]; then
echo $1 >> $tempFile
else
echo $1
fi
}
# ---------------------------------------------------------------------------
# All looking good. Build the command
subject="$HOSTNAME:$self report"
CMD="rsync -aH --stats $EXCLUDE --delete $sDir $dDir"
blogIt "=== [`date +%D\ -\ %T`] Starting $self on $HOSTNAME ==="
blogIt "$CMD"
if [ "$shouldRun" == "run" ]; then
echo $CMD | /bin/bash >> $tempFile 2>&1
[ $? -ne 0 ] && subject="ERROR: ${subject}"
fi
blogIt "=== [`date +%D\ -\ %T`] END $self on $HOSTNAME ==="
[ "$shouldRun" == "run" ] && emailTemp "$subject"
exit 0