#/bin/bash BackuPath=/opt/backup/ WebPath=/usr/local/apache2/htdocs/ DATE=`date +%Y%m%d` WebBakFIle=web${DATE}.tar.bz2 DBFile=DB${DATE}.tar.bz2 DBuser='root' DBpassword='123' ftp_user="bbq" ftp_pass="qwe123" today=`date +%Y%m%d-%H%M` today_dir=$today mkdir $today_dir LogFile=/opt/backup/baklog.txt OldWebBak=web`date -d -5day +"%Y%m%d"`.tar.bz2 OldDbBak=DB`date -d -5day +"%Y%m%d"`.tzr.bz2 cd $WebPath pwd >> $LogFile tar jcf $WebBakFIle * | mv $WebBakFIle $BackuPath for db in `mysql -u$db_user -B -N -e 'show databases;'` do mysqldump -u$db_user --database $db>$db.$today.sql |mv $db.$today.sql $today_dir done tar zcf sql.$today.tar.gz $today_dir/* ls -la | grep 20141129 | awk ' {print $8"\t"$5/1024/1024"MB"}' >> $LogFile pwd ftp -v -n $FTPserver << END user $ftp_user $ftp_pass bin cd $FtpTargetPath ls -l cd web mkdir $today_dir cd $today_dir put $WebBakFIle cd .. cd mysql mkdir $today_dir cd $today_dir put sql.$today.tar.gz bye END rm -rf OldWebBak rm -rf OldDbBak if [ $?=1 ];then echo $(date +"%y-%m-%d %H:%M:%S") ftp had been put file >> $LogFile else echo $(date +"%y-%m-%d %H:%M:%S") filed!!! >> $LogFile fi echo "hello,GOOD BOY!!!" | mutt -a baklog.txt -s "date" -18507656226@163.com
每日凌晨4点自动备份:
[root@localhost opt]# crontab -l
0 4 * * * bash /opt/echo.sh