EST. 2026 — Notes from real server work SYSADMIN · SECURITY · AI · DIRECTADMIN Issue 01
THE SYSADMIN ALMANAC
Home / Backup / Rebuilding a Replica with 236 GB of Binlogs from Another's Backup
Backup

Rebuilding a Replica with 236 GB of Binlogs from Another's Backup

by Ton-Or ·4 Dec 2026·12 min read
Rebuilding a Replica with 236 GB of Binlogs from Another's Backup

When rebuilding a replica from another replica's backup, one mistake can silently corrupt the data without an error: the coordinates used to point it at the master must be the actual master's coordinates, not the binlog coordinates of the machine the backup came from.

1-minute summary

Symptom / problem
A replica stopped replicating for so long it was unusable, and its own binlog used 236 GB of disk.
Who should read
MariaDB replication admins who must rebuild a replica.
Fix in short
Rebuild from an xbstream taken from another replica with --slave-info, then prepare, move-back, set server_id, and point at the master using the master's real coordinates.
Jump to steps

I maintain a client's MariaDB system with one master and two replicas. My reporting app pulled an hourly data copy directly from the master, putting load on the production machine. I wanted to pull from a replica instead. When I checked, I found that one replica had stopped replicating long ago, its old datadir was unusable, and its own binlogs had consumed 236 GB of disk space. I rebuilt it from a physical backup of the other, healthy replica.

The hostnames, account names, database names, and paths in this article are all fictional. The sizes and results are real. As for the commands, I need to be clear: my work notes recorded the steps and results, not every command line. The commands below use standard mariabackup syntax that matches the steps I took. Before using them, check them against the documentation for your installed version. I have not included timings for each step because I did not record them.

The starting state and the options I rejected

There were four machines involved.

Name Role Constraints
host-m Production master Must not be touched; only the database port is open, no SSH
host-c Healthy replica Take the backup from this machine
host-a Broken replica that needs rebuilding Its own binlogs had accumulated to 236 GB
host-b Backup storage machine with a large disk Temporary file storage

The 236 GB of binlogs on the replica came from enabling log_bin together with log_slave_updates without setting log retention. A replica configured this way writes its own binlog for every transaction received from the master and never deletes it.

There were three ways to rebuild it.

Rejected

Back up the master

This gives the most direct coordinates, but adds disk read load to the production machine throughout the backup, and I had no shell access to this machine anyway.

Rejected

Logical dump

No shell access to the source is needed, but for a database in the hundreds of GB, dumping and importing takes too long, and the replica would start far behind.

Chosen

Physical backup of the healthy replica

The master is untouched. I get the complete data files, ready to use once prepared, but I have to be careful to use the correct master coordinates.

host-mmasterExisting replicationhost-cHealthy replica--backup --slave-infoStream as xbstreamhost-bStore 122 GB filePipe via relay hostRebuild host-a1 mbstream -x2 mariabackup --prepare3 mariabackup --move-back4 CHANGE MASTER + START SLAVENew replication, lag 0Master coordinates: xtrabackup_slave_info, not xtrabackup_binlog_info
Data travels from host-c to temporary storage on host-b, then to host-a. host-m is untouched until the final step, when host-a connects as a normal replica.

Backing up a replica: --slave-info is essential

On host-c, I took a streamed physical backup using the root account through a unix socket. I used a socket so there would be no password on the command line for anyone to see through ps.

# บน host-c (replica ที่ยังดี) - ปลายทางของ stream คือพื้นที่เก็บ backup ของ host-b
mariabackup --backup --slave-info --stream=xbstream > /backup/replica-full.xbstream

This produced a 122 GB file stored on host-b.

--slave-info is the option that determines whether this job goes right or wrong. When backing up a replica, two sets of coordinates are relevant, and they point to different places.

File in the backup Contains coordinates for Use when
xtrabackup_binlog_info The binlog of the machine being backed up, host-c itself Setting up a new replica to replicate from host-c
xtrabackup_slave_info The position on the master that host-c had replicated through at backup time Setting up a new replica to replicate from the same master as host-c

The second file is only created when --slave-info is supplied. I wanted host-a to replicate directly from host-m, without chaining it through host-c, so I needed the second file.

Coordinates from the wrong file may produce no error

If I take the filename and position from host-c's xtrabackup_binlog_info and use them in CHANGE MASTER pointing at host-m, the result depends on whether that filename and position exist on the master. The worst case is that they do, and replication starts from a point that does not match the data, skipping or repeating transactions without any warning. Before typing CHANGE MASTER, open the file and make sure its name is slave_info.

Transfer the file, then extract, prepare, move-back

The file was on host-b, but the machine that needed it was host-a. These two machines had no SSH keys set up for each other. I transferred it through an intermediate machine using two ssh connections joined by a pipe. I wrote up the details separately in Hopping through an intermediate machine without keys or sshpass, with nested ssh hanging.

