#!/bin/bash
#author: 505056357@qq.com
echo "Backup Job Start"
#设置要备份的网站目录, that is, the root directory of the Web site
File=/home/wwwroot/default
#设置本地备份目录, for storing the packaged files
backpath=/home/wwwroot/bkdata/
Dbuser=root
#数据密码, remember to enclose the entire password in single quotes and double quotes if the cipher has single quotes, hehe
Dbpwd= ' 123456 '
Dbname=car
domain=192.168.0.187
User=ftpuser
Userpwd= ' 123456 '
bkdate=$ (Date +%y%m%d)
Sqlfile_sql=${dbname}_${bkdate}.sql
Sqlfile_gz=${dbname}_${bkdate}.tar.gz
sqlfp=${backpath}${bkdate}/$sqlfile _sql
If [-E $backpath ${bkdate}]
Then
Echo $backpath is exists.
Else
Mkdir-p $backpath ${bkdate}
Fi
#执行数据库备份,/apps/mysql/bin/mysqldump Modify the path to your actual environment
/apps/mysql/bin/mysqldump-u $dbuser-p$dbpwd $dbname > ${SQLFP}
#对备份之后的数据库文件压缩, or compress directly by adding parameters at the time of database backup
Tar zcf $backpath ${bkdate}/$sqlfile _gz-c ${backpath}${bkdate} ${sqlfile_sql}
#删除未压缩SQL的文件
Rm-f ${SQLFP}
#生成的文件名格式: Web_ Site Directory name _20130419.tar.gz
Fn=web_${file##*/}_${bkdate}.tar.gz
Tar zcf $backpath ${bkdate}/$fn-C ${file%/*} ${file##*/}
#以下操作为FTP自动连接并把本地文件上传到异地服务器上
/usr/bin/lftp ${domain} << End
User ${user} ${userpwd}
LCD ${backpath}
Mirror-r ${bkdate}
Exit
End
echo "Backup Job done"