automysqlbackupに救われた。
SSDが寿命により崩壊し、データが消えたと嘆いていたのですが。mysql のデータは残っていた。
入れたことを忘れていた。
存在を忘れていたautomysqlbackupがバックアップを取ってくれていた。助かる。
まじ救われた。5年前の自分に感謝。
バックアップの保存先。
/var/lib/automysqlbackup
automysqlbackup インストール
debian 11 では。パスワードの設定が必要。以前は debian-sys-adm というユーザーが存在してそれをそのまま使うことができていたが、セキュリティ上の理由の懸念により現在は使えない。
MariaDB [(none)]> SELECT Host, User, Password FROM mysql.user; +-----------+------------------+-------------------------------------------+ | Host | User | Password | +-----------+------------------+-------------------------------------------+ | localhost | root | *ABCDEFGHIJKLMNOP123456787890| +-----------+------------------+-------------------------------------------+
面倒なので、既存のパスワードを消してやる
このため、デフォルトはrootなので、sshでログインしない限り使えない。
set password for root@localhost=password('')
root@localhsot のパスワードを無効にしても大丈夫なのは、ssh でログインしない限り使えないから。リモート( mysql 接続) では使えません。ただしnginxのプロキシでmysql.sock を公開されちゃうとかされたら危険。そのときは知らない。
バックアップが取得されることを確認。
sudo /usr/sbin/automysqlbackup sudo find /var/lib/automysqlbackup/
パスワードを設定する場合
automysqlbackup は単なるスクリプトなので設定は環境変数を使うだけになっている。
/etc/default/automysqlbackup
USERNAME=debian-sys-maint PASSWORD=my_root_password DBNAMES="`mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST --batch --skip-column-names -e "show databases"| sed 's/ /%/g'`"
これで、auto mysql dump backup が稼働する。
debian11 での注意
debian-sys-maint と /etc/mysql/debian.cnf が使えなくなっている。
以前は、/etc/mysql/debian.cnf にメンテナンス用のパスワードを記載していたが使えなくなっているので注意が必要。
debian 11 での利用の場合
debian-sys-maint を有効にする。
ALTER USER 'debian-sys-maint'@'localhost' IDENTIFIED BY 'P@assword123456' WITH GRANT OPTION;
sys-maint を使うように設定する。
USERNAME=debian-sys-maint PASSWORD=P@assword123456
performance_schema を除外する。
DBNAMES="`mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST \ --batch --skip-column-names -e "show databases"| \ grep -v performance_schema | sed 's/ /%/g'`"
perfomance_schema.account テーブルがロックできなくて落ちる。
次のようにロックのエラーが出てくる。
mysqldump: Got error: 1142: "SELECT, LOCK TABLES command denied to user 'debian-sys-maint'@'localhost' for table 'accounts'" when using LOCK TABLES
perfomance_schemaは、バックアップになくても影響ないテーブルなので、除外する。
そのために、/etc/cron.daily/automysqlbackup を書き換えて、grep -v を追加した。
2025-04-13 追加
debian 12 でまたいろいろとトラブった。
sudo cat /etc/cron.daily/automysqlbackup #!/bin/sh test -x /usr/sbin/automysqlbackup || exit 0 /usr/sbin/automysqlbackup
--defaults-file=/etc/mysql/debian.cnf を微妙な感じで残すように書かれてる。なんか微妙な感じ。
# By default, the Debian version of automysqlbackup will use:
# mysqldump --defaults-file=/etc/mysql/debian.cnf
# but you might want to overwrite with a specific user & pass.
# To do this, simply edit bellow.
# Username to access the MySQL server e.g. dbuser
#USERNAME=`grep user /etc/mysql/debian.cnf | tail -n 1 | cut -d"=" -f2 | awk '{print $1}'`
# Username to access the MySQL server e.g. password
#PASSWORD=`grep password /etc/mysql/debian.cnf | tail -n 1 | cut -d"=" -f2 | awk '{print $1}'`
# Host name (or IP address) of MySQL server e.g localhost
DBHOST=localhost
## memo 2025-04-13
#
USERNAME=debian-sys-maint
PASSWORD='enough PASSWORD'
# List of DBNAMES for Daily/Weekly Backup e.g. "DB1 DB2 DB3"
# Note that it's absolutely normal that the db named "mysql" is not in this
# list, as it's added later by the script. See the MDBNAMES directives below
# in this file (advanced options).
# This is ONLY a convenient default, if you don't like it, don't complain
# and write your own.
# The following is a quick hack that will find the names of the databases by
# reading the mysql folder content. Feel free to replace by something else.
# DBNAMES=`find /var/lib/mysql -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f5 | grep -v ^mysql\$ | tr \\\r\\\n ,\ `
# This one does a list of dbs using a MySQL statement.
#DBNAMES=`mysql --defaults-file=/etc/mysql/debian.cnf --execute="SHOW DATABASES" | awk '{print $1}' | grep -v ^Database$ | grep -v ^mysql$ | grep -v ^performance_schema$ | grep -v ^information_schema$ | tr \\\r\\\n ,\ `
DBNAMES=`mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST --execute="SHOW DATABASES" | awk '{print $1}' | grep -v ^Database$ | grep -v ^mysql$ | grep -v ^performance_schema$ | grep -v ^information_schema$ | tr \\\r\\\n ,\ `
ぶっちゃけ、もう手作業でmysqldumpスクリプト作ったほうが早い気がしてる。
参考資料
- https://askubuntu.com/questions/1000128/automysqlbackup-not-working-with-16-04-xubuntu-when-mysql-user-set-plugin-mys
- https://uxmilk.jp/12703
- https://at284km.hatenablog.com/entry/2015/07/11/065931
- https://www.vultr.com/docs/how-to-install-and-configure-automysqlbackup-on-ubuntu-16-04
- https://serverpilot.io/docs/how-to-back-up-mysql-databases-with-automysqlbackup/