Linux VPS Automatic scheduled backup MySQL database sent to the mailbox implementation script

Source: Internet
Author: User
Tags tmp folder vps mysql backup mysql database vps server
Now, we choose the host station or deployment project, more and more think VPS, server cost-effective will be much higher, such as we often see annual pay more than 10 U.S. dollars, more than 20 U.S. dollars VPS server. Compared to the traditional virtual host is much cheaper, but Chiang always stressed that if we are ordinary personal blogs, personal sites, if the virtual host can meet the needs of the proposal or to use the virtual host. Because the virtual host webmaster maintenance cost is lower, and stability and security than VPs, Server strong.

Because, most VPS servers are rogue, all the environment configuration, Web site deployment, data security needs of our own to transport the dimension. Especially for many users of Linux server Novice, the old Chiang encountered a lot of options to choose a VPS after the Reference Tutorial deployment Web site environment to build a station, until the server failed, the host ran, and then feel the need to back up the data, because the regular backup site data is how important, even if there are problems We can also temporarily transfer to another server.

There are a number of ways to back up Web site files and databases, and we can choose scripts, tools from the environment, and complete manual backups. Like our general blogs, personal sites, data updates are not large, you can choose to manually back up regularly, or to regularly back up the MySQL database, because most of the time we do not need to change the site's programs and templates. (Of course, upload image updates can also be backed up regularly, mainly or database)

In this article, Chiang modified a simple script, mainly used to automatically and regularly back up the MySQL database, and then sent to our designated mailbox, so that at least to ensure that the database is complete, because most of the time the site files usually rarely change, the site files can be based on actual changes in the spacing between backup time.

First, MySQL database backup preparation

The code is as follows Copy Code

Yum Install mutt Sendmail-y

Here we need to make sure that Mutt and SendMail are installed in the server to send mail.

Second, script modification parameters

The code is as follows Copy Code

#!/bin/sh
mysql_user= "Database User"
Mysql_name= "Database name"
mysql_pwd= "Database Password"
#要备份网站的数据库信息
d= ' Date ' +%y-%m-%d '
#定义备份邮件标题, you can tell what day it is.
t= "MySQL backup-$d"
#发送邮件标题和内容, this is secondary, mainly the database in the annex
Backname=backup_mysql_$d
#数据库文件名称
backpath=/tmp/
#备份数据库存放路径, we can also periodically sftp to download, this script will not be deleted periodically
Mysqldump-u${mysql_user}-p${mysql_pwd} $mysql _name > $BackPath $backname.sql
#导出数据库
Tar cfP $BackPath $backname.tar $BackPath $backname.sql
#压缩数据库包, this saves space.
echo "$t" |mutt-s "$t" Backup@itbulu.com-a $BackPath $backname.tar
#用mutt发送邮件, send to "backup@itbulu.com" in our designated mailbox.
Exit
#退出当前脚本

We can modify the script file parameter section above to actually need to back up the site parameter content.

The code is as follows Copy Code

Alternate script file: http://soft.itbulu.com/backup/back.sh

Third, the use of script

We can name the script back.sh and put it in the directory where we know it, for example, to the root directory.

The code is as follows Copy Code

chmod +x back.sh
./back.sh

Modify the permissions, and then execute the script to try, we can see in the/tmp/folder more than 2 files, one is the database SQL file, one is compressed after the tar file. If we have no problem with the mailbox set up, we should wait for a while will prompt to send back the database.

Chiang Kai-shek used here is the QQ Post office, or can receive the MySQL database backup files.

Implementation of scheduled backup settings

1, CRONTAB-E

Open the Timing editor file.

2. Add script

The code is as follows Copy Code

1 * * 5 sh/root/back.sh

Set up 5 1 o'clock in the morning 30 days a week to perform a script backup and send mail. Specific crontab usage can refer to here (Linux vps/server crontab scheduled task settings and crontab writing format).

V. BACKUP DATABASE Script Summary

1, the current script is relatively simple, but the function can be implemented. Will temporarily back up the database files in the TMP folder, we can manually go to the server to download a copy.

2, after the old Chiang again to improve the script, such as backup server Backup in a few days after the automatic deletion. There is absolutely no need to use this method for backing up whole-site files, because some sites are very large, and it's really troublesome to send them to the mail in this way. Because most of the time, our database file is the most critical.

Of course, this script old Chiang is available after detection, but does not ensure that everyone is suitable, so if we are used in real-world projects, we recommend that we use it before testing the environment.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.