backup.sh

#!/bin/sh

#
# Script to backup neccesary files to backup server
#
# Prerequsities
# 1. 'backup' user exists on server
# 2. SSH account is setup for 'backup' with no passphrase
#    (see ssh-keygen for more information)
# 3. ~/$HOST directory exists on server
#

# Constants
CURRENT=/root/bin
BACKUP_FILE=$CURRENT/all-20`date +%y%m%d`.tgz
ETC_FILE=$CURRENT/etc.tgz
WIKI_DB_FILE=$CURRENT/wiki_production.sql
EMAN_DB_FILE=$CURRENT/eman_production.sql
SFTP_BATCH=$CURRENT/sftp-batch
HOST=balrog.lounge.se
#SERVER=www.lounge.se
SERVER=192.168.0.1
RAILS_ROOT=/var/www/pages/rails

echo "Backup file to backup@$SERVER"

# Create SFTP batch file (used later)
echo "cd $HOST" > $SFTP_BATCH
echo "put $BACKUP_FILE" >> $SFTP_BATCH
echo "exit" >> $SFTP_BATCH

# Stop lighttpd
# echo 'Stop lighttpd, dump database, create tars'
# pkill lighttpd

# Dump databases (don't know how to handle password)
/usr/local/bin/mysqldump -u'<user>' -p'<password>' wiki_production > $WIKI_DB_FILE
/usr/local/bin/mysqldump -u'<user>' -p'<password>' eman_production > $EMAN_DB_FILE

# Create tar file
/usr/local/bin/gtar -czf $ETC_FILE -X $CURRENT/etc-exclude /etc/*

/usr/local/bin/gtar -czf $BACKUP_FILE \
    $RAILS_ROOT/wiki/app \
    $RAILS_ROOT/eman/app \
    $RAILS_ROOT/wiki/public/stylesheets \
    $RAILS_ROOT/eman/public/stylesheets \
    $RAILS_ROOT/wiki/db \
    $RAILS_ROOT/eman/db \
    $WIKI_DB_FILE \
    $EMAN_DB_FILE \
    $ETC_FILE \
    $CURRENT/etc-exclude \
    $SFTP_BATCH

#    Includes uploaded files
#    $RAILS_ROOT/wiki/public/wiki/files \

# Start lighttpd
# echo 'Start lighttpd'
# /usr/local/sbin/lighttpd -f /etc/lighttpd.conf

# Upload tar. Remove afterwards
sftp -b$SFTP_BATCH "backup@$SERVER"

# Remove temporary files
rm $BACKUP_FILE
rm $ETC_FILE
rm $WIKI_DB_FILE
rm $EMAN_DB_FILE
rm $SFTP_BATCH