1 Ensure that you are in the home directory (if not, run: cd/home)
2 New Backup folder (command: mkdir backup)
3) New Script backup.sh
VI backup.sh
Copy the following code (after replication: wq! to save):
Copy Code code as follows:
#!/bin/bash
#你要修改的地方从这里开始
Mysql_user=root #mysql用户名
mysql_pass=123456 #mysql密码
Ftp_user=vmvps #ftp用户名
ftp_pass=123456 #ftp密码
Ftp_ip=ftp.vmvps.com #ftp地址
Ftp_backup=backup #ftp上存放备份文件的目录, this should be built on its own FTP
Web_data=/home/wwwroot #要备份的网站数据
#你要修改的地方从这里结束
#定义数据库的名字和旧数据库的名字
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 -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.gz
Cd/home/backup
#导出数据库, a database corresponds to a compressed file
For DB in '/usr/local/mysql/bin/mysql-u$mysql_user-p$mysql_pass-b-n-e ' show DATABASES | Xargs '; Todo
(/usr/local/mysql/bin/mysqldump-u$mysql_user-p$mysql_pass ${db} | gzip-9–> ${db}.sql.gz)
Done
#压缩数据库文件为一个文件
Tar zcf/home/backup/$DataBakName/home/backup/*.sql.gz
Rm-rf/home/backup/*.sql.gz
#压缩网站数据
Tar zcf/home/backup/$WebBakName $WEB _data
#上传到FTP空间, delete data from FTP space 5 days ago
FTP-V-N $FTP _ip << End
User $FTP _user $FTP _pass
Type binary
CD $FTP _backup
Delete $OldData
Delete $OldWeb
Put $DataBakName
Put $WebBakName
4 How does the test script run
SH backup.sh
5 Add permissions to the script
chmod +x backup.sh
6) Add daily autorun command (automatically run 00:00 every day)
Crontab-e
* * * */home/backup.sh