To install the email sending component:
Yum Install SendMail Mutt
vi/root/autobackuptoftp.sh
#!/bin/bashmysql_user=root #mysql用户名MYSQL_PASS = 123456 #mysql密码 [email protected] #数据库发送到的邮箱FTP_USER =cat #ftp用户名FTP_PASS =123456 # FTP Password ftp_ip=192.168.2.100 #ftp地址FTP_backup =backup #ftp上存放备份文件的目录, this is going to have to own the FTP web_data=/home/www # The site data to be backed up # defines the name of the database and the name of the old database databakname=data_$ (date + "%y%m%d"). tar.gzwebbakname=web_$ (date +%y%m%d). tar.gzolddata=data_$ (date -d -5day + "%y%m%d"). tar.gzoldweb=web_$ (date -d -5day + "%y%m%d"). tar.gz# Delete the data rm -rf /home/backup/data_$ (date -d -3day + "%Y%m%d") 3 days ago. tar.gz /home/backup/web_$ (date -d -3day + "%y%m%d"). tar.gzcd /home/backup# Export the database, A database a compressed file for db in '/usr/local/mysql/bin/mysql -u$mysql_user -p$mysql_pass -b -N -e ' Show databASES ' | xargs ';d o (/usr/local/mysql/bin/mysqldump -u$mysql_user -p$ mysql_pass ${db} | gzip -9 - > ${db}.sql.gz) done# compress the database file to a file tar zcf /home/backup/$DataBakName /home/backup/*.sql.gzrm -rf /home/backup/*.sql.gz# Send database to email, if the database is compressed too large, please comment this line echo "topic: Database Backup" | mutt -a /home/backup/$DataBakName -s "Content: Database Backup" $MAIL _to# compress website data tar zcf /home/backup/$WebBakName $WEB _data# upload to FTP space, Delete FTP space 5 days ago Data ftp -v -n $FTP _ip << enduser $FTP _user $FTP _passtype binarycd $FTP _backupdelete $OldDatadelete $OldWebput $DataBakNameput $ Webbaknamebyeend
To add execute permissions to the script:
chmod +x/root/autobackuptoftp.sh
Automate daily operation with system crontab:
Crontab-e
Enter the following:
XX * * * */root/autobackuptoftp.sh
00 00 is time/hour, can be modified by itself, for example: 30 12 * * *, that is, 12.30 runs the script every day.
Linux automatic daily backup files and database upload FTP space