Case one: Shell+cron realize MySQL automatic backup and automatically delete n days before backup
| The code is as follows |
Copy Code |
#!/bin/sh Dump=/usr/local/mysql/bin/mysqldump out_dir=/home/ldl/xxx/backup/ Linux_user=ldl Db_name=ldl Db_user=ldl Db_pass=xxx #How much of the days of backup most Days=1 #Core of script CD $OUT _dir Date= ' Date +%y_%m_%d ' Out_sql= "$DATE. SQL" Tar_sql= "Mysql_$date.tar.gz" $DUMP--default-character-set=utf8--opt-u$db_user-p$db_pass $DB _name > $OUT _sql TAR-CZF $TAR _sql./$OUT _sql RM $OUT _sql Chown $DB _name:nobody./$TAR _sql Find./-name "mysql*"-type f-mtime + $DAYS-exec rm {}; +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
Mysqldump do not have to explain it, note-uuser-ppass This can be, cannot have spaces.
| The code is as follows |
Copy Code |
Find./-name "mysql*"-type f-mtime + $DAYS-exec rm {};
|
This sentence is mainly on the-mtime-mtime +5 represents 5 days before the file, unified Delete
My operating system is CentOS 5.4, as long as the script copy to/etc/cron.daily, and set to the same permissions can be executed, no longer add crontab.
CentOS automatically backs up the Web site and database scripts and uploads them to FTP
7th Update, the export of each database is a separate, and then compressed in a compressed package, now you just have to modify the following options can be used.
| The code is as follows |
Copy Code |
| #你要修改的地方从这里开始 Mysql_user=root #mysql用户名 mysql_pass=123456 #mysql密码 Mail_to=cat@hostloc.com #数据库发送到的邮箱 Ftp_user=cat #ftpl用户名 ftp_pass=123456 #ftp密码 Ftp_ip=imcat.in #ftp地址 Ftp_backup=backup #ftp上存放备份文件的目录, this should be built on its own FTP Web_data=/home/www #要备份的网站数据 #你要修改的地方从这里结束 |
Don't forget to build the directory: Mkdir-p/home/backup
If your site data is greater than 5G less than 10G, it will be a little difficult to compress ...
If less than 5G, there is no problem ... But all this is to look at the performance of the VPS just ...
--------------------------------------------------------------------
Work is boring, if do not find something to do, I will fall asleep ...
So this script is born.
The main functions are: (the title is not said!!!!), the details are, automatically back up the Web site files and databases and uploaded to the FTP space, the local retention of 3 days backup, in the remote FTP space to retain 5 days backup.
The database backup uses sends to the email and the FTP space, the double backup, guarantees the data security.
First install email send component
| The code is as follows |
Copy Code |
Yum Install SendMail Mutt
|
The script is as follows:
| The code is as follows |
Copy Code |
| #!/bin/bash #你要修改的地方从这里开始 Mysql_user=root #mysql用户名 mysql_pass=123456 #mysql密码 Mail_to=cat@hostloc.com #数据库发送到的邮箱 Ftp_user=cat #ftp用户名 ftp_pass=123456 #ftp密码 Ftp_ip=imcat.in #ftp地址 Ftp_backup=backup #ftp上存放备份文件的目录, this should be built on its own FTP Web_data=/home/www #要备份的网站数据 #你要修改的地方从这里结束 #定义数据库的名字和旧数据库的名字 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 with a compressed file /usr/local/mysql/bin/mysql-u$mysql_user-p$mysql_pass-b-n-e ' Show DATABASES ' | Xargs > Mysqldata Sed-i ' S/information_schema//g ' mysqldata Sed-i ' S/mysql//g ' mysqldata For DB in ' cat mysqldata '; Todo (/usr/local/mysql/bin/mysqldump-u$mysql_user-p$mysql_pass--databases ${db}> ${db}.sql) Done #压缩数据库文件为一个文件 Tar zcf/home/backup/$DataBakName/home/backup/*.sql.gz Rm-rf/home/backup/*.sql Mysqldata #发送数据库到Email, if the database is compressed too large, please comment this line echo "Subject: Database Backup" | Mutt-a/home/backup/$DataBakName-S "Content: Database Backup" $MAIL _to #压缩网站数据 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 Bye End |
Download Address: http://file.111cn.net/upload/2014/5/AutoBackupToFtp.sh
Download script to add permissions to the script:
| The code is as follows |
Copy Code |
chmod +x autobackuptoftp.sh |
Using Crontab to automate backups, under SSH,
Crontab-e don't have this order? Please see CentOS installation crontab and how to use it
Enter the following content:
| The code is as follows |
Copy Code |
* * * */home/autobackuptoftp.sh |
This enables the automatic backup of Web site files and databases uploaded to FTP space every 00:00
Note: The crontab command is not installed by default in CentOS, and we need to install crontab before we can use it.
Install Crontab:
| The code is as follows |
Copy Code |
Yum Install Crontabs
|
Description
/sbin/service crond Start//boot service
/sbin/service Crond Stop//Off service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration
See Crontab Service Status: Service Crond status
Manually start the Crontab service: Service Crond start
To see if the Crontab service is set to boot, execute command: NTSYSV
Join the boot auto start:
Chkconfig Crond on