一、背景

某某项目,机房到期,需要迁移至其他机房; 此项目已经运行了3年多,fastdfs累计数据大概在250G 左右,现需要把旧的fastdfs数据迁移到新的fastdfs上;

采用scp物理迁移数据的方式,停机迁移。

image-20240906164022920

二、环境信息

机房 操作系统 IP 版本信息 备注
机房A centos7.9 172.16.10.28 fastdfs-v6.06 源码编译安装
机房B openeuler22.03 LTS SP4 172.22.33.201 fastdfs-v6.10.0,docker-27.1.1,docker-compose-v2.10.2 docker-compose部署

三、迁移前准备

3.1 在B机房,使用docker-compose部署fastdfs环境

docker,docker-compose 环境部署过程省略;

⚠️ 需要注意的是,fastdfs采用的宿主机网络直接通信的,请修改IP为宿主机IP地址

  • 创建fastdfs工作目录
vim /home/application/Middleware/fastdfs/docker-compose.yml
  • 编辑docker-compose.yml
version: '3'
services:
  fastdfs:
    image: srebro/fastdfs:6.10.0
    container_name: fastdfs
    restart: always
    volumes:
      - "/home/application/Middleware/fastdfs/fastdfs_data:/home/dfs/data"
    ports: 
      - "22122:22122"
      - "23000:23000"
    environment:
      FASTDFS_IPADDR: "172.22.33.201"
    network_mode: host
  • 运行docker-compose创建容器
$ docker-compose up -d
$ docker-compose ps
NAME                COMMAND              SERVICE             STATUS              PORTS
fastdfs             "/home/fastdfs.sh"   fastdfs             running
  • 模拟测试上传下载
#登录到fastdfs容器内部
$ docker exec -it fastdfs bash

#创建client.conf 配置文件
$ cat > /etc/fdfs/client.conf << EOF
base_path=/tmp
tracker_server=172.22.33.201:22122
EOF

#创建一个测试文件,模拟写入一些内容

$ cat > /tmp/test << EOF
hello, srebro.cn | 运维小弟
EOF

#使用fdfs_upload_file,上传测试文件
$ fdfs_upload_file /etc/fdfs/client.conf /tmp/test
group1/M00/00/05/rBYhyWbayPGAWUFtAAAAIAqGCgY7719471

#看到返回group1/M00/00/05/rBYhyWbayPGAWUFtAAAAIAqGCgY7719471, 在 fastdfs 数据目录上相应目录中均能查看到该文件

$ ls -l /home/dfs/data/00/05/rBYhyWbayPGAWUFtAAAAIAqGCgY7719471
-rw-r--r-- 1 root root 32 Sep  6 17:18 /home/dfs/data/00/05/rBYhyWbayPGAWUFtAAAAIAqGCgY7719471

$ cat /home/dfs/data/00/05/rBYhyWbayPGAWUFtAAAAIAqGCgY7719471
hello, srebro.cn | 运维小弟
  • 测试完没有问题之后,停掉新的fastdfs服务,准备接收迁移旧数据;

⚠️ 需要保持fastdfs_data 目录下内容为空,因为我们是采用直接物理拷贝的方式,把数据直接拷贝到fastdfs_data 目录下

$ cd /home/application/Middleware/fastdfs/
$ docker-compose down
[+] Running 1/1
⠿ Container fastdfs  Removed   

# 删除 fastdfs 数据目录下的所有内容
# 删除前的fastdfs 数据目录下的所有内容
$ ls -l /home/application/Middleware/fastdfs/fastdfs_data/
总用量 1056
drwxr-xr-x 258 root root 4096  5月 16 22:24 00
drwxr-xr-x 258 root root 4096  5月 16 22:24 01
drwxr-xr-x 258 root root 4096  5月 16 22:24 02
drwxr-xr-x 258 root root 4096  5月 16 22:24 03
drwxr-xr-x 258 root root 4096  5月 16 22:24 04
drwxr-xr-x 258 root root 4096  5月 16 22:24 05
drwxr-xr-x 258 root root 4096  5月 16 22:24 06
drwxr-xr-x 258 root root 4096  5月 16 22:24 07
drwxr-xr-x 258 root root 4096  5月 16 22:24 08
drwxr-xr-x 258 root root 4096  5月 16 22:24 09
drwxr-xr-x 258 root root 4096  5月 16 22:24 0A
drwxr-xr-x 258 root root 4096  5月 16 22:24 0B
drwxr-xr-x 258 root root 4096  5月 16 22:24 0C
drwxr-xr-x 258 root root 4096  5月 16 22:24 0D
drwxr-xr-x 258 root root 4096  5月 16 22:24 0E
...........................
-rw-r--r--   1 root root    2  9月  6 17:10 fdfs_storaged.pid
-rw-r--r--   1 root root    2  9月  6 17:10 fdfs_trackerd.pid
drwxr-xr-x 258 root root 4096  5月 16 22:24 FE
drwxr-xr-x 258 root root 4096  5月 16 22:24 FF
-rw-r--r--   1 root root   48  9月  4 16:40 storage_changelog.dat
-rw-r--r--   1 root root  240  9月  4 16:40 storage_groups_new.dat
-rw-r--r--   1 root root 1366  9月  4 16:40 storage_servers_new.dat
-rw-r--r--   1 root root 1083  9月  6 17:20 storage_stat.dat
-rw-r--r--   1 root root   23  9月  4 16:40 storage_sync_timestamp.dat
drwxr-xr-x   2 root root 4096  5月 16 22:24 sync
...........................



