#!/bin/bash
#
# Daily confixx backup by monotek
#
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:
# config
web_dir="/var/www"
mail_dir="/home/confixx"
log_file="/var/log/backup.log"
backup_group="backup"
#mysql_opt="--opt --allow-keywords --compatible=mysql40"
mysql_opt="--opt --allow-keywords"
mysql_pass="rootpass"
sql_backup="yes"
mail_backup="no"
ftp_upload="no"
ftp_user="ftpuser"
ftp_pass="ftppass"
ftp_ip="192.168.0.1"
ftp_dir="/"
send_log="no"
sendmail="/usr/sbin/sendmail"
mail_to_log="mail@domain.de"
mail_from_log="mail@domain.de"
error_count="0"
date=`date '+%G%m%d'`
time=`date '+%T'`
function exitcode ()
{
if [ "$?" = 0 ]; then
echo "$1 ok"
else
echo "$1 not ok"
let error_count=error_count+1
fi
}
#script
echo -e "\n${date} ${time} - STARTING BACKUP! \n"
echo -e "\nTesting Dirs"
test -d /backup/excludes || /bin/mkdir -p /backup/excludes && test -d /backup/system || /bin/mkdir /backup/system 2>&1 >> ${log_file}
exitcode "create dir"
if [ ! -n "`cat /etc/group | grep ${backup_group}`" ]; then
echo -e "\nTest Backup Group"
groupadd ${backup_group} 2>&1 >> ${log_file}
exitcode "create backup group"
fi
echo -e "\nBackuping etc"
tar cfz /backup/system/etc.tar.gz /etc 2>&1 >> ${log_file}
exitcode "backuping etc"
echo -e "\nBackuping confixx"
tar cfz /backup/system/confixx.tar.gz /var/www/confixx /usr/local/confixx 2>&1 >> ${log_file}
exitcode "backuping confixx"
echo -e "\nStopping mysql"
/etc/init.d/mysql stop 2>&1 >> ${log_file}
exitcode "stopping mysql"
echo -e "\nBackuping mysql"
tar cfz /backup/system/mysql.tar.gz /var/lib/mysql/confixx /var/lib/mysql/mysql 2>&1 >> ${log_file}
exitcode "backuping mysql"
echo -e "\nStarting mysql"
/etc/init.d/mysql start 2>&1 >> ${log_file}
exitcode "starting mysql"
echo -e "\ncopieng dirs"
cp -R /backup/excludes/ /backup/scripts /backup/system 2>&1 >> ${log_file}
exitcode "copieng dirs to temp"
echo -e "\nBackuping system"
tar -C /backup/system -c -z -f /backup/system.tar.gz . 2>&1 >> ${log_file}
exitcode "backuping system"
echo -e "\ndelete temp dir"
rm -r /backup/system 2>&1 >> ${log_file}
exitcode "delete temp dir"
if [ "${ftp_upload}" = yes ]; then
echo -e "\nftp upload system backup"
ncftpput -V -u ${ftp_user} -p ${ftp_pass} ${ftp_ip} ${ftp_dir} /backup/system.tar.gz 2>&1 >> ${log_file}
exitcode "ftp upload system backup"
fi
# users
echo -e "\nbackuping users"
for nr in `ls ${web_dir} | grep web\[0-9\] | sed 's/web//'`
do
echo -e "\ncreate web${nr} temp dir"
test -d /backup/web${nr} || /bin/mkdir /backup/web${nr} 2>&1 >> ${log_file}
exitcode "create web${nr} temp dir"
echo -e "\ncreate web${nr} exclude file"
test -e /backup/excludes/exclude_${nr}.list || /bin/touch /backup/excludes/exclude_${nr}.list 2>&1 >> ${log_file}
exitcode "create web${nr} exclude file"
echo -e "\nbackuping web${nr} html"
tar -X /backup/excludes/exclude_${nr}.list -C ${web_dir}/web${nr}/html -c -z -f /backup/web${nr}/web${nr}_html.tar.gz . 2>&1 >> ${log_file}
exitcode "backuping web${nr} html"
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
echo -e "\nmysqldump web${nr}_db${db}"
mysqldump ${mysql_opt} -uroot -p${mysql_pass} -hlocalhost usr_web${nr}_${db} > /backup/web${nr}/web${nr}_db${db}.sql 2>> ${log_file}
exitcode "mysqldump web${nr}_db${db}"
done
if [ "`ls /backup/web${nr} | grep web${nr}_db | wc -w`" -gt 0 ]; then
echo -e "\ncreating web${nr} sql tar"
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 2>&1 >> ${log_file}
exitcode "creating web${nr} sql tar"
echo -e "\ndeleting web${nr} sql"
rm /backup/web${nr}/web${nr}_db*.sql 2>&1 >> ${log_file}
exitcode "deleting web${nr} sql"
fi
fi
if [ "${mail_backup}" = yes ] && [ "`ls ${mail_dir} | grep web${nr}p | wc -w`" -gt 0 ] || [ "${1}" = mail ] && [ "`ls ${mail_dir} | grep web${nr}p | wc -w`" -gt 0 ]; then
echo -e "\ncreating web${nr} mail backup"
ls ${mail_dir} | grep web${nr}p | xargs tar -C ${mail_dir} -c -z -f /backup/web${nr}/web${nr}_mail.tar.gz 2>&1 >> ${log_file}
exitcode "creating web${nr} mail backup"
fi
if [ "${2}" = logs ]; then
echo -e "\ncreating web${nr} log backup"
tar cfz /backup/web${nr}/web${nr}_logs.tar.gz ${web_dir}/web${nr}/log ${web_dir}/web${nr}/.configs ${web_dir}/web${nr}/phptmp ${web_dir}/web${nr}/files 2>&1 >> ${log_file}
exitcode "creating web${nr} log backup"
fi
echo -e "\ncreating complete web${nr} backup file"
tar -C /backup/web${nr} -c -z -f /backup/web${nr}.tar.gz . 2>&1 >> ${log_file}
exitcode "creating complete web${nr} backup file"
echo -e "\ndeleting web${nr} temp dir"
rm -r /backup/web${nr} 2>&1 >> ${log_file}
exitcode "deleting web${nr} temp dir"
if [ "`groups web${nr} | grep ${backup_group} | sed 's/web[0-9]\{1,3\}.:.ftponly.//'`" = ${backup_group} ] && [ "${1}" != mail ]; then
echo -e "\ncopieng web${nr} backup to download dir"
cp /backup/web${nr}.tar.gz ${web_dir}/web${nr}/files/web${nr}.tar.gz 2>&1 >> ${log_file}
exitcode "copieng web${nr} backup to download dir"
echo -e "\nsetting web${nr} file permissions"
chown web${nr}:ftponly ${web_dir}/web${nr}/files/web${nr}.tar.gz 2>&1 >> ${log_file}
exitcode "setting web${nr} file permissions"
if [ "${ftp_upload}" = yes ]; then
echo -e "\nuploading web${nr} file"
ncftpput -V -u ${ftp_user} -p ${ftp_pass} ${ftp_ip} ${ftp_dir} /backup/web${nr}.tar.gz 2>&1 >> ${log_file}
exitcode "uploading web${nr} file"
fi
fi
done
echo -e "\nerror_count=${error_count}"
echo -e "\n${date} `date '+%T'` - BACKUP Complete! \n"
if [ "${send_log}" = yes ] && [ "${error_count}" -gt "0" ]; then
echo -e "\nsend backup log to ${mail_to_log}"
(echo "To: ${mail_to_log}"
echo "From: Backup Agent <${mail_from_log}>"
echo "Subject: Backup log ${date}"
echo ""
echo "Backup Log ${date}"
cat ${log_file}
echo ""
) | 2>&1 ${sendmail} -oi ${mail_to_log}
fi
We use essential cookies to make this site work, and optional cookies to enhance your experience.