前言

最近为了halo博客的可用性,操作了很多,包括如下

  • h2数据库迁移到mysql
  • mysql主从复制
  • 自动备份
  • nginx判断主站状态,若主站宕机,则转换到备用站点
  • 将备份打包成docker

挑几个来写写

自动备份

export.sh

#!/bin/bash  
  
source /etc/profile  
  
  
  
if [ ! -f /root/qiniu_halo_backup/.halo2 ]; then  
  mkdir /root/*****/.halo2  
fi  
  
# 将docker中的halo文件备份  
docker cp halo3:/root/.halo2/indices /root/*****/.halo2/indices  
docker cp halo3:/root/.halo2/keys /root/*****/.halo2/keys  
docker cp halo3:/root/.halo2/logs /root/*****/.halo2/logs  
docker cp halo3:/root/.halo2/plugins /root/*****/.halo2/plugins  
docker cp halo3:/root/.halo2/themes /root/*****/.halo2/themes  
# 将docker中的halo文件备份  
  
# 打包成tar.gz  
tar -czvf /root/*****/*****.tar.gz /root/*****/.halo2 >>/dev/null 2>&1  
  
  
# 发布到存储桶上  
python3 /root/*****/main.py  

# 清理文件
rm -rf /root/*****/*****.tar.gz  
  
rm -rf /root/*****/.halo2

main.py

from qiniu import Auth, put_file, etag, CdnManager  
import os  
  
current_path = os.path.split(os.path.realpath(__file__))[0]  
access_key = "*****"  
secret_key = "*****"  
q = Auth(access_key, secret_key, 180)  
  
  
def upload():  
  bucket = "*****"  
  key = "*****"  
  token = q.upload_token(bucket, key)  
    localfile = f"{current_path}/{key}"  
  ret, info = put_file(token, key, localfile, version='v2')  
    print(info)  
    try:  
        assert ret['key'] == key  
        assert ret['hash'] == etag(localfile)  
    except TypeError:  
        upload()  
  
  
def refresh():  
    cdn_manager = CdnManager(q)  
    urls = [  
        'https://*****/*****.tar.gz' # 刷新的url  
  ]  
    refresh_url_result = cdn_manager.refresh_urls(urls)  
    print(refresh_url_result)  
  
  
if __name__ == "__main__":  
    upload()  
    refresh()

将备份打包成docker镜像

Dockerfile

FROM eclipse-temurin:17-jre as builder

WORKDIR application

RUN wget https://github.com/halo-dev/halo/releases/download/v2.12.2/halo-2.12.2.jar \

&& ln -sf /usr/share/zoneinfo/$TZ /etc/localtime \

&& echo $TZ > /etc/timezone \

&& wget https://*****/*****.tar.gz \

&& tar -zxvf *****.tar.gz -C ~ \

&& rm -rf *****.tar.gz

COPY application.yaml ./

ENTRYPOINT ["java", "-jar", "halo-2.12.2.jar"]

构建镜像

cd /path/to/Dockerfile

docker login --username=xxxx <私有源地址>
docker build . -t <私有源地址>/twoonefour/halo:latest
docker push <私有源地址>/twoonefour/halo:latest

然后找个免费镜像站,部署一下,再将nginx配置改为主站不可用时跳转到该容器就行了

mysql主从复制

上命令速通

主服务器

vim /etc/my.cnf

[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log # log-bin一般为 /var/log/mysql/mysql-bin.log
#设置不要复制的数据库
binlog-ignore-db=mysql
#设置需要复制的数据库
binlog-do-db=halo

:wq保存

重启一下数据库

systemctl restart mysqld

mysql -u root -p
# 登入mysql
mysql> CREATE USER 'halo_replication_user'@'%' IDENTIFIED BY 'xxxxxxxxx';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'halo_replication_user'@'%';
# 创建主从复制的用户
mysql>flush tables with read lock;
# 读锁
mysql>SHOW MASTER STATUS\G;
# 记下当前信息,如我的

#*************************** 1. row ***************************
#             File: mysql-bin.000001
#         Position: 4504302
#     Binlog_Do_DB: halo
# Binlog_Ignore_DB: mysql
#Executed_Gtid_Set:
#1 row in set (0.00 sec)

# position 和 file是要记住的
# 分别是4504302和mysql-bin.000001

mysql>exit;

备份数据库

mysqldump -p --databases halo > halo.sql

从服务器

vim /etc/my.cnf

[mysqld]
server-id = 2

重启mysql服务

systemctl restart mysqld

mysql -u root -p halo < halo.sql 
# 登入mysql并导入数据库数据,若没有halo数据库则先登入进去创建halo数据库,如下
mysql>create database halo;
mysql>exit;
# 再执行
mysql -u root -p halo < halo.sql 

# 接下来启动从服务
mysql -p
# 登入
mysql>STOP SLAVE;
# 停止从服务
mysql>CHANGE MASTER TO MASTER_HOST = 'your_host_ip',MASTER_USER = 'halo_replication_user',MASTER_PASSWORD = 'xxxxxxxxx',MASTER_LOG_FILE = 'mysql-bin.000001',MASTER_LOG_POS = 4504302;
# 修改主服务器连接参数
mysql>START SLAVE;
# 启动从服务
mysql>SHOW SLAVE STATUS \G;
# 查看从服务运行信息

*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: ***
                  Master_User: halo_replication_user
                  Master_Port: ****
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 5119527
               Relay_Log_File: 677d49e76fda-relay-bin.000004
                Relay_Log_Pos: 5116554
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 5119527
              Relay_Log_Space: 5120081
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: daad61e0-b29a-11ec-8670-00163e1ba001
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 10
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set, 1 warning (0.24 sec)

只需要看这两个就行了

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

说明已经主从复制跑起来了

最后一定记得解锁主服务器的锁

mysql>unlock tables;