# 删除 fastdfs 数据目录下的所有内容
$ rm -rf  /home/application/Middleware/fastdfs/fastdfs_data/*

# 删除后的fastdfs 数据目录下的所有内容
$ ls -l /home/application/Middleware/fastdfs/fastdfs_data/
总用量 0

3.2 测试A-B机房网络是否联通

#A机房服务器上
[root@localhost ~]# ping 172.22.33.201 -c 8
PING 172.22.33.201 (172.22.33.201) 56(84) bytes of data.
64 bytes from 172.22.33.201: icmp_seq=1 ttl=63 time=0.365 ms
64 bytes from 172.22.33.201: icmp_seq=2 ttl=63 time=0.372 ms
64 bytes from 172.22.33.201: icmp_seq=3 ttl=63 time=0.389 ms
64 bytes from 172.22.33.201: icmp_seq=4 ttl=63 time=0.368 ms
64 bytes from 172.22.33.201: icmp_seq=5 ttl=63 time=0.372 ms
64 bytes from 172.22.33.201: icmp_seq=6 ttl=63 time=0.396 ms
64 bytes from 172.22.33.201: icmp_seq=7 ttl=63 time=0.292 ms
64 bytes from 172.22.33.201: icmp_seq=8 ttl=63 time=0.339 ms

--- 172.22.33.201 ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 6999ms
rtt min/avg/max/mdev = 0.292/0.361/0.396/0.037 ms


#B机房服务器上
[root@localhost 05]# ping 172.16.10.28 -c 8
PING 172.16.10.28 (172.16.10.28) 56(84) bytes of data.
64 bytes from 172.16.10.28: icmp_seq=1 ttl=63 time=0.260 ms
64 bytes from 172.16.10.28: icmp_seq=2 ttl=63 time=0.378 ms
64 bytes from 172.16.10.28: icmp_seq=3 ttl=63 time=0.393 ms
64 bytes from 172.16.10.28: icmp_seq=4 ttl=63 time=0.392 ms
64 bytes from 172.16.10.28: icmp_seq=5 ttl=63 time=0.356 ms
64 bytes from 172.16.10.28: icmp_seq=6 ttl=63 time=0.349 ms
64 bytes from 172.16.10.28: icmp_seq=7 ttl=63 time=0.363 ms
64 bytes from 172.16.10.28: icmp_seq=8 ttl=63 time=0.293 ms

--- 172.16.10.28 ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7184ms
rtt min/avg/max/mdev = 0.260/0.348/0.393/0.044 ms

四、停机迁移

4.1 登录A机房服务器上,停止🛑旧的fastdfs服务

#停止fastdfs服务
$ /etc/init.d/fdfs_storaged stop
Stopping fdfs_storaged (via systemctl):                    [  确定  ]

$ /etc/init.d/fdfs_trackerd stop
Stopping fdfs_trackerd (via systemctl):                    [  确定  ]


#查看是否还存在fdfs进程
$ ps -ef | grep fdfs | grep -v grep
#返回为空,表示fastdfs已经退出了

4.1 登录A 机房,旧的fastdfs服务器上

只需要拷贝数据目录下,两字符的目录,其他的不要

#切换到数据目录下
$ cd /home/data/fdfs/data

# 查看数据目录下所有两字符目录个数【包含子目录】
$ ls -l ./[0-9A-F][0-9A-F] | wc -l
66303

#scp拷贝所有数据到新的服务器上
scp -rp  /home/data/fdfs/data/[0-9A-F][0-9A-F] root@172.22.33.201:/home/application/Middleware/fastdfs/fastdfs_data/

image-20240906175728404

4.2 登录B 机房,新的fastdfs服务器上

# 验证新的fastdfs服务器上,数据目录下所有两字符目录个数【包含子目录】
$ cd /home/application/Middleware/fastdfs/fastdfs_data/
$ ls -l ./[0-9A-F][0-9A-F] | wc -l
66303

两边数据保持一致,✅ 完成数据的迁移

4.3 启动 新的fastdfs服务器

$ cd /home/application/Middleware/fastdfs

$ docker-compose up -d

$ docker-compose ps
NAME                COMMAND              SERVICE             STATUS              PORTS
fastdfs             "/home/fastdfs.sh"   fastdfs             running

五、业务端,测试新平台是否可用

image-20240906181256063

image-20240906181237339

六、其他

  • ⚠️注意事项: 注意FastDFS中有预留空间的概念,在tracker.conf中设置,配置项为:reserved_storage_space,为磁盘总空间的20%,请酌情设置reserved_storage_space这个参数,配合监控系统,及时关注磁盘剩余空间大小
  • Docker 环境下,FastDFS Tracker 必须使用 Host 网络,https://github.com/happyfish100/fastdfs/issues/224