Focus on cloud service provider activities
Notes on website operation and maintenance

Detailed explanation of Linux VPS host environment website and MYSQL database automatic scheduled backup script

From the beginning of learning to build a website, whether we use a free virtual host or a frugal paid host, or we need to switch to VPS or server storage in larger projects; Whether we are technical or not, or like Laozuo, when we see that netizens have their own websites, we are enthusiastic to build one for fun; No matter whether our website is large or small, profitable or unprofitable, we should learn basic self maintenance, especially the backup of website data.

We should not rely too much on and trust the backup provided by the host company. Even if we buy the products of expensive service providers, they may also cause data loss or crash due to various possibilities. We can choose regular manual backup or automatic backup with certain technology. Generally, the former is for personal websites with a small amount of data, The latter can implement automatic backup for sites with more projects that want automatic management.

stay Laozuo Blog There have been many reports about websites and VPS backups Some simple manual backup files and database exports, and some high-end synchronous incremental backups. For our users, we can choose a suitable one. After all, we only need to learn how to use and implement the results.

1、 Configure RSYNC to realize scheduled synchronous image VPS website backup and replace incremental backup files

2、 Linux VPS automatically synchronizes backup files/databases to the COPY storage site

3、 Free online backup of website files and databases with multiple backups

Because recently Lao Zuo was playing with the Godaddy economic host he bought before, he didn't need to build a website, but it can be used to back up the website storage in our VPS. Personally, the files backed up to his own host are much safer than those backed up by a third party. What do you think?

Among the numerous automatic backup scripts circulated on the Internet, Lao Zuo thinks that the automatic backup FTP upload script of the military brother is better. It can automatically pack the database and web page files in batches, and then upload them to the designated FTP host regularly. Therefore, in this article, the actual use of this script will be explained in detail.

First, preparations

We need to prepare an FTP host to ensure that it can be opened using the FTP tool link. Pay attention to the password. For example, this is the case after my host is connected by FTP.

 Check whether the FTP host is correct

First, we need to confirm whether the FTP host account is correct and get a backup directory. Here, we need HTTPDOCS. Not every host is the same. We must backup to this directory or a folder in it. (Some may be WWWROOT)

Secondly, we need to install lftp in the VPS environment for the application of uploading files.

yum -y install lftp

For example, execute the yum script above in the centos environment to run the installation. If it is debian, change it to apt get

Thirdly, we need to create a folder in the VPS environment for backup file storage, such as/www/backup. The principle is to first back up our website and database to/www/backup after executing the script, and then upload to the FTP space using the script LFTP.

Second, edit the backup script

https://soft.laozuo.org/backup/backup.sh

In the above script, we need to download it locally first, then edit it (do not edit it with Notepad), and use the editplus or subordinate editor. It needs to be explained one by one. Because our VPS path is different from the actual configuration, novices are often prone to make mistakes.

 Backup VPS script

Starting from the above, we must pay attention to modifying the configuration. Backup_Dir represents the folder path of our current website in VPS, which can be modified according to the actual situation. Here is the backup of two websites. If we only need to back up one website, remove one line. Note the following parameters 1 and 2, and the analogy of multiple websites.

The middle part of the MYSQL database information needs to be written with the MYSQL account of the ROOT information to ensure that the backup is complete. The following is the database name of the site to be backed up. If only one database is backed up, we will remove one line. If more than one database is added, pay attention to the parameters.

 VPS backup script FTP information

Here is also a key point. We need to fill in the FTP account we prepared during the preparation, especially FTP_BackupDir. We need to correspond to the file storage directory we see after opening the direct FTP tool. According to the actual situation, not every host company is the same.

 Backup VPS script path

Here we need to pay attention to two problems. One is that if we only need to back up one site, the redundant line should be removed. By default, the old Zuo writes two to distinguish between them. If there are multiple sites, the directory writing method should be backed up. And we need to pay attention to the directory problem. In this script, my directories are in the www, some are in home, and some are in var. They are different. We need to modify our paths according to our actual conditions. (Check carefully)

Third, deploy test backup

After the modification is OK, we upload it to the root directory in VPS. We can test whether it can be backed up.

sh /root/backup.sh

 Perform backup test

We can see the compression and upload process after the test backup.

Fourth, deploy automatic scheduled backup

We can implement scheduled backup by adding scripts to crontab. For example, we set the backup at 1 am every day.

crontab -e

Open the crontab file and add

0 1 * * * /root/backup.sh

The daily scheduled backup can be realized. If we do not need to do so frequently, we can also modify the scheduled time, such as the time of the week when the backup is performed. The prerequisite is that we need to have installed crontab. Look at the backup data I tested here.

 VPS customized backup test

In Laozuo's FTP space, you can see three days of backup data. The database and files are packaged separately, and can be downloaded locally if necessary.

In conclusion, if our site is small or not very busy, we can back up manually, and we can test the automatic backup settings on the test machine before deploying. Although the FTP backup script is relatively simple, it still needs to be tested and improved before deploying. We have seen any tutorial, even if Lao Zuo's test has been published, But when we use it ourselves, we also need to test to ensure that it is OK.

Vote for you
Domain name host preferential information push QQ group: six hundred and twenty-seven million seven hundred and seventy-five thousand four hundred and seventy-seven Get preferential promotion from merchants.
Like( zero )
Do not reprint without permission: Lao Zuo's Notes » Detailed explanation of Linux VPS host environment website and MYSQL database automatic scheduled backup script


Scan the code to follow the official account

Get more news about webmaster circle!
Entrepreneurship, operation and new knowledge