Skip to content

Instantly share code, notes, and snippets.

@merob
Created August 15, 2014 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merob/4f8923b014ffb3248f84 to your computer and use it in GitHub Desktop.
Save merob/4f8923b014ffb3248f84 to your computer and use it in GitHub Desktop.
#!/bin/bash
# you do need to change these
DBPW=`cat /root/.dbrootpw` # location of file which _just_ contains database root password
S3PATH="s3://nameofs3bucket" # s3 bucket name
# all lists separated by white space
ACTIVEUSERS="root" # any usernames who have homedirs to backup
DIRS=`ls -d /var/www/*` # list all directories to be backed up. (Will be backed up in separate files)
# below: any custom config files
CONFIG="/etc/httpd/conf/httpd.conf /etc/httpd/conf.d /etc/postfix /etc/sysconfig/network /etc/sysconfig/iptables /etc/ssh/sshd_config /etc/aliases"
# you might need to change these
DBHOST=localhost # hostname of mysql server
DBUSER=root # username with access to databases which need to be backed up
LOCALBACKUPROOT=/root/backups # top level for backups.
S3CMD=/usr/local/bin/s3cmd # location of s3cmd
# you probably don't need to change anything below here.
DATE=`date +%Y-%m-%d`
BKLOC=$LOCALBACKUPROOT/$DATE
#create backup dir
if [ ! -d "$BKLOC" ]
then
if [ ! -d "$LOCALBACKUPROOT" ]
then
mkdir $LOCALBACKUPROOT
fi
mkdir $BKLOC
fi
#get list of databases to backup
DATABASES=$((mysql -u $DBUSER -p$DBPW | tail -n +2) <<EOF
show databases ;
EOF
)
#add homedirs of active users
for USER in $ACTIVEUSERS
do
#crontab for each user
crontab -lu $USER > $BKLOC/$USER.crontab
#add homedirs to files
USERHOME=`awk -F: -v u="$USER" '{if ($1==u) print $6}' /etc/passwd`
if [ -d "$USERHOME" ]
then
DIRS="$DIRS $USERHOME"
fi
done
#start making files
#create backups of directories
for dir in $DIRS
do
TARNAME=${dir//\//_}
tar --exclude=$LOCALBACKUPROOT -jcf $BKLOC/$TARNAME.tar.bz2 $dir
done
#backup config files
tar --exclude=$LOCALBACKUPROOT -jcf $BKLOC/config.tar.bz2 $CONFIG
#backup all databases
for db in $DATABASES
do
mysqldump -u $DBUSER -p$DBPW $db | bzip2 > $BKLOC/$db.sql.bz2
done
#dump all to s3
echo sync $BKLOC to s3
$S3CMD sync $BKLOC/ $S3PATH/backups/latest/
echo duplicate s3 daily backup to latest
$S3CMD sync $S3PATH/backups/latest/ $S3PATH/backups/$DATE/
#remove local copy
rm -rf $BKLOC/*bz2 $BKLOC/*crontab
rmdir $BKLOC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment