All pastes #153946 Raw Edit

Stuff

public text v1 · immutable
#153946 ·published 2006-08-30 00:05 UTC
rendered paste body
#!/bin/bash
#Backup script for my two computers. Copies the nightly backup from 
#remote machine and does a local backup of this machine.
 
TIME_FILE=/var/log/backup_time
BACKUP_DIR=/
BASE_DIR=/mnt/backup
LOG_FILE=/var/log/backup_error_log
COUNT=3
DIR_NAME="$BASE_DIR/week_$(date +%U)_$(date +%Y)"

#Only want root to be able to read backups. Otherwise it is a loop hole to
#read all files in the system including /etc/shadow!
umask 0077

#clear log file
date                                                    > $LOG_FILE 2>&1

#Want to keep backup partition unmounted most of the time for safely reasons.
if ! $( df | grep -q /mnt/backup &> /dev/null)
then
    echo 'mounting /mnt/backup'                        >> $LOG_FILE 2>&1
    if ! mount /mnt/backup                             >> $LOG_FILE 2>&1
    then 
	echo mount /mnt/backup failed exiting          >> $LOG_FILE 2>&1
	exit 1
    fi 
fi

#Do an differential backup if $1 is set
if [ "$1" ];
then
    ATLAS_FILE=atlas-$(date +%a-%d-%b)-diff.tar
    echo differential backup                           >> $LOG_FILE 2>&1
    cd $DIR_NAME                                       >> $LOG_FILE 2>&1
    tar c --newer=$TIME_FILE --one-file-system \
          -f $(hostname -s)-$(date +%a-%d-%b)-diff.tar \
          $BACKUP_DIR                                  >> $LOG_FILE 2>&1
else
    ATLAS_FILE=atlas-$(date +%a-%d-%b)-full.tar
    for f in $(ls -1dt $BASE_DIR/week_* | tail --lines=+$COUNT)
    do 
      echo removing old backups                        >> $LOG_FILE 2>&1
      /bin/rm -fr $f                                   >> $LOG_FILE 2>&1
    done

    #Needs to be after directory clean up.
    if [ ! -d "$DIR_NAME" ] ; 
    then
	echo creating directory $DIR_NAME              >> $LOG_FILE 2>&1
	mkdir $DIR_NAME                                >> $LOG_FILE 2>&1
    fi
    cd $DIR_NAME                                       >> $LOG_FILE 2>&1
    echo full backup                                   >> $LOG_FILE 2>&1
    date > $TIME_FILE
    tar c --one-file-system -f $(hostname -s)-$(date +%a-%d-%b)-full.tar \
          $BACKUP_DIR                                  >> $LOG_FILE 2>&1
fi

#Copy atlas backup to local directory
DIR=/mnt/backup/week_$(date +%U)_$(date +%Y)
FILE=backup.tar
REMOTE_FILE=$DIR/$FILE  
BACKUP_FILE=$DIR/$ATLAS_FILE

#Need a directory cat can write to.
LOCAL_FILE=/mnt/backup/atlas_backup/$FILE 

#In theory the local user could read the file while it's being copied so
#in a more security conscious situation you could set up a non-login user 
#account to do the following. p.s. Writing securely is inherently hard.
su cat -c "scp catsurf@atlas:$REMOTE_FILE $LOCAL_FILE" >> $LOG_FILE 2>&1

#Only root should be able to read backups.
chown root:root $LOCAL_FILE
chmod 0600 $LOCAL_FILE

mv $LOCAL_FILE $BACKUP_FILE                            >> $LOG_FILE 2>&1

#Won't umount /mnt/backup if this shell is executing in one of it's directories
cd $HOME                                               >> $LOG_FILE 2>&1
umount /mnt/backup                                     >> $LOG_FILE 2>&1