|
|
Backup script
This is the script that I run daily and weekly to back up my computer. The idea is that it uses dump(8) to back up most of the partitions to another server. That server is archived onto tapes, but I'm not the only user of that server, so I PGP/GPG encrypt my backups before they are copied.
#!/bin/sh
# Jason's backup script.
# <jason+backup@ixid.net>
# This does a dump to a remote host over ssh.
# The first argument is the dump level, the default is 0 (full).
DUMPLEVEL=$1
[ -n "$DUMPLEVEL" ] || DUMPLEVEL="0"
umask 066
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:$PATH
export PATH
# Set this up for your site.
DESTHOST=server.elsewhere.com
DESTUSER=jason
DESTPATH=personal-backups
# You must protect this key! In order for backups to be automated,
# this key should not have a password.
DSA_KEY=/home/jason/.ssh/backup_dsa
# These probably don't need to be changed.
# The DATE should not have spaces.
DATE=`date +%Y-%m-%d-%H-%M-%S`
DESTDIR="$DESTPATH/`hostname`-$DATE""-level-$DUMPLEVEL"
DUMPCMD="dump -$DUMPLEVEL -u -a -f - "
COMPRESSCMD="bzip2 -c9"
# Make sure that the user that invokes this has the right public key.
ENCRYPTCMD="gpg --homedir /root/.gnupg -e -r jason"
HOSTNAME=`hostname`
# What should be backed up? Read all of the partitions from /etc/fstab.
PARTITIONS=`cut -f 2 -d " " /etc/fstab`
# Don't try to make a list of what to include--you'll forget
# something important. Use an exclusion list.
EXCLUDE="/tmp"
# Remvove the excluded partitions.
for i in $EXCLUDE ; do\
PARTITIONS=`echo $PARTITIONS | sed -e "s^$i^^"` ;
done;
STARTTIME="Starting backup at "`date +%Y-%m-%d\ %H:%M.%S`
echo $STARTTIME
echo "Partitions: $PARTITIONS"
echo "Dump level: $DUMPLEVEL"
echo "Destination: $DESTUSER@$DESTHOST:$DESTDIR"
echo "----------------------------------------------------";
# First, we have to be able to log into the backup destination.
echo "Starting ssh-agent and adding the backup key."
eval `ssh-agent`
ssh-add $DSA_KEY
# Create the directory on the remote host.
ssh $DESTUSER@$DESTHOST mkdir -p $DESTDIR
# Example of a command line.
# dump -0ua -f - / | gzip -c9 | gpg -e -r jason | \
# ssh jason@server.elsewhere.com "cat > \
# personal-backups/hostname-19SEP2001-full-slash.dump.gz.gpg"
for PART in $PARTITIONS; do \
# substitue the /'s for .'s
PNAME=$HOSTNAME-`echo $PART | sed -e "s^/^\.^g"`-jlbak;
#
echo "Dumping \"$PART\"...";
$DUMPCMD $PART | $COMPRESSCMD | $ENCRYPTCMD | \
ssh $DESTUSER@$DESTHOST "cat > $DESTDIR/$PNAME";
echo "----------------------------------------------------";
done
FINISHTIME="Finished backup at "`date +%Y-%m-%d\ %H:%M.%S`
echo "$STARTTIME\nPartitions: $PARTITIONS\nDump level: $DUMPLEVEL\
\nDump command: $DUMPCMD <partition>\n$FINISHTIME\n\n/etc/fstab:\n" \
| ssh $DESTUSER@$DESTHOST "cat > $DESTDIR/summary";
cat /etc/fstab \
| ssh $DESTUSER@$DESTHOST "cat >> $DESTDIR/summary";
echo "\ndf:\n" \
| ssh $DESTUSER@$DESTHOST "cat >> $DESTDIR/summary";
df | ssh $DESTUSER@$DESTHOST "cat >> $DESTDIR/summary";
echo "\nCompress command: $COMPRESSCMD\nEncrypt command: $ENCRYPTCMD\n\n" \
| ssh $DESTUSER@$DESTHOST "cat >> $DESTDIR/summary";
# Kill ssh-agent after we're finished.
echo "Killing ssh-agent."
kill $SSH_AGENT_PID
echo $FINISHTIME
[CategoryCode]
|