# Backup Management Documentation

## Overview

Fitur **Backup Management** tersedia di `/admin/backup` memungkinkan admin untuk backup database MySQL dan custom directories, serta restore/import database melalui UI.

## Fitur

### Backup

| Tipe | Deskripsi | Format File |
|------|-----------|-------------|
| MySQL Database | Dump seluruh database via `mysqldump` + gzip | `mysql_backup_YYYY-MM-DD_HH-MM-SS.sql.gz` |
| Directory | Tarball custom directories via `sudo tar` | `dir_backup_{path}_YYYY-MM-DD_HH-MM-SS.tar.gz` |
| Full | MySQL dump + semua custom directories dalam satu tarball | `full_backup_YYYY-MM-DD_HH-MM-SS.tar.gz` |

### Restore / Import

| Fitur | Deskripsi |
|-------|-----------|
| Restore dari List | Klik ikon RotateCcw (orange) di backup type `mysql` atau `full` untuk restore langsung ke database |
| Import SQL | Upload file `.sql` atau `.sql.gz` dari komputer ke database via tombol "Import SQL File" |

## mysqldump Flags

Backup MySQL menggunakan flag berikut untuk memastikan dump identik dengan export manual:

```
--routines                # Stored procedures (22 procedures di database pw)
--triggers                # Triggers
--events                  # Events
--single-transaction      # Consistent snapshot tanpa lock
--add-drop-table          # DROP TABLE sebelum CREATE TABLE
--default-character-set=utf8mb4  # Character set
--complete-insert         # INSERT dengan column names
--skip-lock-tables        # Tidak lock tabel
```

> Database `pw` memiliki 22 stored procedures (acquireuserpasswd, addGM, adduser, changePasswd, dsb). Flag `--routines` wajib agar procedures ikut ter-backup.

## Database `pw` Structure

Database game Perfect World berisi tabel:

| Tabel | Deskripsi |
|-------|-----------|
| `auth` | User authorization (userid, zoneid, rid) |
| `forbid` | Bans/mutes (userid, type, ctime, forbid_time, reason) |
| `iplimit` | IP restrictions per user |
| `point` | Account points/time tracking |
| `usecashlog` | Cash usage history |
| `usecashnow` | Active cash transactions |
| `users` | Account credentials (ID, name, passwd, email) |

## Settings

Backup settings disimpan di tabel `settings`:

| Key | Default | Deskripsi |
|-----|---------|-----------|
| `backup_mysql_host` | dari `.env` DB_HOST | MySQL host |
| `backup_mysql_port` | dari `.env` DB_PORT | MySQL port |
| `backup_mysql_database` | dari `.env` DB_DATABASE | Database name |
| `backup_mysql_username` | dari `.env` DB_USERNAME | MySQL username |
| `backup_mysql_password` | dari `.env` DB_PASSWORD | MySQL password |
| `backup_directory` | `storage_path('app/backups')` | Lokasi penyimpanan backup |
| `backup_custom_paths` | `["/home/gamedbd/dbhomewdb","/home/uniquenamed/uname"]` | Custom directory paths |

## Routes

| Method | URI | Nama | Deskripsi |
|--------|-----|------|-----------|
| GET | `/admin/backup` | `admin.backup.index` | Halaman backup management |
| GET | `/admin/backup/progress` | `admin.backup.progress` | Poll progress status (JSON) |
| POST | `/admin/backup/settings` | `admin.backup.settings.update` | Update settings |
| POST | `/admin/backup/mysql` | `admin.backup.mysql` | Trigger MySQL backup |
| POST | `/admin/backup/directory` | `admin.backup.directory` | Trigger directory backup |
| POST | `/admin/backup/all` | `admin.backup.all` | Trigger full backup |
| POST | `/admin/backup/import` | `admin.backup.import` | Upload & import SQL file |
| POST | `/admin/backup/restore/{filename}` | `admin.backup.restore` | Restore from existing backup |
| GET | `/admin/backup/download/{filename}` | `admin.backup.download` | Download backup file |
| DELETE | `/admin/backup/{filename}` | `admin.backup.delete` | Hapus backup |

## Security Notes

- Restore dan import **MENIMPA seluruh database** — ada dialog konfirmasi dengan warning sebelum eksekusi
- Import hanya menerima file `.sql` dan `.sql.gz`, max 500MB
- Restore hanya tersedia untuk backup type `mysql` dan `full`
- Semua route dilindungi oleh middleware `auth` + `role:admin`

## Sudoers

`www-data` memerlukan akses tanpa password untuk:

```
www-data ALL=(ALL) NOPASSWD: /bin/tar
www-data ALL=(ALL) NOPASSWD: /bin/cp
www-data ALL=(ALL) NOPASSWD: /usr/bin/mysqldump
www-data ALL=(ALL) NOPASSWD: /usr/bin/mysql
```

File sudoers: `/etc/sudoers.d/www-data`

## File Terkait

| File | Deskripsi |
|------|-----------|
| `app/Http/Controllers/Admin/Backup/BackupController.php` | Controller (backup, restore, import, download, delete) |
| `database/migrations/2026_01_06_180000_create_backups_table.php` | Migration tabel backups |
| `routes/admin.php` | Route definitions |
| `resources/js/pages/admin/backup/index.tsx` | Halaman utama React |
| `resources/js/pages/admin/backup/components/BackupActions.tsx` | Tombol backup + import |
| `resources/js/pages/admin/backup/components/BackupList.tsx` | Daftar backup + tombol restore/download/delete |
| `resources/js/pages/admin/backup/components/BackupRestoreDialog.tsx` | Dialog konfirmasi restore |
| `resources/js/pages/admin/backup/components/BackupImportDialog.tsx` | Dialog upload import SQL |
| `resources/js/pages/admin/backup/components/BackupSettingsDialog.tsx` | Dialog pengaturan MySQL/paths |
| `resources/js/pages/admin/backup/components/BackupProgressDialog.tsx` | Dialog progress bar |
| `resources/js/pages/admin/backup/components/BackupStats.tsx` | Stat cards (disk usage, total, last) |
| `resources/js/pages/admin/backup/hooks/useBackupOperations.ts` | Hook backup/restore/import operations |
| `resources/js/pages/admin/backup/hooks/useBackupProgress.ts` | Hook polling progress |
| `resources/js/pages/admin/backup/hooks/useBackupSettings.ts` | Hook settings form |
