# Add a new server to the backup system — this folder is all you need

How to add one production server (the source) to the backup system.
There are only **two places, three steps**.

```
[MASTER server]  1. Run setup-master-en.bat  ->  2. Restart MySQL once
                                                    |
[BACKUP server]  3. Register on the admin page -> Select tables -> [Start seeding]
                                                    |
                                              Done (the rest is automatic)
```

You'll need: the master's **MySQL root password**, **Administrator rights**, and a
**low-traffic window** (one restart is required).

> **First**, open the backup admin page **http://localhost/ -> Server management ->
> "Add server"**. At the top it shows a non-conflicting value like
> **`This server's server-id : #11`**. Write that number down and enter it in step 1.

---

## Step 1 — MASTER server: run `setup-master-en.bat`

1. Copy **both `setup-master-en.bat` and `setup-master-en.ps1`** to the **same folder** on the master server (USB / RDP / etc.).
   > ⚠️ They are a **pair**. The `.bat` (launcher) runs the `.ps1` (actual work) next to it. Do not copy only one.
   > ⚠️ **Do not re-save them in Notepad** — it changes the encoding and causes errors. (If you must edit, use VS Code; save the `.ps1` as "UTF-8 with BOM" and leave the `.bat` as-is.)
2. **Right-click `setup-master-en.bat` -> Run as administrator** (always run the `.bat`, not the `.ps1` directly).
3. Enter the values it asks for:

   | Prompt | Value |
   |---|---|
   | Backup server public IP | `42.125.196.178` (Enter = default) |
   | server-id for this master | **the # shown on the admin "Add server" page** (e.g. 11) |
   | Binary-log folder | `D:\mysql-binlog` (Enter = default) |
   | MySQL root password | the master's root password |
   | Replication account password | anything (Enter = auto-generate). **Write down the value shown** |

   -> my.ini settings + replication account + firewall are configured automatically.
   (The original my.ini is backed up as `my.ini.k3bak-<date>`, so it's safe.)

4. When it finishes it prints the **replication account / password / service name**.
   **Write these down** (used in step 3).

## Step 2 — MASTER server: restart MySQL (once)

Binary logging requires a restart. Do it during a **low-traffic window**.
Using the service name shown at the end of step 1:

```
net stop <service-name>
net start <service-name>
```

> The only downtime is those few seconds. Seeding and replication afterwards do
> not stop the master.

---

## Step 3 — BACKUP server: register on the admin page (just clicks)

On the backup server, open **http://localhost/** in a browser (log in).

1. **Server management -> Add server**
   - Master IP: the master's public IP
   - Port: `3306`
   - Replication account / password: **the values you wrote down in step 1**
   - Save -> a dedicated instance pair (realtime + delayed) is **created automatically** (1–2 min).

2. Shortly after, go to that server's **Select tables** screen.
   - Check the tables to back up (you can exclude large ones) -> **Save selection**
   - Click **[Start seeding]**

3. Everything after this is **automatic**:
   initial backup (realtime + delayed) -> replication filter applied -> realtime replication connected.
   Watch progress in the **Backup pipeline** indicator on the screen.
   When **"Replication connected"** appears, you're done.

---

## Once it's running

- **Realtime replication**: master changes appear on the backup server immediately
- **3-hour delayed replication**: accidentally deleted data still lives here for 3 hours
- **Auto-watchdog**: if replication stops, it auto-restarts within 5 minutes
- The admin page lets you download point-in-time SQL and browse code backups

## Troubleshooting

- **[Start seeding] is disabled**: the instance isn't ready yet -> refresh after 1–2 min, or you haven't selected tables
- **Pipeline shows "Failed"**: usually a master firewall/account issue -> re-run step 1 and retry
- **Roll back**: if the master's my.ini has a problem, restore `my.ini.k3bak-<date>` and restart MySQL

