Subversion Repositories Projects

Rev

Rev 307 | Rev 312 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
277 dhylands 1
#!/bin/bash
2
#
3
# This script actss as a frontend for rsnapshot. It checks to see if 
4
# my laptop is connected to my network at home, and if it is, then
5
# it fires up the backup.
6
#
307 dhylands 7
# To install the script, add the following line to /etc/cron.d/rsnapshot
8
#
9
#   00 2        * * *           root    /home/dhylands/projects/bash/backup.sh daily
10
#
11
# cron should pick up the change automatically
277 dhylands 12
 
307 dhylands 13
scriptname=$(basename $0)
14
scriptdir=$(dirname $0)
15
 
277 dhylands 16
LOG=/var/log/backup.log
17
 
18
HOSTNAME=$(hostname)
19
 
307 dhylands 20
echo "$(date): ==============================================" >> ${LOG}
21
echo "$(date): Starting backup of HOSTNAME = ${HOSTNAME}" >> ${LOG}
277 dhylands 22
 
307 dhylands 23
BACKUP_MOUNT=/backup-dave
24
BACKUP_DIR=${BACKUP_MOUNT}/${HOSTNAME}
25
 
26
# Check to see if we're at home or not.
27
 
277 dhylands 28
CHECK_WIRELESS=0
29
 
30
case ${HOSTNAME} in
31
 
32
    # These machines are always attached to the network, so don't
33
    # need the wireless check
34
 
35
    dave-ubuntu)
36
        CHECK_WIRELESS=0
37
        ;;
38
 
39
    dave-laptop)
40
        CHECK_WIRELESS=1
41
        ;;
42
 
43
    *)
44
        echo "$(date): Unrecognized hostname '${HOSTNAME}'" >> ${LOG}
45
        exit 0
46
        ;;
47
 
48
esac
49
 
50
if [ ${CHECK_WIRELESS} = 1 ];
51
then
307 dhylands 52
    if ! ${scriptdir}/check-wireless.sh >> ${LOG}
277 dhylands 53
    then
307 dhylands 54
        exit 1
277 dhylands 55
    fi
56
fi
57
 
58
#
307 dhylands 59
# Check to see that /backup-dave is mounted, and if not
60
# then mount it
61
#
62
 
63
if mount | grep ${BACKUP_MOUNT}
64
then
65
    echo "$(date): ${BACKUP_MOUNT} appears to be mounted." >> ${LOG}
66
else
67
    echo "$(date): ${BACKUP_MOUNT} doesn't appear to be mounted - trying to mount" >> ${LOG}
68
 
69
    # Volume doesn't appear to be mounted. Mount it now
70
    # so we don't backup locally
71
 
72
    if mount ${BACKUP_MOUNT}
73
    then
74
        echo "$(date): ${BACKUP_MOUNT} mounted successfully" >> ${LOG}
75
    else
76
        echo "$(date): Unable to mount ${BACKUP_MOUNT} - aborting backup" >> ${LOG}
77
        exit 1
78
    fi
79
fi
80
 
81
#
277 dhylands 82
# Since we may not be connected to the network all of the time, we run this
83
# script often, but only do the backups when the oldest backup of a given
84
# level is more than it's period old. So when weekly.0 is more than a week
85
# old then we do a weekly backup.
86
#
87
 
88
daySecs=$(( 24 * 60 * 60 ))
89
weekSecs=$(( $daySecs * 7 ))
90
monthSecs=$(( $daySecs * 365 / 12 ))
91
 
92
currTime=$(date '+%s')
93
 
94
#
95
# Always do a daily backup
96
#
97
 
98
echo "$(date): Started daily rsnapshot" >> ${LOG}
99
nice /usr/bin/rsnapshot -v daily >> ${LOG} 2>&1
100
echo "$(date): Finished daily rsnapshot" >> ${LOG}
101
 
102
weekStamp=$(find ${BACKUP_DIR}/ -maxdepth 1 -name weekly.0 -printf '%T@\n')
103
weekStamp=${weekStamp%.*}
104
 
105
if [ $(( $weekStamp + $weekSecs + $weekSecs )) -lt $currTime ]
106
then
107
    # weekly.0 is more than two weeks old. Do a weekly backup.
108
    # We need to use two weeks since daily.6 will become weekly.0
109
    # and daily.6 is already a week old
110
 
111
    echo "$(date): Started weekly rsnapshot" >> ${LOG}
112
    nice /usr/bin/rsnapshot -v weekly >> ${LOG} 2>&1
113
    echo "$(date): Finished weekly rsnapshot" >> ${LOG}
114
fi
115
 
116
monthStamp=$(find ${BACKUP_DIR}/ -maxdepth 1 -name monthly.0 -printf '%T@\n')
278 dhylands 117
monthStamp=${monthStamp%.*}
277 dhylands 118
 
119
if [ $(( $monthStamp + $monthSecs + $monthSecs )) -lt $currTime ]
120
then
121
    # monthly.0 is more than two months old. Do a monthly backup.
122
    # weekly.4 will become monthly.0
123
 
124
    echo "$(date): Started monthly rsnapshot" >> ${LOG}
125
    nice /usr/bin/rsnapshot -v monthly >> ${LOG} 2>&1
126
    echo "$(date): Finished monthly rsnapshot" >> ${LOG}
127
fi
128
 
129