Quantcast
Channel: Marius van Witzenburg » Western DIgital
Viewing all articles
Browse latest Browse all 3

How to backup from a Western Digital Sharespace to another Sharespace (v2)

$
0
0

I earlier wrote a script to sync two Western Digital ShareSpace drives. Since that was written in a hurry I thought lets have another good look at it...

I changed the checks for USB drives and sync the complete the shares directory instead of only the shares.

The following script is a new rewritten version of the old script which works faster for my use.

You may modify it to suit your situation but please leave the credits in the file :-)

You might need this: How to enable SSH on a Western Digital ShareSpace

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
#!/bin/sh
# WD ShareSpace sync script v1.1
# By Marius van Witzenburg <info@>
# http://
#
# Works with busybox 1.1.1 and newer versions.
#
 
# Settings
src_ip="172.20.20.22"
mailto="marius@"
mailfrom="noreply@"
daemon="yes"
force="no"
waittime=5
maxlogs=48
 
#
# DO NOT CHANGE BELOW!
#
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin
PID=$$
 
pwd=`pwd`
root=`dirname $0`
logdir=${root}/logs
logfile=`basename $0`
cd ${root}
 
cleanup() {
    #rm -f ${log}
    return $?
}
s_hup() {
    logger -s -p daemon.notice -t ${logfile} "*** SIGHUP! Exiting... ***"
    cleanup
    #exit $?
}
s_int() {
    logger -s -p daemon.notice -t ${logfile} "*** SIGINT! Exiting... ***"
    echo -e "From: ${mailfrom}nTo: ${mailto}nSubject: ShareSpace backup.nnSIGINT! Exiting..." | /usr/sbin/msmtp ${mailto}
    kill ${JOB}
    exit $?
}
s_term() {
    logger -s -p daemon.notice -t ${logfile} "*** SIGTERM! Exiting... ***"
    echo -e "From: ${mailfrom}nTo: ${mailto}nSubject: ShareSpace backup.nnSIGTERM! Exiting..." | /usr/sbin/msmtp ${mailto}
    kill ${JOB}
    exit $?
}
 
trap s_hup SIGHUP
trap s_int SIGINT
trap s_term SIGTERM
 
if [ "${daemon}" = "yes" ]
then
    logger -s -p daemon.notice -t ${logfile} "Having a rest for the first run..."
    sleep 30 &
    JOB=$!
    wait ${JOB}
fi
 
# Loop and loop and loop...
while [ "true" ]
do
    # Check if ssh host is up and running.
    alive=`ssh -ax -c blowfish -q -q -o "BatchMode=yes" -o "ConnectTimeout 5" root@${src_ip} "echo 2>&1" && return 0 || echo 1`
    if [ "${alive}" -eq 0 ]
    then
        # Change the 1 to 1-3 depending on which load you want
        #loadavg=`ssh -ax -c blowfish root@${src_ip} uptime | sed -e "s/.*load average: (.*...), (.*...), (.*...)/1/" -e "s/ //g"`
        loadavg=`ssh -ax -c blowfish root@${src_ip} uptime | sed 's/.*average: ([0-9]).*/1/g'`
 
        if [ "${loadavg}" -eq 0 ] || [ "${force}" = "yes" ]
        then
            # Be sure we have a log dir
            test -d ${logdir} || mkdir ${logdir}
 
            # Shift logs
            test -f ${logdir}/${logfile}.${maxlogs} && rm -f ${logdir}/${logfile}.${maxlogs}
            lcur=${maxlogs}
            while [ ${lcur} -gt 1 ]
            do
                lpre=${lcur}
                lcur=`expr ${lcur} - 1`
                test -f ${logdir}/${logfile}.${lcur}.gz && mv ${logdir}/${logfile}.${lcur}.gz ${logdir}/${logfile}.${lpre}.gz
            done
            test -f ${logdir}/${logfile} && mv ${logdir}/${logfile} ${logdir}/${logfile}.1
            test -f ${logdir}/${logfile}.1 && gzip ${logdir}/${logfile}.1
 
            logger -s -p daemon.notice -t ${logfile} "Starting backup from '${src_ip}'"
 
            # Sync internal shares and exclude usb shares
            logger -s -p daemon.notice -t ${logfile} "Syncing internal shares..."
            rsync -e 'ssh -ax -c blowfish -l root' -aWvz --timeout=30 --exclude="/usb[1-3]-1share1" --delete --stats ${src_ip}:/shares/ /shares/ > ${logdir}/${logfile} 2>&1 &
            JOB=$!
            wait ${JOB}
 
            # Sync usb shares if connected
            for x in `ssh -ax -c blowfish root@${src_ip} ls -l /shares/| grep "^d" | awk '{ print $9 }'`
            do
                if [ ! -z "`expr ${x} : '(usb[0-9]-[0-9]share[0-9])'`" ]
                then
                    if [ -d "/shares/${x}/" ]
                        then
                        logger -s -p daemon.notice -t ${logfile} "Syncing ${x}..."
                        rsync -e 'ssh -ax -c blowfish -l root' -aWvz --timeout=30 --delete --stats ${src_ip}:/shares/${x}/ /shares/${x}/ >> ${logdir}/${logfile} 2>&1 &
                        JOB=$!
                        wait ${JOB}
                    fi
                fi
            done
 
            tail -2 ${logdir}/${logfile} | logger -s -p daemon.info -t ${logfile}
        else
            logger -s -p daemon.notice -t ${logfile} "Load average on '${src_ip}' is ${loadavg} - aborting"
        fi
    else
        logger -s -p daemon.notice -t ${logfile} "No access to '${src_ip}' - aborting"
    fi
 
    if [ "${daemon}" != "yes" ]
    then
        logger -s -p daemon.notice -t ${logfile} "Backup finished"
        break
    fi
 
    logger -s -p daemon.notice -t ${logfile} "Waiting for cycle (${waittime} minutes)..."
    sleep `expr ${waittime} * 60` &
    JOB=$!
    wait ${JOB}
    logger -s -p daemon.notice -t ${logfile} "Running cycle..."
done
 
# Go back to old location
cd ${pwd}
 
# EOF

Viewing all articles
Browse latest Browse all 3

Trending Articles