Docker series uses Docker to set up a blog slave site based on WordPress and implement regular backup of master-slave site configuration

Preface

有这个想法还是因为上次更换了图床的域名,之后在批量修改wordpress数据库里图床地址的时候,因为sql命令在复制粘贴的时候出现了格式问题,导致mariadb里的wordpress数据库崩了,虽然后面靠登录到wordpress里用”WPvivid备份插件”提前做的备份恢复过来了,但是中途博客大概也down了30分钟。。。这还是当时wordpress运气好还能够登录,如果运气不好wordpress都不能登录就要折腾更长时间了,感觉定期使用mysqldump备份mariadb数据库还有有必要的,纳上日程吧(因为源站是架设在macmini上,以前我不太想在macmini上运行脚本)。

After thinking about it for a while, I feel that the safest way is to set up a redundant WordPress site (which can be deployed in the same home data center, or on a cloud host, or in the home data center of other friends in the same city (or other place), and then use backup plug-ins (such as WPvivid backup plug-in) to regularly back up the WordPress and database data to the redundant site. When needed, the latest source site backup data can be immediately imported to provide services. Combined with the hot standby source site function of CDN, it can also achieve: disaster recovery in the same city (other place), and hybrid deployment of local and cloud disaster recovery functions.

创建并初始化mariadb数据库(可选)

wordpress需要使用mariadb数据库(mysql也可以,但是有选择的情况下永远优先mariadb),因为我喜欢采用docker run方式来部署wordpress(采用docker-compose方式部署wordpress不需要单独创建mariadb),所以需要手动创建mariadb数据库并进行初始化配置(如果有现成的mariadb就直接初始化即可),创建新mariadb数据库的流程建议如下:

Create a new public-net bridge (optional)

docker network create public-net

Note:
This step is not required, but because the non-default bridge public-net needs to be referenced in both the mariadb container and the wordpress container so that wordpress can directly access mariadb using the container name, it needs to be created in advance (if there are other existing non-default bridges, you can also reuse them).

In fact, you can achieve the same purpose by using the –link parameter to directly link mariadb in WordPress. However, in order to facilitate management and operation and maintenance of more containers in the future (for example, creating different bridges to connect containers in different areas), try not to use the –link parameter. Because when there are more containers, linking everywhere may confuse yourself and make operation and maintenance inconvenient. Therefore, it is best to develop a good habit of using the –net parameter from the beginning.

Create a new mariadb database (optional)

If there is no mariadb database container in the environment or you do not want to reuse the existing one, you need to create a new one.

Create a directory on the host that needs to be mounted into the mariadb container

mkdir -p /docker/mariadb/db

Create mariadb container

The command to build the mariadb database in docker run format is as follows:

docker run --name=mariadb01 -d --restart=always --net=public-net \ -p 3306:3306 \ #Map the host's port 3306 to the container's port 3306 -v /docker/mariadb/db:/var/lib/mysql \ #Mount the previously created host directory to the specified directory inside the container -e MARIADB_ROOT_PASSWORD=123 \ #Specify the password corresponding to the database root user, which you can modify by yourself mariadb:10.11

Initialize the mariadb database

Please refer to my other article for details:Tips and tricks: Create a new empty database and grant permissions to corresponding usersIn the following text, it is assumed that the initialization database name, the corresponding username and password are all wordpress.

Deploy WordPress and initialize

使用docker run方式部署wordpress

创建宿主机上需要挂载到wordpress容器中的目录:

mkdir -p /docker/wordpress/html

The command to build wordpress in docker run format is as follows:

docker run --name=wordpress -d --restart=always --net=public-net \
    -p 8080:80 \ #映射宿主机8080端口到容器的80端口,请大家根据实际环境选择宿主机端口
    -v /docker/wordpress/html:/var/www/html \ #映射宿主机上创建的目录到容器内指定路径
    wordpress

Note: Please delete the comment after #

使用docker-compose方式部署wordpress

对于不想单独创建数据库并初始化的朋友,可以直接采用docker-compose方式来部署wordpress,按照如下步骤操作:

创建宿主机上需要挂载到wordpress容器中的目录:

mkdir -p /docker/wordpress

创建docker-compose.yml:

vim /docker/wordpress/docker-compose.yml

然后将以下内容粘贴进docker-compose.yml并保存:

version: '3.0'
 
services:
  db:
    image: mariadb:10.11
    restart: unless-stopped
    environment:
      MARIADB_ROOT_PASSWORD: 123 # 按需修改
      MARIADB_DATABASE: wordpress
      MARIADB_USER: wordpress
      MARIADB_PASSWORD: wordpress # 按需修改
    volumes:
      - './db:/var/lib/mysql'
    networks:
      - default
 
  app:
    image: wordpress:latest
    restart: unless-stopped
    ports:
      - 8080:80  # 按需修改。与防火墙开放端口一致。
    environment:
      WORDPRESS_DB_HOST: db  # 如果需要使用命名容器,使用 'mariadb-wp'
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress # 按需修改
    volumes:
      - './app:/var/www/html'
    depends_on:
      - db
    networks:
      - default
 
  redis:
    image: redis:alpine
    restart: unless-stopped
    volumes:
      - ./redis-data:/data
    networks:
      - default
 
networks:
  default:
    name: public-net

然后拉起服务:

cd /docker/wordpress
docker-compose up -d

Initialize WordPress

usehttp://host IP:8080Log in, select Simplified Chinese as the language, and click Continue in the red box in the lower right corner:

image.png

点击下图红框中的”现在就开始”开始初始化:
image.png

按照初始化数据库时候配置的参数进行填写,最后点击做下的”提交”按钮开始初始化wordpress库:
image.png

If the previous configurations are like this, the following success interface appears:
image.png

点击右下角红框中的”运行安装程序”,就进入wordpress一些关键信息的填写,包括站点标题、用户名密码和管理员邮箱等,填写完毕以后点击左下角红框中的”安装”:
image.png

至此,wordpress初始化工作终于完成了,点击左下角红框中的”登录”进入wordpress并开始正式配置。
image.png


Note: If it is a completely new site, then you will enter the step of formally configuring WordPress. I will use an article to summarize some experience of getting started with WordPress, including plug-ins, theme configuration, etc. This article will not involve it. In fact, I am only half-baked in this area.


If you need to publish to the public Internet through a reverse proxy, you can refer to my other two articles:Docker series uses Docker to build its own reverse proxy based on NPMandLinux panel series configure reverse proxy and use non-443 port for publishing.

Install and enable the WPvivid backup plugin

如红框所示,在”插件”界面点击红框中的”添加新插件”:

image.png

Search in the search box on the upper rightWPvivid Backup Plugin,然后点击红框中的”立即安装”:
image.png

点击红框中的”启用”:
image.png

Enter the WPvivid configuration interface:
image.png

Finally, we get to the point. After that, you need to configure WPvivid on the source site and the main site respectively.

Install FTP server software on the target site (optional)

The main job of the WPvivid backup plugin on the source site is to back up the WordPress data and related library files in the database of the source site to local or remote storage. In this article, the best way is to back up directly to the directory of the WPvivid plugin on the target site via FTP, so that the target site can quickly import information directly. Of course, this is not necessary, and you can back up to any remote storage, such as:

image.png

Just using other remote storage requires two more steps: downloading the backup from the remote storage and uploading it to the WPvivid plug-in directory of the target site. These two steps don’t take much more time (just a little slow), so I said the content of this section is optional.

The way to install FTP software at the target site has a lot to do with the operating system environment of the target site. My target system is a Debian 12 virtual machine with the Baota Linux panel installed, so I will use my environment as an example to demonstrate the installation of FTP. For other environments, please study it by yourself. It's just about installing an FTP server.

在宝塔面板的”软件商店”中搜索并安装”Pure-Ftpd”:

image.png

然后在”FTP”点击”添加FTP”用以添加目标站点上的ftp用户及主目录:
image.png

The key is that the root directory of the user in the red box points to:
image.png

The path is:

/docker/wordpress/html/wp-content

Why is the path above? In fact, you can see it in the configuration interface of WPvivid:

image.png

因为后面在WPvivid通过ftp的方式添加远程存储的时候不能直接用”/”指定根位置,所以这里把FTP用户的根目录指向了/wp-content,方便后面添加的时候用/wpvividbackups的方式指定根目录。

After successfully adding the FTP user, please confirm whether the root path is correct:
image.png

To connect to the target site in FTP passive mode, the pure FTP configuration needs to be modified as follows:

image.png

PassivePortRange:
Define the port range that the source site ftp client can connect to when the ftp server is working in passive mode (please modify according to the actual situation), because it needs to be configured on the target site firewall (if enabled). What is more complicated here is that if the target site is set up on a cloud host, these ports need to be allowed in both the cloud host provider's security policy and the cloud host's own security policy. For example, if you are using a cloud host on Tencent Cloud, you first need to:
image.png

Then allow access to these ports through the ufw firewall on the cloud host Debian system itself.

ForcePassiveIP:
This is very important. If it is the same intranet environment, you need to specify the intranet IP address of the target site WordPress host; if it is a cloud host, you can directly use the public IP.

After configuration, you can use the FTP client in passive mode to access and confirm whether the FTP server on the target host can run normally.

Note:
The active and passive modes of ftp are actually quite interesting. Once you understand them, you can understand many network failures. I suggest you study them carefully if you are interested. Everyone's ftp environment may be different. If there is a problem, you may need to analyze the cause yourself. If it doesn't work, you can only use other methods.

In addition, if combined with the IP addresses provided by virtual LAN software such as TailScale, you can form a disaster recovery and hot standby solution with hosts in the same city, in different locations, or even anywhere in the world.

Source site WPvivid configuration

Adding Remote Storage

Fill in the information according to the target site FTP configuration information in the previous section:

image.png

The path is not the absolute path of the target site host, but the relative path to the root directory set by the ftp user. This is very important and it took me a long time to figure it out.

If the connection is successful, it will be displayed under the storage options below:
image.png

Schedule backup

根据自己实际需求在计划标签里选择备份频率及备份内容,然后选择”发送文件到远程存储”,然后选择左下角红框中的”保存修改”:

image.png

The default scheduled backup will upload the backup data of the source site to the target site and then delete the local backup. If you want to keep the backup generated by the scheduled task on the source site, you need to check the option in the red box under the Settings tab:
image.png


Note: This method has many limitations. Although it can be used, it is no longer recommended. The reasons and alternative methods are discussed in the last part.


如果想要在计划时间之外手动配置,可在”备份&恢复”标签下进行:

image.png

You can back it up once here so that you can view it on the target site.

Target site WPvivid configuration

其实目标站点不需要做什么配置,只需要在备份&恢复标签下点击红框中的”扫描已上传备份或接收备份”,然后在下方出现的备份中选择点击还原即可,因为我们上面在源站点备份了一次,所以点击下方红框中的”扫描已上传备份或接收备份”按钮,正常应该可以看到上面源站的备份数据:
image.png

Summarize

In fact, the key to this method is that the source site uploads the backup file directly to the backup directory of the target site's WPvivid plug-in through FTP. In this way, the target site only needs to scan to immediately see the configuration file of the source site and restore it immediately. Even if the wordpress of the source site cannot be opened at all, the access to wordpress can be restored in a very short time. However, even if it is not through FTP but other remote storage methods, there is only one more step of downloading and manually transferring to the target site. In fact, it does not take much longer, but the technical perception is not as good as this FTP method.

In addition, after the slave site is published through the reverse proxy, the hot backup source site can be pointed to this slave site on the CDN to achieve disaster recovery for the WordPress site. Of course, in this case, the slave site is best deployed on a cloud host or off-site. For example, I deploy it in my home data center, so that it will be over when there is a power outage.

After using it for a while, I have eliminated the use of FTP for remote backup. There are two reasons:
1. Only one remote node can be set
In addition to a hot backup WordPress site in the intranet, I now also have a mirror site on the Tencent Cloud server. There may be more in the future, and backing up one remote node is not enough.
2. Verification is required when restoring
When I use FTP to back up the configuration, I have to enter the FTP server information (IP, port number, username and password) when restoring it at the remote site, which is quite annoying.

So the idea is still the same, but I changed the implementation method. Instead of sending the regularly backed-up configuration directly to the remote slave site via FTP, I used Syncthing's multi-point folder synchronization method to implement it.
将主站点WPvivid定期备份的文件从”发送文件到远程存储”改为”保存备份到本地”:

image.png

Then use syncthing to synchronize the regularly backed-up files in the WPvivid local directory directly to the same directory of the WPvivid plugin on other sites. This method is perfect. For detailed syncthing configuration methods, please refer to my other article:Docker series: A detailed tutorial on how to synchronize multiple folders using Docker based on syncthing.

The content of the blog is original. Please indicate the source when reprinting! For more blog articles, you can go toSitemapUnderstand. The RSS address of the blog is:https://blog.tangwudi.com/feed, welcome to subscribe; if necessary, you can joinTelegram GroupDiscuss the problem together.
No Comments

Send Comment Edit Comment


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠(ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ°Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
Emoticons
Emoji
Little Dinosaur
flower!
Previous
Next
       
error:
en_US
Spring Festival
hapiness