Once the file reached host-a, all remaining steps ran on that machine.

# บน host-a - หยุดบริการ แล้วย้าย datadir เก่าออกไปข้าง ๆ ยังไม่ลบ
systemctl stop mariadb
mv /var/lib/mysql /var/lib/mysql.old
mkdir -p /var/lib/mysql /data/restore

# 1) แตก stream ลงโฟลเดอร์เปล่า (ไฟล์ stream อยู่คนละโฟลเดอร์กับปลายทาง)
mbstream -x -C /data/restore < /data/stream/replica-full.xbstream

# 2) prepare: เล่น redo log ที่ติดมากับ backup ให้ไฟล์ข้อมูลอยู่ในสถานะสอดคล้องกัน
mariabackup --prepare --target-dir=/data/restore

# 3) ดูพิกัดของ master ก่อนย้ายไฟล์ จดไว้
cat /data/restore/xtrabackup_slave_info

# 4) move-back: ย้ายไฟล์เข้า datadir ตามค่าใน my.cnf (datadir ต้องว่าง)
mariabackup --move-back --target-dir=/data/restore
chown -R mysql:mysql /var/lib/mysql
  1. Stop MariaDB on host-a and move the old datadir aside. Do not delete it until the new instance is up.
  2. Extract the file with mbstream -x into an empty directory.
  3. mariabackup --prepare must finish with the message completed OK. Do not continue without it.
  4. Open xtrabackup_slave_info and record the master's binlog filename and position.
  5. Run mariabackup --move-back, then chown the entire datadir to the database user.
  6. Edit my.cnf as described in the next section before starting the service.

I used --move-back instead of --copy-back because copying files this large would require twice the space. Moving within the same filesystem only renames them and uses no extra space. The tradeoff is that the extracted directory is no longer available for another attempt. If something goes wrong after move-back, I have to extract the stream file again, so I need to keep the stream file until the job is finished.

The coordinate filenames depend on the mariabackup version. Some newer versions change the prefix from xtrabackup_ to mariadb_backup_. Run ls in the extracted directory first; do not guess the names.

my.cnf on the rebuilt machine: server_id, read_only, skip-slave-start

The backup is a copy of the data files and does not include my.cnf. But people often copy my.cnf from the source machine as a template, bringing along the same server_id as host-c. Two replicas with the same id will keep competing for the connection to the master. The symptom is replication repeatedly disconnecting and reconnecting.

# my.cnf บน host-a
[mariadb]
server_id = 3            # ต้องไม่ซ้ำกับ master และ replica ตัวอื่น
read_only = 1            # กันแอปหรือคนเขียนลง replica ตรง ๆ
# skip-slave-start       # comment ออกหลังชี้ master เสร็จ ให้ replication กลับมาเองหลัง reboot

# ถ้ายังเปิด binlog บน replica ต้องกำหนดอายุ ไม่งั้นกลับไปที่ 236 GB อีกรอบ
# log_bin = mysql-bin
# log_slave_updates = 1
# expire_logs_days = 7

The skip-slave-start line is useful only during the rebuild: it prevents replication from starting automatically before I have pointed it at the correct master. But if left in place, replication will not restart automatically after each reboot, and the replica will gradually become stale without anyone noticing. That was exactly the state I found this machine in. Once I had pointed it at the master and confirmed replication was running, I commented this line out.

read_only = 1 does not block accounts with elevated privileges, but it does block ordinary application accounts, which are the more common case. I included binlog retention as a commented example because my notes did not record the final value I set. The principle is that if no downstream replica connects to this replica, there is no need to enable log_slave_updates at all. If it is enabled, always set a retention period.

Pointing at the master using coordinates from the backup

Start the service, then enter the coordinates recorded from xtrabackup_slave_info.

-- บน host-a
CHANGE MASTER TO
  MASTER_HOST = 'host-m',
  MASTER_PORT = 3306,
  MASTER_USER = 'repl_a',
  MASTER_PASSWORD = '<REPL_PASSWORD>',
  MASTER_LOG_FILE = 'mysql-bin.00XXXX',   -- ค่าจาก xtrabackup_slave_info
  MASTER_LOG_POS = NNN;                    -- ค่าจาก xtrabackup_slave_info

START SLAVE;
SHOW SLAVE STATUS\G

mysql-bin.00XXXX and NNN are placeholders for the actual values from your file. I used filename and position coordinates. If the system uses GTID, the slave_info file will contain a different form of the command; use what the file provides.

Check three values in SHOW SLAVE STATUS.

-- สิ่งที่ต้องเห็น
--   Slave_IO_Running: Yes
--   Slave_SQL_Running: Yes
--   Seconds_Behind_Master: ตัวเลขที่ลดลงเรื่อย ๆ จนเป็น 0

-- ยืนยันด้วยข้อมูลจริง: แถวล่าสุดของตารางธุรกรรมต้องเป็นของวันนี้
SELECT MAX(created_at) FROM app_db.transactions;

