Subversion Repositories Projects

Rev

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