Not afraid to lose data VPS Automatic Backup Ultimate Guide

Source: Internet
Author: User
Keywords Mysql vps

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

Nearly half a year, has lost 5 of Web site data, mostly due to the damage caused by VPS hard disk, RAID10 for speed is very not insurance

The last 2 times were directspace and BUYVM.

So, must backup, make good VPS ready to lose data ready

Yesterday spent a whole day, all the current stations do a daily synchronization, the following to share the actual operation methods.

Reprint please indicate the original source http://www.21andy.com/blog/20120520/2026.html

There are a lot of information on Rsync online, but most of the I would like to see you dizzy, the following I use examples to explain, the focus of the place I will elaborate, I hope that everyone will use.

Below, assuming that the site's VPS for a, storage backup VPS for B, the system is CentOS

Backup method for B timed to A pull data to back up

A, VPS a specific deployment above

1. Install rsync

Yum-y Install Rsync

Add rsync to boot

Echo ' rsync--daemon ' >>/etc/rc.d/rc.local

2. Set up rsync password

Echo ' Your username: your password ' >/etc/rsyncd.scrt

chmod 600/ETC/RSYNCD.SCRT

Here's username and password that will be used in VPS B

3. Configure Rsync

Vim/etc/rsyncd.conf

Put the following #后面是我的注释

Download: rsyncd.conf

UID = root

GID = root

Use chroot = no

Read Only = yes

Max 50x15 = 10

Port = 873

PID file =/var/run/rsyncd.pid

Lock file =/var/run/rsync.lock

#log file =/var/log/rsync.log # I don't want to log log

Log format =%t%a people%m%f%b

Syslog facility = Local3

Timeout = 300

[WWW]

Path =/var/www/

Comment = 21andy.com

Ignore errors

Read Only = yes

List = no

Auth users = Andy

LSA file =/etc/rsyncd.scrt

#exclude = 21andy.com/blog/cache/#不需要备份的目录, I use the Exclude from method to exclude

Exclude from =/etc/rsync_exclude.txt

Hosts allow = Backup server IP

Hosts deny = *

4. Exclude directories that are not backed up

Vim/etc/rsync_exclude.txt

Enter a directory that is not backed up, one for each line, not an absolute path, and must use the relative path of path in the above configuration file, such as

21andy.com/blog/cache/

21andy.com/manual/

This exclusion file has more advanced +-writing, we do not need, simple enough to use the Exclude from method, the advantage is to add no need to back up the content, easy to add, and do not need to restart the rsync process

5. Create a script to restart Rsync

vim/root/rsyncd_restart.sh

Put the following

Kill-9 ' Cat/var/run/rsyncd.pid '

Rm-f/var/run/rsyncd.pid

Rm-f/var/run/rsyncd.lock

Rsync--daemon

chmod 600/root/rsyncd_restart.sh

chmod +x/root/rsyncd_restart.sh

Now restart the rsync process directly using/root/rsyncd_restart.sh

6. Back Up MySQL scripts

This script can back up multiple databases at the same time, with gzip compression, saved by date directory, 3 days before the backup will be automatically deleted

vim/root/mysql_backup.sh

Download: mysql_backup.sh

#!/bin/bash

# Please modify the following configuration information

mysql_user= "User" #MySQL备份用户

mysql_password= "Password" #MySQL备份用户的密码

mysql_host= "localhost"

Mysql_port= "3306"

mysql_charset= "UTF8" #MySQL编码

Backup_db_arr= ("DB1" "DB2") #要备份的数据库名称, multiple separated by spaces ("DB1" "DB2" "DB3")

Backup_location=/var/www/mysql #备份数据存放位置, please do not bring "/" at the end, this can remain the default, the program will automatically create a folder

Expire_backup_delete= ' on ' #是否开启过期备份删除 on as turned off

Expire_days=3 #过期时间天数 defaults to three days, which is only valid when Expire_backup_delete is turned on

# The Bank does not need to change the following

Backup_time= ' Date +%y%m%d%h%m ' #定义备份详细时间

backup_ymd= ' Date +%y-%m-%d ' #定义备份目录中的年月日时间

Backup_3ago= ' date-d ' 3 days ago ' +%y-%m-%d ' #3天之前的日期

