Linux VPS Automatic Backup: Script upload FTP and rsync incremental backup

Source: Internet
Author: User
Tags chmod vps rsync

☆☆☆ Daily automatic backup site data and database upload ftp☆☆☆

This way, the main is a script (including compressed site data and database, upload), and then use the cron command every day in a specified period of time to run, see the script code (script information needs to set itself)

The code is as follows Copy Code

#!/bin/bash
#以下信息请自行修改

Mysql_user=root #mysql用户名
mysql_pass=123456 #mysql密码
Mail_to=admin@zrblog.net #数据库发送到的邮箱
Ftp_user=zrblog #ftp用户名
ftp_pass=123456 #ftp密码
Ftp_ip=*.*.*.* #ftp地址
Ftp_backup=zrblog-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 with 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
#发送数据库到Email, if the database is compressed too large, please comment this line
#echo "Topics: 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

The above script according to your VPS information set, Save as autobackuptoftp.sh, and then upload to the VPS root directory.

Please make sure your VPS can send a letter properly, otherwise please install (yum install SendMail mutt), then give the script permissions

The code is as follows Copy Code

chmod +x/root/autobackuptoftp.sh

Next we can test whether this script is working properly, SSH login to manually run the script to see if the relevant FTP location received data. If everything works, we'll use cron to automate this script every day.

The code is as follows Copy Code

Crontab–e

Add the following tasks

The code is as follows Copy Code

* * */root/autobackuptoftp.sh

Task time please set yourself, such as the above code of 00 03, is 3 points, as far as possible to choose your site visitors at least the time period, such as the early morning.

Attached script Download: http://www.zrblog.net/sh/AutoBackupToFtp.sh

Note If the backup folder does not exist or is not established when you run the script, manually create the backup folder in the VPS home directory.

☆☆☆ II, rsync incremental backup ☆☆☆

Rsync incremental backup, requires 2 VPS, let them do data synchronization between. For example, our site in VPS A, then a is the server side, with another VPS B to do an incremental backup, then B is the client.

This is just a simple b for a backup, A and B are the same Linux system.

First download server-side and client profiles: server-Side Profiles--Client configuration files

"Server-side Settings"

First, the download back to the server-side configuration file uploaded to the/etc directory of the VPS, and then modify the relevant file settings (you can also modify and upload first)

① modified/etc/rsyncd/rsyncd.conf A total of 5 places

The code is as follows Copy Code

1, address = 1.2.3.4 #第七行修改为服务器IP

2, hosts allow=4.3.2.1 #第二十行修改为客户端IP

3, [Zrblog] #第三十四行自定义命名

4, Path =/home/wwwroot #第三十五行修改同步目录

5. Auth users = Zrblog #第三十八行指定用户名

② Modify/etc/rsyncd/rsyncd.secrets user and password, and give permission (this user and password please specify)

The code is as follows Copy Code
chmod 600/etc/rsyncd/rsyncd.secrets
③ running on the server Rsyns

The code is as follows Copy Code
/usr/bin/rsync--daemon--config=/etc/rsyncd/rsyncd.conf

"Client Setting"
Upload the downloaded client configuration file to the/root directory of the VPS, and then set up the related files
① Modify the/root/pass file, fill in the Password settings and server-side consistent, and give permissions.

The code is as follows Copy Code
chmod 600/root/pass
② Modify the/root/rs.sh file, set the rsync path and server-side consistent, and give permissions.

The code is as follows Copy Code
Rsync-avzp--delete--password-file=/root/pass Zrblog@1.2.3.4::zrblog/home/wwwroot
User name in #上面第一个zrblog为服务器端/etc/rsyncd/rsyncd.secrets
#1.2.3.4 for server-side IP
#第二个zrblog为服务器端设定的自定义命名
#最后的/home/wwwroot is a synchronized Web site Directory
Then, set the permissions

The code is as follows Copy Code
chmod +x/root/rs.sh
When you have completed the above settings, please run/root/rs.sh on the client manually to detect the success of the normal sync.
Add that if you use the Iptalbes, please note that rsync requires open port 873, otherwise it is not synchronized.
If you fail to achieve what you want, repair it according to your specific circumstances.
Finally, make a scheduled task to run the synchronization on the client on time (using the crontab command).
Believe that, as long as you use the above two automatic backup of any one, no doubt for your website data security provides a strong guarantee.

S

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.