初めに
さくらのVPSのOSをCentOSStream9からDebian13に再インストールした時の作業メモです。
DockerでmysqlとRuby on Railsを設定しており、DockerHubにpushしてOS再インストール、DockerHubからpullして再構築とカッコイイ事は出来ず、Dockerの環境を再構築しました。まだまだです。
なんでOSを再インストールしたか
CentOSStream9にmacOSからsshでアクセスすると、
** WARNING: connection is not using a post-quantum key exchange algorithm.
** This session may be vulnerable to "store now, decrypt later" attacks.
** The server may need to be upgraded. See https://openssh.com/pq.html
と「まだ実用化されていないが今ssh通信を貯めておいて後で量子コンピューターが実用化されたら復号される」旨の警告が鬱陶しく、CentOS Streamを10に上げようかと思ったが以前メモリー不足でインストーラが途中でハングしたのでDebianに再インストール。
バックアップ
データーベースの内容をバックアップする、backup.shというシェルスクリプトを作ってあるので、
backup.sh
でDBバックアップ。
tar -cvf bin.tar ./bin
tar -cvf src.tar ./src
gzip -9 src.tar
gzip -9 bin.tar
tar -cvf mysql.cnf.tar ./.my.cnf
gzip -9 mysql.cnf.tar
sudo cp -p /etc/ssh/ssh_config .
sudo chown xxxx:xxxx ssh_config
tar -cvf ssh_config.tar ssh_config
gzip -9 ssh_config.tar
で必要なのバックアップ。
クライアントで、
cd $HOME/Downloads/
mkdir vps_reinstall_2026_08_20
cd vps_reinstall_2026_08_20
scp -P xxxxx tetu@xxx.xxx.xxx.xxx:/home/xxxx/backup/download_backup.tar .
scp -P xxxxx tetu@xxx.xxx.xxx.xxx:/home/xxxx/src.tar.gz .
scp -P xxxxx tetu@xxx.xxx.xxx.xxx:/home/xxxx/bin.tar.gz .
scp -P xxxxx tetu@xxx.xxx.xxx.xxx:/home/xxxx/mysql.cnf.tar.gz .
scp -P xxxxx tetu@xxx.xxx.xxx.xxx:/home/xxxx/ssh_config.tar.gz .
でクライアントに取得。
mkdir tmp
cd tmp
tar -xvf ./../download_backup.tar
gzip -cd 2026_08_20_sleep.sql.gz
07-31 15:49:45.797491','2026-08-15 22:58:25.154085'),(868,'2026-08-16',155,-9,80,'-','07:44:00','02:50:00','明かりをつけて寝てしまい、入眠に深い睡眠が出なかったかです。後10分で3h行ったので、明かりは消して寝ます。どうにもです。以上メモです。','2026-07-31 15:49:45.797491','2026-08-16 23:41:25.874874'),(869,'2026-08-17',82,55,145,'-',
でバックアップが取れているのを確認。
OS再インストール
さくらのVPSのOSをDebian12に再インストールしました。さくらインターネットが提供しているイメージを使いました。
まずCentOSStream9を停止。
sudo shutdown -h now
OS再インストール。ユーザ画面が作成してくれるユーザ(管理者)パスワード設定。
SSHの公開鍵設定。
パケットフィルターにSSHのポート番号指定。
コンソールからssh設定。
cd /etc/ssh
sudo vi ./sshd_config
Include /etc/ssh/sshd_config.d/*.conf
Port xxxxx
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
LogLevel INFO
PermitRootLogin no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
PasswordAuthentication no
PermitEmptyPasswords no
KbdInteractiveAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials yes
UsePAM yes
PrintMotd no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
AllowUsers xxxx@xxx.xxx.xxx.xxx xxxx@xxx.xxx.xxx.xxx
程度を設定。AllowUsersはスペース区切りで並べました。
sudo systemctl reload sshd
クライアントから、
ssh debian@xxx.xxx.xxx.xxx -p xxxxx
クライアント側のknown_hostsと不整合したので古いのを削除してログイン出来ました。続いて普段使いのユーザー作成。
sudo adduser xxxx
sudo usermod -aG sudo xxxx
sudo su - xxxx
sudo apt update
sudo apt update出来ました。
84 packages can be upgraded. Run 'apt list --upgradable' to see them.
N: Repository 'http://security.debian.org/debian-security bookworm-security InRelease' changed its 'Suite' value from 'stable-security' to 'oldstable-security'
N: Repository 'http://deb.debian.org/debian bookworm InRelease' changed its 'Version' value from '12.6' to '12.15'
N: Repository 'http://deb.debian.org/debian bookworm InRelease' changed its 'Suite' value from 'stable' to 'oldstable'
N: Repository 'http://deb.debian.org/debian bookworm-updates InRelease' changed its 'Suite' value from 'stable-updates' to 'oldstable-updates'
と警告が出てまあそりゃ今は13が現行なので上げようかなと。
sudo apt update
sudo apt upgrade --without-new-pkgs
sudo apt full-upgrade
sudo apt autoremove
cat /etc/debian_version
12.15
cd /etc/apt/
sudo sed -i 's/bookworm/trixie/g' /etc/apt/sources.list
sudo apt update
sudo apt upgrade --without-new-pkgs
sudo apt full-upgrade
sudo reboot
cat /etc/debian_version
13.6
sudo apt autoremove --purge
普段遣いのユーザのssh設定。さくらのVPSのインストーラが作ってくれた初期debianユーザで、
cd /tmp
cp -pr $HOME/.ssh .
sudo chown -R xxxx:xxxx ./.ssh
sudo su - xxxx
mv /tmp/.ssh .
drwx------ 2 xxxx xxxx 4096 Aug 20 08:39 .ssh
クライアントから、
ssh xxxx@xxx.xxx.xxx.xxx -p xxxxx
でログイン出来ました。
Dockerのインストール。
sudo apt update
sudo apt install ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
Hit:1 http://security.debian.org/debian-security trixie-security InRelease
Hit:2 http://deb.debian.org/debian trixie InRelease
Hit:3 http://deb.debian.org/debian trixie-updates InRelease
Get:4 https://download.docker.com/linux/debian trixie InRelease [32.5 kB]
Get:5 https://download.docker.com/linux/debian trixie/stable amd64 Packages [43.9 kB]
Fetched 76.4 kB in 0s (343 kB/s)
All packages are up to date.
ちゃんとdownload.docker.comがリポジトリに追加されていたのでapt install。
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
4f55086f7dd0: Pull complete
d5e71e642bf5: Download complete
Digest: sha256:5dd0d3e6e255913fc30f90b9f2b1d359cc2cbdb48090cc4b65f1676e203243cc
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Dockerがインストール出来ました。
MySQLとRuby on Railsインストール
まずMySQLを設定しました。
sudo apt install default-mysql-client
cd $HOME
mkdir backup
(backupというディレクトリーをDockerからマウントしているので)
以前のDockerfileとdocker-compose.ymlを使用。
Dockerfile。
FROM mysql:latest
RUN apt-get update
RUN apt-get install locales-all
ENV LANG ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja
ENV LC_ALL ja_JP.UTF-8
docker-compose.yml。
services:
app-db:
container_name: xx
image: mysql:latest
command:
--collation-server=utf8mb4_0900_bin
--transaction-isolation=READ-COMMITTED
restart: always
environment:
MYSQL_ROOT_PASSWORD: xxxx
TZ: Asia/Tokyo #タイムゾーンを東京に
ports:
- xxxx:xxxx
volumes:
- type: bind
source: /home/xxxx/backup
target: /backup
sudo docker compose up
[ERROR] [MY-011065] [Server] Unable to determine if daemon is running: Inappropriate ioctl for device (rc=0)
512Mコースではメモリーが足りなかった。スワップを増やす。
MiB Mem : 462.1 total, 165.7 free, 164.6 used, 149.7 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 297.5 avail Mem
さくらのVPSの吊るしだとスワップ作ってない。作ります。
cd /
sudo fallocate -l 8G /swapfile
sudo mkswap /swapfile
mkswap: /swapfile: insecure permissions 0644, fix with: chmod 0600 /swapfile
Setting up swapspace version 1, size = 8 GiB (8589930496 bytes)
sudo chmod 600 ./swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo reboot
効いていますように。
MiB Mem : 462.1 total, 11.3 free, 355.3 used, 107.7 buff/cache
MiB Swap: 8192.0 total, 7976.3 free, 215.7 used. 106.8 avail Mem
効いていた。
MySQLのDockerコンテナーとイメージを削除してやり直し。
sudo docker compose up
sudo docker exec -it db /bin/sh
sh-5.1#
入った。データーリストア。
クライアントから
scp -P xxxxx download_backup.tar xxxx@xxx.xxx.xxx.xxx:/home/xxxx/
mkdir data_restore
cd data_restore
tar -xvf ./../download_backup.tar
gzip -d 2026_08_20_sleep.sql.gz
cd ./../
cd backup
cp -p ./../data_restore/2026_08_20_sleep.sql .
mysql -h 127.0.0.1 -P xxxx -u root -p
CREATE DATABASE xxxx;
USE xxxx;
create user 'xxxx'@'%' identified by 'xxxx';
GRANT SELECT, INSERT, UPDATE, DELETE ON xxxx.* TO 'xxxx'@'%';
source /backup/2026_08_20_sleep.sql
データリストア。SELECTしてリストアされたのを確認しました。
cd $HOME
cd backup
ls
2026_08_20_sleep.sql
rm -i 2026_08_20_sleep.sql
rm: remove regular file '2026_08_20_sleep.sql'? y
cd ..
rm -i download_backup.tar
rm -rf data_restore
Ruby on Railsのインストール
sudo docker network create xxxx
sudo docker network connect xxxx db
cd $HOME/src/sleep-bak
Dockerfile。
FROM ruby:3.1
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN gem update --system ${RUBYGEMS_VERSION} && \
bundle install
COPY . /app
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE xxxx
CMD ["rails", "server", "-b", "0.0.0.0"]
docker-compose.yml
services:
web:
container_name: sleep-bak
build: .
command: /bin/sh -c "bundle exec rails s -p xxxx -b '0.0.0.0'"
volumes:
- .:/app
ports:
- xxxx:xxxx
tty: true
stdin_open: true
restart: always
DockerもRuby on Railsも分からない…。
sudo docker compose up --build
sudo docker network connect xxxx sleep-bak
curl http://localhost:xxxx/
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails 7.0.10</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link rel="icon" href="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIwcHgiIGhlaWdodD0iMzIwcHgiIHZpZXdCb3g9IjAgMCAzMjAgMzIwIiB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPgogICAgPHRpdGxlPkljb248L3RpdGxlPgogICAgPGcgaWQ9Ikljb24iIHN0cm9rZT0ibm9uZSIgc3Ryb2tlLXdpZHRoPSIxIiBmaWxsPSJub25lIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiPgogICAgICAgIDxnIGlkPSJSdWJ5LU9uLVJhaWxzLUxvZ28iIHRyYW5zZm9ybT0idHJhbnNsYXRlKDUuMDAwMDAwLCAyMC4wMDAwMDApIiBmaWxsPSIjRDgxRTAwIj4KICAgICAgICAgICAgPHBhdGggZD0iTTIxLjkzMzA3MDYsM
次にブラウザーからTOPページにアクセスできた。JSONを返すページを確認し、JSONを取得しているReactページも確認し、動作していました。
バックアップの設定
クライアントから
scp -P xxxxx bin.tar.gz xxxx@xxx.xxx.xxx.xxx:/home/xxxx/
scp -P xxxxx mysql.cnf.tar.gz xxxx@xxx.xxx.xxx.xxx:/home/xxxx/
で転送。
gzip -cd mysql.cnf.tar.gz | tar -xvf -
gzip -cd bin.tar.gz | tar -xvf -
mysql -h 127.0.0.1 -P xxxx -u root -p
create user '[バックアップユーザ名]'@'%' identified by 'xxxx';
GRANTはかなりたくさんつけた。テーブルのロック取ったりするので。何かいい方法はないだろうか。
/usr/bin/mysqldump --defaults-extra-file=/home/xxxx/xxxx -h 127.0.0.1 -P [ポート番号] [DB名] > /home/xxxx/backup/`date "+%Y_%m_%d"`_sleep.sql
取れたのでシェルスクリプト確認。
cd $HOME/bin/
backup.sh
取れたので結果を削除。cronに掛ける。
crontab -e
0 23 * * * /home/tetu/bin/backup.sh
後はシステム全体のアップデート。
sudo su -
crontab -e
0 2 17 * * /root/bin/update.sh
#!/bin/bash
apt update && apt upgrade -y && apt autoremove && reboot
chmod 755 update.sh
明日確認します。後日確認したらバックアップは取れていました。続いて取ります。
ファイヤーウォールの設定
ufwインストール。
sudo apt update && sudo apt install ufw
sudo ufw enable
この状態でブラウザーRonRのページを見たら見えてしまったので再起動した。
sudo reboot
コンソールで
sudo ufw allow from xxx.xxx.xxx.xxx to any port [sshで使用しているポート]
sshログイン。
この状態でブラウザーでブラウザーでRonRページを見たら見えてしまったので多分既にiptablesにDockerがルール書いてくれてるのだろうと。
sudo ufw allow from xxx.xxx.xxx.xxx to any port [RonRのポート]
はした。
sudo iptables-save
-A DOCKER -d xxx.xxx.xxx.xxx/xxx ! -i [Interface名] -o [Interface名] -p tcp -m tcp --dport [MySQLのポート番号] -j ACCEPT
-A DOCKER -d xxx.xxx.xxx.xxx/xxx ! -i [Interface名] -o [Interface名] -p tcp -m tcp --dport [RonRのポート番号] -j ACCEPT
-A DOCKER ! -i [Interface名] -p tcp -m tcp --dport [RonRのポート番号] -j DNAT --to-destination xxx.xxx.xxx.xxx:[RonRのポート番号]
-A DOCKER ! -i [Interface名] -p tcp -m tcp --dport [MySQLのポート番号] -j DNAT --to-destination xxx.xxx.xxx.xxx:[MySQLのポート番号]
COMMIT
# Completed on Thu Aug 20 14:17:34 2026
DOCKERルールを追加してくれてたのでRonRはこのまま放置。ちなみにMySQLはクライアントから繋げるだろうか。
mysql -h xxx.xxx.xxx.xxx -P xxxx -u root -p
入れてしまったのでiptablesのルールを削除する。
sudo iptables -L DOCKER -v --line-numbers
tetu@ik1-340-30415:~$ sudo iptables -L DOCKER -v --line-numbers
Chain DOCKER (4 references)
num pkts bytes target prot opt in out source destination
1 4 224 ACCEPT tcp -- ![Interface名] [Interface名] anywhere xxx.xxx.xxx.xxx tcp dpt:mysql
1がついていたので1を削除
sudo iptables -D DOCKER 1
sudo iptables -L DOCKER -v --line-numbers
Chain DOCKER (4 references)
num pkts bytes target prot opt in out source destination
1 2 120 ACCEPT tcp -- ![Interface名] [Interface名] anywhere xxx.xxx.xxx.xxx tcp dpt:http-alt
2 0 0 DROP all -- ![Interface名] [Interface名] anywhere anywhere
3 0 0 DROP all -- ![Interface名] [Interface名] anywhere anywhere
mysql -h xxx.xxx.xxx.xxx -P xxxx -u root -p
ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx' (60)
自分が使っている端末から入れなくなったので設定できました。
VPS内からはMySQLにログインできたし、バックアップスクリプトの動作も確認しました。
以上でさくらのVPSで借りているサーバにDebianをインストールしました。以上メモです。