Share the fully automatic daily backup method of VPS website data: rsync + crontab

888u

Last update at :2023-12-12,Edit by888u

The most important thing in building a website is data. Yesterday, the website was down for 9 hours due to a switch problem in the datacenter (details). However, because the website data was not backed up every day, the only data available was the data from 4 days ago. I have gained wisdom after learning, and now I have decided to back up my website data regularly every day. The technology stack used is rsync + crontab to back up the website data to another VPS. Here I will share how to fully automatically back up the website data every day.

1. Material preparation

In addition to the server for building your own website, you also need a VPS as a backup server. The line of this VPS is not particularly important. Data can be transferred slowly and stably. It is best to find a VPS to build your website with you. The transfer speed between VPS is ok as a backup server.

Determine the hard disk capacity of the backup server based on the amount of data on your website. The data that the website needs to back up includes database data (usually tens of megabytes) and website data. If you have a lot of pictures, it may be uploaded to G. This site’s The website data size is about 1G, so if you back up every day, the hard disk capacity of the backup server needs to be greater than 30G (if you back up every day and clean the data once a month).

This site is placed on the Hong Kong VDS of GigsGigsCloud. I use HostDare as the backup server. Yesterday’s transfer speed was 13.81MB/s. If you don’t have a suitable backup server yet, you can also consider HostDare, whose hard drives are relatively large: HostDare: Los Angeles CN2 GIA, Los Angeles CN2 GT plans are all sorted out, with a lifetime discount of 25%.

2. Data synchronization settings

Data transmission between two VPS uses rsync. Here is a transmission command:

rsync -avP data.zip root@104.11.11.11:/root/backup

The purpose of this command is to transfer the data.zip on the website building VPS to the /root/backup directory on the backup VPS (IP is 104.11.11.11).

The backup idea is very clear:

Here is a written packaging and transmission script, saved as backup.sh:

DATE=$(date +%Y%m) BLOG_DIR='/home/wwwroot/www.pianyivps.com' BLOG_NAME='www.pianyivps.com' MYSQL_USER='user' MYSQL_PASS='password' DB='db' BACKUP_DIR='/root/backup' BACKUP_IP_DEST='104.11.11.11' BACKUP_DIR_DEST='/root/backup' if [ ! -d ${BACKUP_DIR} ]; then mkdir ${BACKUP_DIR} fi cd ${BACKUP_DIR} mysqldump -u$MYSQL_USER -p$MYSQL_PASS ${DB} > ${DB}_${DATE}.sql echo "SQL size: $(wc -c ${DB}_${DATE}.sql | awk '{print $1}')" zip -r ${BLOG_NAME}_${DATE}.zip ${BLOG_DIR} > /dev/null echo "WWW size: $(wc -c ${BLOG_NAME}_${DATE}.zip | awk '{print $1}')" zip ${BLOG_NAME}_${DATE}_ALL.zip ${BLOG_NAME}_${DATE}.zip ${DB}_${DATE}.sql > /dev/null rsync -avP ${BLOG_NAME}_${DATE}_ALL.zip root@${BACKUP_IP_DEST}:${BACKUP_DIR_DEST} rm -rf ${DB}_${DATE}.sql ${BLOG_NAME}_${DATE}.zip ${BLOG_NAME}_${DATE}_ALL.zip echo ${BLOG_NAME}_${DATE}_DONE

In order to prevent the need to enter the root password every time you transfer data, you can configure the SSH public key first.

1. Generate SSH public key on the website VPS

The command to generate an SSH public key is as follows, just press Enter all the way. The generated public key is /root/.ssh/id_rsa.pub:

ssh-keygen

2. Save the SSH public key of the website building VPS to the backup VPS

Rename the id_rsa.pub just now, for example here I renamed it to pianyivps.pub, then copy the file to /root/.ssh of the backup VPS, and save the changed public key to the backup VPS:

catpianyivps.pub >> authorized_keys

After that, you no longer need to enter the root password when using rsync.

3. Scheduled task settings

The last scheduled task is to use the crontab command. For example, I want to back up website data at 0:30 every day:

# Execute the /root/backup.sh script at 0:30 every day, and the execution log is saved in /root/backup.log 30 0 * * * /root/backup.sh >> /root/backup.log 2>&1

OK, you're done, your data is automatically backed up every day, so you can sit back and relax.

Recommended site search: check IP address, domain name price, which Hong Kong space is better, how to cancel domain name registration, Korean independent server, online server website, Hong Kong IP, Ministry of Industry and Information Technology registration website, free US hosting, check IP address,

Share the fully automatic daily backup method of VPS website data: rsync + crontab

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码