Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add PlanetScale backup cron job (#3704)

Steven Teyc0ffbede0599
diff --git a/apps/web/.env.example b/apps/web/.env.example
index 3fae905b48..d1a2891e29 100644
--- a/apps/web/.env.example
+++ b/apps/web/.env.example
@@ -66,8 +66,11 @@ STRIPE_WEBHOOK_SECRET=
STRIPE_CONNECT_WEBHOOK_SECRET=
STRIPE_CONNECT_V2_WEBHOOK_SECRET=
-# Stripe App webhook events
+# Stripe Integration
STRIPE_APP_WEBHOOK_SECRET=
+STRIPE_APP_SECRET_KEY=
+STRIPE_APP_SECRET_KEY_TEST=
+STRIPE_APP_SECRET_KEY_SANDBOX=
# Shopify App webhook events
SHOPIFY_WEBHOOK_SECRET=
@@ -172,16 +175,12 @@ PAYPAL_CLIENT_ID=
PAYPAL_CLIENT_SECRET=
PAYPAL_WEBHOOK_ID=
-# Stripe Integration
-STRIPE_APP_SECRET_KEY_TEST=
-STRIPE_APP_SECRET_KEY=
-STRIPE_APP_SECRET_KEY_SANDBOX=
-
# Program lander generation
FIRECRAWL_API_KEY=
-# Set a global token to identify Singular events
-SINGULAR_WEBHOOK_TOKEN=
+# for generating planetscale backups (https://planetscale.com/docs/api/service-tokens)
+PLANETSCALE_SERVICE_TOKEN_ID=
+PLANETSCALE_SERVICE_TOKEN=
# Hubspot
HUBSPOT_CLIENT_ID=
diff --git a/apps/web/app/(ee)/api/cron/trigger-backup/route.ts b/apps/web/app/(ee)/api/cron/trigger-backup/route.ts
new file mode 100644
index 0000000000..ebcc409d6b
--- /dev/null
+++ b/apps/web/app/(ee)/api/cron/trigger-backup/route.ts
@@ -0,0 +1,57 @@
+import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { verifyVercelSignature } from "@/lib/cron/verify-vercel";
+import { logAndRespond } from "../utils";
+
+export const dynamic = "force-dynamic";
+
+// Creates a PlanetScale branch backup
+// Scheduled UTC: 12:40 AM, 8:40 AM, 12:40 PM, 8:40 PM (cron: 40 0,8,12,20 * * *)
+
+export async function GET(req: Request) {
+ try {
+ await verifyVercelSignature(req);
+
+ const tokenSecret = process.env.PLANETSCALE_SERVICE_TOKEN;
+
+ if (!tokenSecret) {
+ throw new DubApiError({
+ code: "internal_server_error",
+ message: "Missing PLANETSCALE_SERVICE_TOKEN",
+ });
+ }
+
+ const res = await fetch(
+ `https://api.planetscale.com/v1/organizations/dub/databases/dub/branches/main/backups`,
+ {
+ method: "POST",
+ headers: {
+ Authorization: `jynn53mcug4l:${tokenSecret}`,
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ name: `cron-backup-${new Date().toISOString()}`,
+ retention_value: 7,
+ retention_unit: "day",
+ }),
+ },
+ );
+
+ const bodyText = await res.text();
+
+ if (!res.ok) {
+ throw new DubApiError({
+ code: "internal_server_error",
+ message: `PlanetScale backup failed (${res.status}): ${bodyText}`,
+ });
+ }
+
+ const backup = JSON.parse(bodyText);
+ console.log({ backup });
+
+ return logAndRespond(
+ `Created PlanetScale backup: ${backup.id} (${backup.name}) state=${backup.state}`,
+ );
+ } catch (error) {
+ return handleAndReturnErrorResponse(error);
+ }
+}
diff --git a/apps/web/vercel.json b/apps/web/vercel.json
index 6898c24d6b..ccd1f32643 100644
--- a/apps/web/vercel.json
+++ b/apps/web/vercel.json
@@ -40,6 +40,10 @@
"path": "/api/cron/trigger-withdrawal",
"schedule": "0 0,12 * * *"
},
+ {
+ "path": "/api/cron/trigger-backup",
+ "schedule": "40 0,8,12,20 * * *"
+ },
{
"path": "/api/cron/payouts/force-withdrawals",
"schedule": "0 12 * * *"