This time, Seconds_Behind_Master dropped to 0, and the latest row in the transaction table was dated the day I was doing the work. I always check these two things together because lag alone cannot tell me whether the data is correct. I explained why in Reading from a replica needs a label showing how many minutes old the data is.

Checks before calling the job doneIO and SQL = YesConnected to master, applying events->Lag drops to 0Caught up with master->Latest row is from todayBusiness data matches->Remove skip-slave-startRestarts after reboot

Only after all checks passed did I delete /var/lib/mysql.old and the 236 GB of old binlogs. Those binlogs belonged to the old instance, which was no longer running, so I could delete them directly as files. For binlogs belonging to a running instance, do not delete the files manually. Use PURGE BINARY LOGS so the binlog index is updated accordingly.

What I ran into: Access denied with the correct password

The replication account cost me the most time. I entered the account used by host-c on host-a and got Access denied. I checked the password several times. It was correct.

The answer was on the master: each replica had its own replication account, restricted to its host.

-- บน host-m (อ่านอย่างเดียว) - ดูว่าบัญชี replication ผูกกับ host ไหน
SELECT user, host FROM mysql.user WHERE Repl_slave_priv = 'Y';
-- +--------+-------------+
-- | user   | host        |
-- +--------+-------------+
-- | repl_a | 10.10.10.11 |
-- | repl_c | 10.10.10.12 |
-- +--------+-------------+

The account for host-c only worked when connecting from host-c's address. When connecting from host-a, the database could not find a row matching both the username and host, so it rejected the connection. The error message did not distinguish a wrong password from a wrong host. host-a already had its own account from before it broke; I needed to use that one.

This design is correct; do not loosen it

A separate replication account for each machine, restricted by host, lets me revoke access one machine at a time, and a password leaked from one machine cannot be used from elsewhere. When facing Access denied like this, do not fix it by changing the host to %. Find the correct account to use.

Another issue had the same symptom: an admin account defined as user@'%' could not connect through the unix socket on host-a. I had to supply -h 127.0.0.1 to use TCP, or use root through the socket instead.

Moving heavy reads off the master

The original goal of this work was to reduce load on the production machine. Once host-a was up, I moved the data source for the hourly copy job in stages, first from host-m to host-c, then to host-a, keeping host-c as a fallback.

# ส่วนหัวของสคริปต์ดึงสำเนา - แหล่งข้อมูลเป็นตัวแปรตัวเดียว สลับได้ด้วยการแก้บรรทัดเดียว
SRC="-h host-a"          # ตัวหลัก: replica ที่เพิ่งสร้าง
# SRC="-h host-c"        # ตัวสำรอง: เปิดบรรทัดนี้แทนเมื่อ host-a มีปัญหา

# credential อยู่ในไฟล์ option แยก ไม่อยู่ในสคริปต์ และต้องเป็นตัวเลือกแรก
mariadb-dump --defaults-extra-file=/root/.mirror.cnf $SRC \
  --single-transaction --skip-lock-tables app_db transactions \
  | mariadb mirror_db

--single-transaction together with --skip-lock-tables lets the pull run without locking the source tables. I still switch sources by editing a variable manually, rather than automatically. That is deliberate: switching reads to a machine whose data may be older should involve a human decision.

236 GBBinlogs accumulated on the broken replica
122 GBxbstream file from the healthy replica
4 stepsextract, prepare, move-back, point at master
lag 0After pointing at master using backup coordinates
59 tablesFirst reload from host-a
0 failuresIn the first full pull
1,800 secondsMonitor's lag threshold
HourlyReplica monitoring interval

The first full pull from host-a reloaded 59 tables with 0 failures. After that, I set up a monitor to run hourly, comparing both replicas with the master. It treats Seconds_Behind_Master above 1,800 seconds as lagging and also checks for direct writes to the replicas. How I store the results and display them on a web page is covered in the article about data freshness labels linked above, so I will not repeat it here.

An unmonitored replica can stay broken for a long time unnoticed

This machine had stopped replicating and accumulated 236 GB of binlogs before I found it. A replica kept as a fallback needs monitoring just like a machine in active use, or the day it is needed will be the day someone discovers it is unusable.

Rebuilding a replica from another replica's backup avoids disturbing the production machine, and the procedure is short. There are three points to watch: include --slave-info when taking the backup and use the coordinates from that file, set a new and unique server_id, and remove skip-slave-start when the job is done. If you have access to back up the master without affecting production work, that is also an option; the coordinates will be more straightforward. If I have left anything out or made a typo, I will come back and fix it later.

Want us to set this up?

Get help with server setup, configuration, migration, or troubleshooting. Save time for the work that matters.

Get a quote

Hit something like this? Stuck somewhere?

Come trade notes on the LinuxThai board

→ Continue on the board

Comments 0

No comments yet — be the first