Website data is the life of the webmaster. The recent DS failure caused a large number of user data losses, which has sounded the alarm for us: it is very necessary to be diligent in backup. It is better to put eggs in several baskets at any time. It’s good to put them in one! Today, Hao VPS will share 2 ways to schedule to automatically back up .
rsync
☆☆☆1. Daily automatic backup upload website data and database to FTP☆☆☆
This method is mainly a script (including compressing website data and database , upload), and then use the cron command to run it in the specified time period every day. Please see the script code below (the information in the script needs to be set by yourself)

#!/bin/bash
# Please modify the following information yourself
MYSQL_USER=root         <x2 >          #mysqlusername
MYSQL_PASS=123456                     <x2 > #mysqlpassword
[email protected]                  #The email address to which the database is sent
FTP_USER=vpsok#ftp username
FTP_PASS=123456   < x2>                     #ftp password
FTP_IP=*.*.*.*        <x2 >                 # ftp address
FTP_backup=vpsok-backup             < x2>            #The directory where backup files are stored on ftp. You need to install this on ftp yourself. Built
WEB_DATA=/home/wwwroot           #Website data to be backed up
#Modify the above information by yourself
#Define the name of the database and the name of the old database
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
#Delete local data 3 days ago
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
#Export Database, one database and one compressed file
for db in `/usr/local/mysql/bin/mysql -u$MYSQL_USER -p$MYSQL_PASS -B -N -e 'SHOW DATABASES' | xargs`; do
(/usr/local/mysql/bin/mysqldump -u$MYSQL_USER -p$MYSQL_PASS ${db} | gzip -9 – > ${db}.sql.gz)
done
#The compressed database file is A file
tar zcf /home/backup/$DataBakName /home/backup/*.sql.gz
rm -rf /home/backup/*.sql.gz
#Send the database to Email, if The database is too large after compression, please comment this line
#echo "Subject: Database Backup" | mutt -a /home/backup/$DataBakName -s "Content: Database Backup" $MAIL_TO
#Compress website data
tar zcf /home/backup/$WebBakName $WEB_DATA
#Upload to FTP space, 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

Set the above script according to your VPS information, save it as AutoBackupToFtp.sh, and then upload it to the root directory of the VPS.
Please ensure that your VPS can send mail normally, otherwise please install (yum install sendmail mutt), and then give permissions to the script

chmod +x /root/AutoBackupToFtp.sh

Next, we can test whether the script is working properly. Log in with ssh to manually run the script and check whether the relevant FTP location has received data. If everything is normal, we can use cron to automatically run this script every day.

crontab –e

Add the following tasks

00 03 * * * /root/auto backup to FTP.Yes

Please set the time of the task yourself. For example, 00 03 in the above code is 3 o'clock. Try to choose a time period when your website has the fewest visitors, such as early morning.
Attached script download: https://www.vpsok.net/download/sh/AutoBackupToFtp.sh
【Notes】 If when running the script, it prompts that the backup folder does not exist or cannot be To create, please manually create a backup folder in the home directory of the VPS.
☆☆☆2. RSYNC Incremental backup ☆☆☆
RSYNC Incremental backup , 2 units are required VPS allows data synchronization between them. For example, if our website is placed on VPS A, then A is the server; if we use another VPS B for incremental backup, then B is the client.
What we are talking about here is simply that B is a backup for A, and both A and B are on the same Linux system.
First download the server and client configuration files: Server configuration file - Client configuration file
[Server settings]
First, upload the downloaded server-side configuration file to the /etc directory of the VPS, and then modify the settings of the relevant files (you can also modify them first and then upload them)
① Modify /etc/rsyncd/rsyncd.conf 5 places in total

1. address = 1.2.3.4    #The seventh line is changed to server IP
2. hosts allow=4.3.2.1   #The twentieth line is changed to client IP
3. [vpsok]     #Line 34 Custom naming
4. path = /home/wwwroot    #Line 35 modifies the synchronization directory
5. auth users = vpsok     #Line 38 specifies the user name

② Modify the user and password of /etc/rsyncd/rsyncd.secrets and grant permissions (please specify this user and password yourself)

chmod 600 /etc/rsyncd/rsyncd.secrets

③Run rsyns on the server

/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf

【Client Settings】
Upload the downloaded client configuration file to the /root directory of the VPS, and then set the relevant files
① Modify the /root/pass file and fill in the password The settings are consistent with those on the server side, and permissions are given.

chmod 600 /root/pass

② Modify the /root/rs.sh file, set the rsync path to be consistent with the server side, and grant permissions.

rsync -avzP --delete  --password-file=/root/pass [email protected]::vpsok /home/wwwroot
#The first vpsok above is the server The username
#1.2.3.4 in /etc/rsyncd/rsyncd.secrets is the server IP

#The second vpsok is the custom name set on the server side
#The last /home/wwwroot is the synchronized website directory
Then, set the permissions

chmod +x /root/rs.sh

After completing the above settings, please manually run /root/rs.sh on the client to check whether the synchronization is successful.
If it doesn't get what you want, fix it on a case-by-case basis.
Finally, make a scheduled task to run synchronization on time on the client (using the crontab command).
I believe that as long as you use any of the above two automatic backups, it will undoubtedly provide a strong guarantee for the security of your website data.
Finally, the above tutorial materials come from the Internet, and I am especially grateful to Xiaoye Blog for his great assistance. If friends encounter problems during implementation, you can leave a message and give feedback to work together
to troubleshoot and resolve.


Hong Kong/United States/Domestic High Speed ​​VPS

Tags: RSYNC, Incremental backup, Auto backup

window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":["mshare","kaixin001","tsina"," tsohu","tqq","renren","qzone","weixin","bdysc","bdxc","tqf","tieba","douban","bdhome","sqq","thx" ],"bdPic":"","bdStyle":"0","bdSize":"24"},"slide":{"type":"slide","bdImg":"2","bdPos" :"left","bdTop":"100"},"selectShare":{"bdContainerClass":null,"bdSelectMiniList":["tsina","tqq","tsohu","qzone","renren", "weixin","mshare"]}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg .share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
postid
8821

Leave a Reply