Mastodon 站点迁移

换个新服务器,记录一下迁移过程。

配置系统

配置 ssh-key

登入新服务器后进行如下操作:

1
2
mkdir -p ~/.ssh
nano ~/.ssh/authorized_keys

将生成的ssh-rsa公钥粘贴入其中,随后通过ssh-key密钥方式登录。
修改配置:

1
nano /etc/ssh/sshd_config

找到 PasswordAuthentication 一行,将其前面的 # 删掉 (取消注释),在后面将 yes 改成 no
然后

1
systemctl restart sshd

然后开一个新终端测试公钥登录,确认能进再关闭旧会话。

安装常用命令

1
2
sudo apt update
sudo apt install wget rsync python3 git curl vim ufw -y

配置防火墙

首先:

1
2
sudo ufw allow OpenSSH
sudo ufw enable

打开防火墙,随后打开 80443 端口:

1
2
sudo ufw allow http
sudo ufw allow https

然后可以通过 sudo ufw status 检查防火墙状态,会看到 80 和 443 端口的显示。

安装 dockerdocker-compose

参考官网教程
Set up Docker’s apt repository:

1
2
3
4
5
6
7
8
9
10
11
12
13
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Install the Docker packages:

1
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

check:

1
2
docker --version
docker compose version

The Docker service starts automatically after installation. To verify that Docker is running, use:

1
sudo systemctl status docker

Some systems may have this behavior disabled and will require a manual start:

1
sudo systemctl start docker

Verify that the installation is successful by running the hello-world image:

1
sudo docker run hello-world

拉取 Mastodon 镜像

拉取镜像:

1
2
3
mkdir -p /home/mastodon/mastodon
cd /home/mastodon/mastodon
docker pull melocery/mastodon:latest # 此处拉取我自己的镜像,非官方版本,修改过字数上限、主题等

下载适配的配置文件,本站仍为 v3.5.19,下载对应版本:

1
wget https://raw.githubusercontent.com/mastodon/mastodon/v3.5.19/docker-compose.yml

打开配置文件:

1
nano docker-compose.yml

把 web、streaming、sidekiq 的 image 改为刚才下载的镜像。

配置 Mastodon

直接复制旧服务器上的配置文件:

1
rsync -avz -e "ssh -i ~/.ssh/id_rsa_migration" root@旧服务器IP:/home/mastodon/mastodon/.env.production ./.env.production

由于 docker 版本更新,配置文件中的 DB 和 redis 要修改为

1
2
DB_HOST=db
REDIS_HOST=redis

数据库和 Redis 文件

先在新服务器上生成 ssh key,在 /home/mastodon/mastodon/ 下执行:

1
ssh-keygen -t ed25519 -C "mastodon-migration" -f ~/.ssh/id_rsa_migration

passphrase 回车留空即可。打开旧服务器上的 authorized_keys

1
nano ~/.ssh/authorized_keys

在文件末尾粘贴新服务器的公钥。
停止旧服务器上的 Mastodon 服务:

1
docker-compose down

在新服务器测试能否顺利从旧服务器复制文件,在新服务器运行:

1
2
rsync -avzn -e "ssh -i ~/.ssh/id_rsa_migration" root@旧服务器IP:/home/mastodon/mastodon/postgres14 /home/mastodon/mastodon
rsync -avzn -e "ssh -i ~/.ssh/id_rsa_migration" root@旧服务器IP:/home/mastodon/mastodon/redis /home/mastodon/mastodon

这是 dry-run (-n),只显示会复制哪些文件,不真正传输。确认 dry-run 没问题后,运行:

1
2
rsync -avz -e "ssh -i ~/.ssh/id_rsa_migration" root@旧服务器IP:/home/mastodon/mastodon/postgres14 /home/mastodon/mastodon
rsync -avz -e "ssh -i ~/.ssh/id_rsa_migration" root@旧服务器IP:/home/mastodon/mastodon/redis /home/mastodon/mastodon

文件会传到 /home/mastodon/mastodon/postgres14/home/mastodon/mastodon/redis。传输完成后,给文件夹赋权:

1
2
chown -R 70:70 ./postgres14
chown -R 70:70 ./redis

媒体文件

在旧服务器 Mastodon 用户下执行清理命令,移除远程媒体文件:

1
2
docker-compose run --rm -e RAILS_ENV=production web bin/tootctl media remove
docker-compose run --rm -e RAILS_ENV=production web bin/tootctl media remove-orphans

从新服务器复制 public 文件夹:

1
2
3
4
mkdir -p ./public/system
rsync -azv -e "ssh -i ~/.ssh/id_rsa_migration" \
--exclude='cache' \ # 排除缓存文件夹
root@旧服务器IP:/home/mastodon/mastodon/public/system/ ./public/system/

赋权:

1
chown 991:991 -R ./public

构建用户首页时间流

在新服务器上:

1
2
3
4
cd /home/mastodon/mastodon
docker compose down
docker compose run --rm web bin/tootctl feeds build
docker compose up -d

nginx 配置

在域名网站把域名的 DNS 设置指向的 IP 地址改为新服务器地址。然后安装 nginx:

1
sudo apt install nginx -y

从旧服务器直接把之前站点的配置复制过来:

1
2
3
rsync -avz -e "ssh -i ~/.ssh/id_rsa_migration" root@旧服务器IP:/etc/nginx/sites-available/your.domain /home/mastodon
sudo mv /home/mastodon/your.domain /etc/nginx/sites-available/
ln -s /etc/nginx/sites-available/your.domain /etc/nginx/sites-enabled/

之后先编辑

1
sudo nano /etc/nginx/sites-available/your.domain

在所有包含 ssl 的行前加注释,之后运行 sudo nginx -t,没有报错之后,使用 systemctl reload nginx 重启 nginx。
配置 SSL 证书:

1
2
3
4
5
6
apt install snapd
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot certonly --nginx -d your.domain

重启 nginx,打开配置文件,将所有刚才加上的注释取消。检查 Certificate 和 Key 的路径是否和刚才生成的路径一致,如不一致,修改为刚才 certbot 给出的路径。使用 nginx -t 检查没有错误,重启 nginx。
通过 certbot renew --dry-run 检查证书是否能自动更新。

开启全文搜索

参考:如何利用Docker搭建Mastodon实例(一):基础搭建篇 - 开启全文搜索

小结

刚迁移完外站网友头像为空,但会慢慢显示。这是由于没有把旧服务器的 cache 复制过来,所以外站的媒体存储全部都要依靠服务器一点一点和外站服务器交互再拉过来,图片类的信息都很慢。
主要参考: