#!/bin/bash
# config
web_dir="/var/www/"
mail_dir="/var/spool/mail"
backup_group="my_backup"
mysql_opt="--opt --allow-keywords"
mysql_pass="blubber"
sql_backup="yes"
mail_backup="yes"
ftp_upload="yes"
ftp_user="user"
ftp_pass="blubber"
ftp_ip="192.168.0.2"
ftp_dir="/backup"
test -d /backup || /bin/mkdir /backup
test -d /backup/excludes || /bin/mkdir /backup/excludes
if [ ! -n "`cat /etc/group | grep ${backup_group}`" ]; then
groupadd ${backup_group}
fi
# system
/bin/tar cfz /backup/etc.tar.gz /etc
/etc/init.d/mysql stop
/bin/tar cfz /backup/mysql.tar.gz /var/lib/mysql/confixx /var/lib/mysql/mysql
/etc/init.d/mysql start
/bin/tar cfz /backup/system.tar.gz /backup/etc.tar.gz /backup/mysql.tar.gz
/bin/rm /backup/etc.tar.gz /backup/mysql.tar.gz
if [ "${ftp_upload}" = yes ]; then
/usr/bin/ncftpput -V -u ${ftp_user} -p ${ftp_pass} ${ftp_ip} ${ftp_dir} /backup/system.tar.gz
fi
# users
for nr in `ls ${web_dir} | grep web\[0-9\] | sed 's/web//'`
do
test -d /backup/web${nr} || /bin/mkdir /backup/web${nr}
test -e /backup/excludes/exclude_${nr}.list || /bin/touch /backup/excludes/exclude_${nr}.list
/bin/tar -X /backup/excludes/exclude_${nr}.list -C ${web_dir}/web${nr}/html -c -z -f /backup/web${nr}/web${nr}_html.tar.gz .
if [ "${sql_backup}" = yes ]; then
for db in `echo show databases | mysql -u root -p${mysql_pass} | grep usr_web${nr}_ | sed 's/usr_web[0-9]\{1,3\}_//'`
do
/usr/bin/mysqldump ${mysql_opt} -uroot -p${mysql_pass} -hlocalhost usr_web${nr}_${db} > /backup/web${nr}/web${nr}_db${db}.sql
done
if [ "`ls /backup/web${nr} | grep web${nr}_db | wc -w`" -gt 0 ]; then
ls /backup/web${nr} | grep web${nr}_db | xargs tar -C /backup/web${nr} -c -z -f /backup/web${nr}/web${nr}_sql.tar.gz
/bin/rm /backup/web${nr}/web${nr}_db*.sql
fi
fi
if [ "${mail_backup}" = yes ] && [ "`ls ${mail_dir} | grep web${nr}p | wc -w`" -gt 0 ]; then
ls /var/spool/mail | grep web${nr}p | xargs tar -C /var/spool/mail -c -z -f /backup/web${nr}/web${nr}_mail.tar.gz
fi
if [ "`ls /backup/web${nr} | grep web\[0-9\] | wc -w`" -gt 1 ]; then
/bin/tar -C /backup/web${nr} -c -z -f /backup/web${nr}.tar.gz .
else
/bin/mv /backup/web${nr}/web${nr}_html.tar.gz /backup/web${nr}.tar.gz
fi
/bin/rm -r /backup/web${nr}
if [ "`groups web${nr} | grep ${backup_group} | sed 's/web[0-9]\{1,3\}.:.ftponly.//'`" = ${backup_group} ]; then
/bin/cp /backup/web${nr}.tar.gz ${web_dir}/web${nr}/files/web${nr}.tar.gz
/bin/chown web${nr}:ftponly ${web_dir}/web${nr}/files/web${nr}.tar.gz
if [ "${ftp_upload}" = yes ]; then
/usr/bin/ncftpput -V -u ${ftp_user} -p ${ftp_pass} ${ftp_ip} ${ftp_dir} /backup/web${nr}.tar.gz
fi
fi
done