--- tags: public-tech breaks: false --- # Mastodonサーバを別サーバに移設した Ubuntu 18.04 LTSで動かしていたMastodonサーバを、Ubuntu 20.04 LTSが動いている新しいサーバに移設した。事前準備を色々とやったおかげで、([UptimeRobotいわく](https://stats.uptimerobot.com/GwDZksJWPr))1時間15分程度のダウンタイムで作業を終えることができた。 ## 背景 いま私が運営している身内用のMastodonサーバは2018年5月に立てたもので、Ubuntu 18.04 LTSを使っていた。来年にはこれのサポートが[切れてしまう](https://ubuntu.com/about/release-cycle)ので、早めにUbuntu 20.04 LTSに移行したかった。`do-release-upgrade`を使うことも考えたが、[PostgreSQL周りで面倒な話がある](https://blog.noellabo.jp/entry/2020/11/08/145740)と聞いていたので、思い切ってサーバごと移設することにした。 幸いなことにMastodonはドキュメントがかなり充実していて、サーバを移設するためのドキュメントも存在する([Migrating to a new machine](https://docs.joinmastodon.org/admin/migrating/))。基本的にはこれに従って作業を行った。ダウンタイムを短くしたかったので、事前にHackMDに手順書を作っておいて、それにしたがって作業した。以下はその手順書を適当にsanitizeして公開するもので、Mastodonサーバの移設をする人の助けになれば幸いである。 ## 環境 移設する元のサーバ(旧サーバ): - OS: Ubuntu 18.04 LTS - Mastodon: 3.5.0 - Ruby: 3.0.3p157 - PostgreSQL: 10.19 - Redis: 4.0.9 - Elasticsearch: 6.8.13 移行した先のサーバ(新サーバ): - OS: Ubuntu **20.04** LTS - Mastodon: 3.5.0 - Ruby: 3.0.3p157 - PostgreSQL: **14.2** - Redis: **5.0.7** - Elasticsearch: 7.17.2 双方のサーバに共通して: - さくらのVPS 2Gプラン - Dockerは非使用 - 画像はGoogle Cloud Storageに保存しているため、移設不要 ## 移設前の作業 ### Mastodonのバージョンを最新(v3.5.0)にする Mastodonのdocumentがv3.5.0基準になっているように見えるので、上げておく。 ### DNSのTTLを短めに変更 作業では60分にしたが、もっと短くても良いかもしれない。 ### 新サーバで新しいMastodon鯖を構築する(ただし`mastodon:setup`は実行しない) [公式ドキュメント](https://docs.joinmastodon.org/admin/install/)の手順で作業する。ただし"Generating a configuration"の手前で止めておき、`mastodon:setup`は実行しない。 Elasticsearchは後で別途構築するので、ここでは何もしないでおく。 ### 旧サーバの`public/`を新サーバにコピー 画像はGCSにあるので移動不要だが、Mastodonをカスタムして投稿欄下のお知らせなどを出しているため、それに必要なJSONファイルや画像などを移動しておく。 ### 旧サーバの`.env.production`ファイルを新サーバにコピー Elasticsearchはあとで構築するため無効にしておく。 ### 旧サーバのNginx設定ファイルを新サーバにコピー Mastodonレポジトリで配布しているもの(`dist/nginx.conf`)を使うならコピー不要。 ### 旧サーバのsystemd設定ファイルをコピー Mastodonレポジトリで配布しているもの(`dist/mastodon-*.service`)を使うならコピー不要。 ### 旧サーバのsystemd configファイルをコピー 新サーバで`certbot`を新しく回す手もあるが、こちらのほうが簡単(な気がする)。 ### 旧サーバの(独自)バックアップシステムを新サーバにコピー バックアップシステムが動作するのに必要なパッケージも入れておく。 ### PGTune結果を新サーバの`postgresql.conf`に追加 [PGTune](https://pgtune.leopard.in.ua/#/)で作った結果を`/etc/postgresql/14/main/postgresql.conf`の末尾に追加 ### 旧サーバにPostgreSQL 14の`pg_dump`をインストール 後述する移設の際にデータベースのdumpを取得することになるが、旧サーバと新サーバでDBのバージョンが違う場合は、新サーバのバージョンに合わせた`pg_dump`を使う必要があるらしい(Thanks to: [@osapon@mstdn.nere9.help](https://mstdn.nere9.help/@osapon/108051975468210963)さん)。https://zenn.dev/dai0916/articles/a593ccd9ab3773b2b04f などを参考にソースコードをとってきてビルドする。 ``` wget https://ftp.postgresql.org/pub/source/v14.2/postgresql-14.2.tar.gz tar xf postgresql-14.2.tar.gz cd postgresql-14.2 sudo mkdir /usr/local/postgresql-14.2/ ./configure --prefix=/usr/local/postgresql-14.2/ make make install ``` ### 旧サーバで503を簡単に返せるようにする たとえばNginxの設定ファイルとかに以下のようなコードを追加すると、`sudo touch /var/tmp/503`するだけで503を返せる。 ``` ## NGINX if is EVIL!! ## Thanks to: https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/ ## Thanks to: https://agentzh.blogspot.com/2011/03/how-nginx-location-if-works.html ## Thanks to: http://macotasu.hatenablog.jp/entry/2017/06/18/200135 ## Thanks to: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if ## Thanks to: https://trac.nginx.org/nginx/ticket/745 ## Thanks to: https://yoshipc.net/maintenance-mastodon/ set $maintenance false; if (-e /var/tmp/503) { set $maintenance true; } if ($maintenance = true) { return 503; } ``` ### サーバの利用者にメンテナンス予定を通知 <iframe src="https://mstdn.anqou.net/@wakuwaku/108057606344596175/embed" class="mastodon-embed" style="max-width: 100%; border: 0" width="400" allowfullscreen="allowfullscreen"></iframe><script src="https://mstdn.anqou.net/embed.js" async="async"></script> ## 移設 ### メンテナンスの開始を宣言 <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">これからわくわく鮟鱇ランドのメンテナンスをします。メンテナンス中はサービスが停止します。メンテナンスの状況は適宜、このツイートのツリーで報告します。何事もなければ1〜2時間くらいで終わる予定です。</p>— 艮 鮟鱇 (@ushitora_anqou) <a href="https://twitter.com/ushitora_anqou/status/1510514308622733313?ref_src=twsrc%5Etfw">April 3, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> ### 旧サーバで503を返すようにする 前述の仕組みがある場合は`sudo touch /var/tmp/503`する。 ### 旧サーバでMastodonを止める ``` sudo systemctl stop mastodon-web mastodon-sidekiq mastodon-streaming ``` ### 旧サーバでPostgreSQL DBのdumpをとる PostgreSQL 14の`pg_dump`を使う。`-Fc`オプションを使うことで、出力は圧縮される。少し時間がかかるのでverboseでやるとよかったかも。 ``` sudo -u mastodon /usr/local/postgresql-14.2/bin/pg_dump -Fc mastodon_production -f backup.dump ``` `backup.dump`を新サーバへコピーする。 ### 新サーバでPostgreSQL DBのrestoreをする ``` sudo -u mastodon createdb -T template0 mastodon_production sudo -u mastodon pg_restore -Fc -U mastodon -n public --no-owner --role=mastodon -d mastodon_production backup.dump ``` かなり時間がかかるのでverboseでやるとよかったかも。 ### 新サーバで`RAILS_ENV=production bundle exec rails assets:precompile`を実行 ### 新サーバで`RAILS_ENV=production ./bin/tootctl feeds build --all`を実行 ### 新サーバでMastodonを起動 `sudo systemctl start mastodon-sidekiq mastodon-web mastodon-streaming` ### Nginxのconfを有効化 ### DNSの設定を変えて新サーバにアクセスが向かうようにする ### whatsmydns.net でDNS propagationの様子を見る ### 新サーバ上のMastodonが正しく動作しているかを確認 いくつか問題点はあった(後述)が、概ね正しく動いていた。 ### メンテナンスの終了を宣言 <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">無事復旧しました。ご協力ありがとうございました。不具合など残っている可能性があるので、その場合はご連絡ください。なお既知の不具合として、リストが正しく表示されていません。 <a href="https://t.co/1jEUhYrtpj">https://t.co/1jEUhYrtpj</a></p>— 艮 鮟鱇 (@ushitora_anqou) <a href="https://twitter.com/ushitora_anqou/status/1510531229330063364?ref_src=twsrc%5Etfw">April 3, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> ## 移設後の作業 ### DNSのTTLをもとの値に変更 Cloudflareの場合は「自動」に。 ### Certbotの状態を確認 特に`certbot renew`ができるかと、`certbot renew`が定期的に実行される状態になっているかを確認。 ### バックアップシステムを構築 ### Elasticsearchを構築して全文検索を有効化 これについては別記事を書くかも。サーバ移設は特に関係なく、素直に日本語対応全文検索システムを構成する。 [書きました。](https://hackmd.io/1hAxBjI3TV-ty4vjG6VEUA) ## 振り返り - DNSの設定として1時間は長すぎた - 自分のPCでは見れないのに、他のユーザはアクセスできるみたいな時間が長かった。 - 前日に1時間にしておいて、当日に5分とかにするとよさそう。 - DNSを書き換える前に動作確認する方法がほしい - 今回はえいやで動作させたが、本当は他のユーザがアクセスできない状態で動作確認をしたい。 - リストに過去の投稿が表示されない - HTLは`tootctl feeds build --all`で正しく表示されるようになったが、リストは表示されない。 - これは単にtootctl側で実装されていないため([のようにみえる](https://github.com/mastodon/mastodon/blob/0a8a0fb5990eb6d3842948654d0b6140e776d8a9/app/services/precompute_feed_service.rb#L5))。ヘルプメッセージにはリストも構成対象に含まれているようにみえるのだが、単に間違っている? - Redisのdumpをとって移設すればよかったかも
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up