| 1 | #!/usr/local/bin/bash |
|---|
| 2 | |
|---|
| 3 | # This file REQUIRES bash, and can not be run under /bin/sh! |
|---|
| 4 | |
|---|
| 5 | ### |
|---|
| 6 | # Copyright 2006 Bert JW Regeer. All rights reserved. |
|---|
| 7 | # |
|---|
| 8 | # Redistribution and use in source and binary forms, with or without |
|---|
| 9 | # modification, are permitted provided that the following conditions |
|---|
| 10 | # are met: |
|---|
| 11 | # 1. Redistributions of source code must retain the above copyright |
|---|
| 12 | # notice, this list of conditions and the following disclaimer. |
|---|
| 13 | # 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 14 | # notice, this list of conditions and the following disclaimer in the |
|---|
| 15 | # documentation and/or other materials provided with the distribution. |
|---|
| 16 | # |
|---|
| 17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
|---|
| 18 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 19 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 20 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
|---|
| 21 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 22 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|---|
| 23 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 24 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|---|
| 25 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|---|
| 26 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|---|
| 27 | # SUCH DAMAGE. |
|---|
| 28 | # |
|---|
| 29 | # The views and conclusions contained in the software and documentation are |
|---|
| 30 | # those of the authors and should not be interpreted as representing official |
|---|
| 31 | # policies, either expressed or implied, of bsdPanel project. |
|---|
| 32 | # |
|---|
| 33 | ### |
|---|
| 34 | |
|---|
| 35 | for HOMEDIR in `/usr/bin/find /usr/home -type d -maxdepth 1 | grep -v '^.$'`; do |
|---|
| 36 | if [ -f $HOMEDIR/.exclude ]; then |
|---|
| 37 | echo "Exclude found for $HOMEDIR" |
|---|
| 38 | while read line; do |
|---|
| 39 | if [ "${line:0:1}" != "/" ]; then |
|---|
| 40 | # They are missing a full path, we will be nice and add it |
|---|
| 41 | TMPLINE="$HOMEDIR/$line" |
|---|
| 42 | line=$TMPLINE |
|---|
| 43 | fi |
|---|
| 44 | echo " Excluding $line" |
|---|
| 45 | echo $line >> /tmp/exclude.$$ |
|---|
| 46 | done < $HOMEDIR/.exclude |
|---|
| 47 | fi |
|---|
| 48 | done |
|---|
| 49 | |
|---|
| 50 | mount | grep /backup |
|---|
| 51 | |
|---|
| 52 | if [ $? -ne 0 ]; then |
|---|
| 53 | /sbin/mount /backup |
|---|
| 54 | if [ $? -ne 0 ]; then |
|---|
| 55 | logger -s -p user.err -t backup "Backup drive could not be mounted! Bailing!" |
|---|
| 56 | /sbin/umount -f /backup |
|---|
| 57 | exit 1 |
|---|
| 58 | fi |
|---|
| 59 | fi |
|---|
| 60 | |
|---|
| 61 | BACKUPDIR="/backup/`date "+%Y-%m-%d"`" |
|---|
| 62 | OLDDIR="/backup/`date -v-14d "+%Y-%m-%d"`" |
|---|
| 63 | |
|---|
| 64 | if [ ! -d $BACKUPDIR ]; then |
|---|
| 65 | mkdir $BACKUPDIR |
|---|
| 66 | fi |
|---|
| 67 | |
|---|
| 68 | if [ $? -ne 0 ]; then |
|---|
| 69 | logger -s -p user.err -t backup "Could not create backup folder $BACKUPDIR. Bailing!" |
|---|
| 70 | /sbin/umount -f /backup |
|---|
| 71 | exit 1 |
|---|
| 72 | fi |
|---|
| 73 | |
|---|
| 74 | # We got this far, now we have to run rsync. Rsync is very verbose on purpose |
|---|
| 75 | |
|---|
| 76 | /usr/local/bin/rsync -av --relative --delete /usr/home/ $BACKUPDIR --exclude-from=/tmp/exclude.$$ |
|---|
| 77 | |
|---|
| 78 | EXITCODE=$? |
|---|
| 79 | |
|---|
| 80 | if [ $EXITCODE -ne 0 ]; then |
|---|
| 81 | if [ $EXITCODE -ne 24 ]; then |
|---|
| 82 | logger -s -p user.err -t backup "rsync failed. Do not trust backup ($BACKUPDIR)" |
|---|
| 83 | /sbin/umount /backup |
|---|
| 84 | exit 1 |
|---|
| 85 | fi |
|---|
| 86 | fi |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | echo "Backup complete! Deleting oldest backup (14 days ago)" |
|---|
| 90 | |
|---|
| 91 | if [ -d $OLDDIR ]; then |
|---|
| 92 | rm -rf $OLDDIR |
|---|
| 93 | fi |
|---|
| 94 | |
|---|
| 95 | /sbin/umount /backup |
|---|
| 96 | /bin/rm -f /tmp/exclude.$$ |
|---|
| 97 | |
|---|
| 98 | logger -s -p user.notice -t backup "Backup complete at $BACKUPDIR." |
|---|