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 methods of scheduled, automatic backup of .
![]()
☆☆☆1. Daily automatic backup of website data and database upload to FTP☆☆☆
This method is mainly a script (including compressing website data and database, uploading), and then using the cron command to run it at a specified time period every day. Please see the script code below (the information in the script needs to be set by yourself)
#!/bin/bash #以下信息请自行修改 MYSQL_USER=root #mysql用户名 MYSQL_PASS=123456 #mysql密码 [email protected] #数据库发送到的邮箱 FTP_USER=vpsok #ftp用户名 FTP_PASS=123456 #ftp密码 FTP_IP=*.*.*.* #ftp地址 FTP_backup=vpsok-backup #ftp上存放备份文件的目录,这个要自己得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 #导出数据库,一个数据库一个压缩文件 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 #压缩数据库文件为一个文件 tar zcf /home/backup/$DataBakName /home/backup/*.sql.gz rm -rf /home/backup/*.sql.gz #发送数据库到Email,如果数据库压缩后太大,请注释这行 #echo "主题:数据库备份" | mutt -a /home/backup/$DataBakName -s "内容:数据库备份" $MAIL_TO #压缩网站数据 tar zcf /home/backup/$WebBakName $WEB_DATA #上传到FTP空间,删除FTP空间5天前的数据 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 make sure 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: http://www.zrblog.net/sh/AutoBackupToFtp.sh
[Notes]If you are prompted that the backup folder does not exist or cannot be created when running the script, please manually create the backup folder in the home directory of the VPS.
☆☆☆2. RSYNC Incremental backup ☆☆☆
RSYNC Incremental backup requires 2 VPS to synchronize data 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 is mentioned 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 side 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 it first and then upload it)
①Modify /etc/rsyncd/rsyncd.conf in 5 places
1、address = 1.2.3.4 #第七行修改为服务器IP 2、hosts allow=4.3.2.1 #第二十行修改为客户端IP 3、[vpsok] #第三十四行自定义命名 4、path = /home/wwwroot #第三十五行修改同步目录 5、auth users = vpsok #第三十八行指定用户名
② 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
③在服务器上运行rsyns
/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
【Client settings】
将下载的客户端配置文件上传至VPS的/root目录,然后对相关文件设定
①修改/root/pass文件,填入密码设定与服务器端一致,并赋予权限。
chmod 600 /root/pass
②修改/root/rs.sh文件,设定rsync路径与服务器端一致,并赋予权限。
rsync -avzP --delete --password-file=/root/pass [email protected]::vpsok /home/wwwroot #上面第一个vpsok为服务器端/etc/rsyncd/rsyncd.secrets中用户名 #1.2.3.4为服务器端IP #第二个vpsok为服务器端设定的自定义命名 #最后的/home/wwwroot为同步的网站目录
然后,设定权限
chmod +x /root/rs.sh
完成以上设定后,请手动在客户端运行/root/rs.sh,检测是否正常同步成功。
补充一点,如果你使用了iptalbes,请注意rsync需要开放端口873,不然也是没法同步的。
若未能达到你想要的,就根据具体情况修缮。
最后,做一个计划任务,在客户端按时运行同步(使用crontab命令)。
相信,只要您使用了上面两种自动备份中的任何一种,都无疑为您的网站数据安全提供了一份有力的保障。以上教程资料来于网络,参考 https://wiki.vpsmm.com/rsync/