﻿#!/bin/bash
MYSQL_USER=webback
MYSQL_PASS=123456
MAIL_TO=whhack.com@gmail.com
FTP_USER=whhack
FTP_PASS=123456
FTP_IP=whhack.com
FTP_backup=web.com/day


DataBakName=Data_$(date +"%Y%m%d").tar.gz
WebBakName=Web_$(date +%Y%m%d).tar.gz
OldData=Data_$(date -d -5day +"%Y%m%d").tar.gz
OldWeb=Web_$(date -d -32day +"%Y%m%d").tar.gz

rm -rf /home/backup/day/Data_$(date -d -3day +"%Y%m%d").tar.gz /home/backup/day/Web_$(date -d -3day +"%Y%m%d").tar.gz
cd /home/backup/day

for db in `/usr/local/mysql/bin/mysql -u$MYSQL_USER -p$MYSQL_PASS -B -N -e 'SHOW DATABASES' | xargs`; do
    (/usr/local/mysql/bin/mysqldump -u$MYSQL_USER -p$MYSQL_PASS ${db} --skip-lock-tables | gzip -9 - > ${db}.sql.gz)
done

zip -r /home/backup/day/web001.zip /usr/local/mysql/var/web/
zip -r /home/backup/day/sql000.zip /home/backup/day/*.sql.gz
tar zcf /home/backup/day/$DataBakName  /home/backup/day/*.zip
rm -rf /home/backup/day/*.zip
rm -rf /home/backup/day/*.sql.gz

echo "数据库:web-mysql-beifen_$(date +"%Y%m%d")" | mutt -a /home/backup/day/$DataBakName -s "数据库:web-mysql-beifen_$(date +"%Y%m%d")" $MAIL_TO


WEB_DATA1=/home/wwwroot/web.com/data/attachment/forum/$(date -d -1day +"%Y%m/%d")/
WEB_DATA2=/home/wwwroot/web.com/data/attachment/album/$(date -d -1day +"%Y%m/%d")/
WEB_DATA3=/home/wwwroot/web.com/data/attachment/portal/$(date -d -1day +"%Y%m/%d")/
zip -r /home/backup/day/forum.zip $WEB_DATA1
zip -r /home/backup/day/album.zip $WEB_DATA2
zip -r /home/backup/day/portal.zip $WEB_DATA3

tar zcf /home/backup/day/$WebBakName /home/backup/day/*.zip
rm -rf /home/backup/day/*.zip

echo "每日附件:web-web-beifen_$(date +"%Y%m%d")" | mutt -a /home/backup/day/$WebBakName -s "每日附件:web-web-beifen_$(date +"%Y%m%d")" $MAIL_TO

/usr/local/php/sbin/php-fpm restart

ftp -v -n $FTP_IP << END
user $FTP_USER $FTP_PASS
type binary
cd $FTP_backup
delete $OldData
delete $OldWeb
put $DataBakName
put $WebBakName
bye
END
