𝗕𝗮𝗰𝗸𝗶𝗻𝗴 𝗨𝗽 𝗔𝗻 𝗔𝗣𝗜 𝗚𝗮𝘁𝗲𝘄𝗮𝘆 𝗖𝗼𝗻𝗳𝗶𝗴
Managing multiple API gateways requires more than simple file exports. When you manage several environments like production and staging from one place, you need a system that allows for scheduled snapshots and specific rollbacks.
I built a backup and restore feature for a Laravel app that manages Kong API gateways. Here is how I structured it to ensure safety and reliability.
𝗧𝗵𝗲 𝗠𝗼𝗱𝗲𝗹 A simple file export fails when you need history and status tracking. I created a KongConnectionBackup model. This model tracks: • A unique ID for every backup. • A link to a specific connection. • The captured configuration payload. • The current status of the backup.
𝗜𝘀𝗼𝗹𝗮𝘁𝗲𝗱 𝗦𝘁𝗮𝘁𝘂𝘀 𝗠𝗮𝗰𝗵𝗶𝗻𝗲 I used Enums to manage the backup state. This prevents errors in the UI. The statuses are: • Pending • Running • Completed • Failed
The Enum handles its own labels and colors. This keeps the frontend code clean. The view simply asks the Enum how to look.
𝗔𝘀𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗼𝘂𝘀 𝗪𝗼𝗿𝗸 Backing up a live gateway is slow. Network calls and large payloads can cause timeouts. I moved the work into a queued job.
The process follows a strict contract:
- Create a row with a Pending status.
- The job starts and changes the status to Running.
- If the service succeeds, the status becomes Completed.
- If the service fails, the status becomes Failed.
I always re-throw exceptions after marking a backup as Failed. This ensures the error shows up in your failed job logs instead of disappearing silently.
𝗦𝗮𝗳𝗲 𝗥𝗲𝘀𝘁𝗼𝗿𝗲𝘀 Backups are easy to retry. Restores are dangerous because they push state back to a live system. I added two guardrails: • Section awareness: You can restore specific parts like plugins without touching users. • Reconciliation: The system compares the snapshot to the live state instead of performing a blind overwrite.
𝗧𝗲𝘀𝘁𝗶𝗻𝗴 𝗦𝘁𝗿𝗮𝘁𝗲𝗴𝘆 I use Pest to test the status machine in isolation. I mock the service to ensure the backup moves from Pending to Completed correctly. I also test that the status moves to Failed when an error occurs. This allows me to test logic without needing a real gateway.
𝗧𝗵𝗲 𝗟𝗲𝘀𝘀𝗼𝗻 Model the artifact clearly. Push heavy work into background jobs. Use Enums for state. Spend most of your testing effort on the restore process, because mistakes there are much harder to fix.
Source: https://dev.to/nasrulhazim/backing-up-an-api-gateways-config-per-connection-restore-safe-1efj