Copy the following script directly to/root/backup.sh
[Email protected] ~]# chmod +x/root/backup.sh
[Email protected] ~]# CRONTAB-E
XX * * */root/backup.sh
[Email protected] ~]# service Crond restart
[Email protected] ~]# chkconfig--level 2345 Crond on
#!/bin/bash# the place you want to modify starts here mysql_user=root #mysql用户名MYSQL_PASS = #mysql密码FTP_USER = #ftp用户名FTP_PASS = #ftp密码FTP_IP = #ftp地址FTP_backup = #ftp上存放备份文件的目录, you need to build web_data=/home/wwwroot on the FTP first #本地要备份的网站数据 #你要修改的地方从这里结束 if [!-f/usr/bin/ftp]; Then yum install ftp-yfiif [!-d/home/backup]; Then MKDIR/HOME/BACKUPFI #定义数据库的名字和旧数据库的名字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 #删除本地3天前的数据rm-RF/ home/backup/data_$ (date-d -3day + "%y%m%d"). tar.gz/home/backup/web_$ (date-d -3day + "%y%m%d"). Tar.gzcd/home/backup # Export 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 '; Do (/usr/local/mysql/bin/mysqldump-u$mysql_user-p$mysql_pass ${db}-Q--skip-lock-tables | gzip-9-> ${DB}.SQL.G Z) done #压缩数据库文件为一个文件tar zcf/home/backup/$DataBakName/home/backup/*.sql.gzrm-rf/home/backup/*.sql.gz #压缩网站数据tar zcf/home/backup/$WebBakName $WEB _data #上传到FTP空间, delete ftp space 5 days ago Data ftp-v-N $ Ftp_ip << enduser $FTP _user $FTP _passtype binarycd $FTP _backupdelete $OldDatadelete $OldWebput $DataBakNameput $ Webbaknamebyeend
Linux automatically backs up files and databases and uploads them to the specified remote FTP