backup_dir= $backup _location/$backup _ymd #备份文件夹全路径

Welcome_msg= "Welcome to use MySQL backup tools!" #欢迎语

# to determine whether MySQL boot, MySQL does not start the backup exit

mysql_ps= ' ps-ef |grep mysql |wc-l '

mysql_listen= ' netstat |grep listen |grep $mysql _port|wc-l '

if [$mysql _ps = 0]-o [$mysql _listen = = 0]]; Then

echo "Error:mysql is not running! Backup stop! "

Exit

Else

Echo $welcome _msg

Fi

# connected to MySQL database, unable to connect, backup exits

Mysql-h$mysql_host-p$mysql_port-u$mysql_user-p$mysql_password <

Use MySQL;

Select Host,user from user where user= ' root ' and host= ' localhost ';

Exit

End

Flag= ' echo $? '

If [$flag!= "0"]; Then

echo "Error:can ' t connect MySQL server! Backup stop! "

Exit

Else

echo "MySQL Connect ok! Please wait ...

# to determine if there is a database defined for the backup, or to start the backup if defined, or exit the backup

If ["$backup _db_arr"!= "];then

#dbnames =$ (cut-d ', '-f1-5 $backup _database)

#echo arr is (${backup_db_arr[@]})

For dbname in ${backup_db_arr[@]}

Do

echo "Database $dbname backup start ..."

' Mkdir-p $backup _dir '

' Mysqldump-h$mysql_host-p$mysql_port-u$mysql_user-p$mysql_password $dbname--default-character-set= $mysql _ CharSet | gzip > $backup _dir/$dbname-$backup _time.sql.gz '

Flag= ' echo $? '

if [$flag = = "0"];then

echo "Database $dbname success Backup to $backup _dir/$dbname-$backup _time.sql.gz"

Else

echo "Database $dbname backup fail!"

Fi

Done

Else

echo "Error:no database to backup! Backup stop

Exit

Fi

# Delete an expired backup if it's turned on

If ["$expire _backup_delete" = "on"-a "$backup _location"!= "];then

' Find $backup _location/-type d-o-type f-ctime + $expire _days-exec rm-rf {} \; '

echo "Expired backup data Delete complete!"

Fi

echo All database backup success! "You!"

Exit

Fi

chmod 600/root/mysql_backup.sh

chmod +x/root/mysql_backup.sh

All right, join crontab. Automatic backup every 00:00

* * * */root/mysql_backup.sh

At this point, the site is located on the VPs a deployment has been completed! Next, set up on the backup VPS B to pull the backup.

Second, the specific deployment of VPS B above

1. Install rsync

Yum-y Install Rsync

There is no need to join the boot up, because it is the client, not the server

2. Set up rsync password

Echo ' The password you set on a ' >/etc/rsync.pass

chmod 400/etc/rsync.pass

3. Test the synchronization

Build a place to store backups first

Mkdir-p/var/rsync/

Test the Sync

RSYNC-AVZP--delete--password-file=/etc/rsync.pass username @192.168.0.100::www/var/rsync/21andy.com/

This order, I'll explain a few points

-avzp is what, search my Site Introduction

--delete is to delete a file for example a, when sync, B will automatically delete that file

--password-file just VPS B in/etc/rsync.pass set that password, and VPS a/etc/rsyncd.scrt in the same password, so cron run, it does not need a password

"Username" in this command is the username in the/etc/rsyncd.scrt of VPS A

The 192.168.0.100 in this command is the IP address of VPS a

:: www, attention is 2: number, www for VPS a profile/etc/rsyncd.conf [www], meaning that according to the/etc/rsyncd.conf on a to synchronize the [WWW] section of the content, a: number, used for not according to the configuration file, Synchronize the specified directory directly

4. Join crontab daily 00:30 sync

* * * * RSYNC-AVZP--delete--password-file=/etc/rsync.pass username @192.168.0.100::www/var/rsync/21andy.com/>/dev/n Ull 2>&1

ok! This is done! Not afraid to lose data, automatic backup every day!

If you need to insure a little more, add a VPS C

c to sync B, double backup, which is not afraid to hang!

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.