Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce backup and restore for postgres #37326

Open
wants to merge 28 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0906372
chore: Introduce backup and restore for postgres
abhvsn Nov 11, 2024
d52d62f
chore: Dummy server side change
abhvsn Nov 11, 2024
f304f6c
chore: Fix export DB
abhvsn Nov 12, 2024
5368a68
chore: Update import script for postgres DB restore
abhvsn Nov 12, 2024
be3f228
Merge branch 'release' into feat/backup-restore-pg
abhvsn Nov 13, 2024
74f96f0
fix: Remote and local postgres permissions
abhvsn Nov 13, 2024
a31fffe
Merge branch 'feat/backup-restore-pg' of github.com:appsmithorg/appsm…
abhvsn Nov 13, 2024
7933a05
Merge branch 'release' of github.com:appsmithorg/appsmith into feat/b…
abhvsn Nov 13, 2024
a6d9be8
chore: Only export appsmith schema to avoid backing up un-necessary s…
abhvsn Nov 13, 2024
032356b
chore: Minor refactors
abhvsn Nov 14, 2024
ecd642f
Merge branch 'release' of github.com:appsmithorg/appsmith into feat/b…
abhvsn Nov 14, 2024
da77268
chore: merge release branch
sharat87 Nov 29, 2024
c56c283
fix test
sharat87 Nov 29, 2024
cfba51d
Remove extension to pg-data
sharat87 Nov 29, 2024
9bda084
cosmetics
sharat87 Nov 29, 2024
b1faada
fix test
sharat87 Nov 30, 2024
4252c0e
fix test
sharat87 Nov 30, 2024
e03d925
fix comment
sharat87 Nov 30, 2024
3f10a2c
Merge branch 'release' into feat/backup-restore-pg
sharat87 Jan 27, 2025
552044c
Use Keycloak DB URL if DB URL is that of MongoDB
sharat87 Jan 27, 2025
00e8bff
Merge branch 'release' into feat/backup-restore-pg
sharat87 Feb 4, 2025
f849878
fix lint
sharat87 Feb 4, 2025
d338415
Merge branch 'release' into feat/backup-restore-pg
sharat87 Feb 4, 2025
5890ef7
chore: merge release branch
sharat87 Feb 6, 2025
4e54e91
fix: Fix Postgres backup & restore, tested on EE
sharat87 Feb 6, 2025
96c6331
chore: merge release branch
sharat87 Feb 6, 2025
1b43c4c
fmt
sharat87 Feb 6, 2025
9ff1b9c
fix: Fix case where Postgres URL is invalid
sharat87 Feb 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/client/packages/rts/src/ctl/backup/BackupState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export class BackupState {
readonly args: readonly string[];
readonly dbUrl: string;

readonly initAt: string = new Date().toISOString().replace(/:/g, "-");
readonly errors: string[] = [];

Expand All @@ -8,8 +10,9 @@ export class BackupState {

isEncryptionEnabled: boolean = false;

constructor(args: string[]) {
constructor(args: string[], url: string) {
this.args = Object.freeze([...args]);
this.dbUrl = url;

// We seal `this` so that no link in the chain can "add" new properties to the state. This is intentional. If any
// link wants to save data in the `BackupState`, which shouldn't even be needed in most cases, it should do so by
Expand Down
53 changes: 49 additions & 4 deletions app/client/packages/rts/src/ctl/backup/backup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
encryptBackupArchive,
executeCopyCMD,
executeMongoDumpCMD,
executePostgresDumpCMD,
getAvailableBackupSpaceInBytes,
getEncryptionPasswordFromUser,
getGitRoot,
Expand Down Expand Up @@ -66,6 +67,34 @@ describe("Backup Tests", () => {
console.log(res);
});

test("Test postgres dump CMD generation", async () => {
const dest = "/dest";
const url = "postgresql://username:password@host/appsmith";
const cmd = [
"pg_dump --host=host",
"--port=5432",
"--username=username",
"--dbname=appsmith",
"--schema=appsmith",
"--schema=public",
"--schema=temporal",
"--file=/dest/pg-data.sql",
"--verbose",
"--serializable-deferrable",
].join(" ");

const res = await executePostgresDumpCMD(dest, {
host: "host",
port: 5432,
username: "username",
password: "password",
database: "appsmith",
});

expect(res).toBe(cmd);
console.log(res);
});

test("Test get gitRoot path when APPSMITH_GIT_ROOT is '' ", () => {
expect(getGitRoot("")).toBe("/appsmith-stacks/git-storage");
});
Expand Down Expand Up @@ -246,7 +275,7 @@ test("Get DB name from Mongo URI 1", async () => {
const mongodb_uri =
"mongodb+srv://admin:[email protected]/my_db_name?retryWrites=true&minPoolSize=1&maxPoolSize=10&maxIdleTimeMS=900000&authSource=admin";
const expectedDBName = "my_db_name";
const dbName = utils.getDatabaseNameFromMongoURI(mongodb_uri);
const dbName = utils.getDatabaseNameFromUrl(mongodb_uri);

expect(dbName).toEqual(expectedDBName);
});
Expand All @@ -255,7 +284,7 @@ test("Get DB name from Mongo URI 2", async () => {
const mongodb_uri =
"mongodb+srv://admin:[email protected]/test123?retryWrites=true&minPoolSize=1&maxPoolSize=10&maxIdleTimeMS=900000&authSource=admin";
const expectedDBName = "test123";
const dbName = utils.getDatabaseNameFromMongoURI(mongodb_uri);
const dbName = utils.getDatabaseNameFromUrl(mongodb_uri);

expect(dbName).toEqual(expectedDBName);
});
Expand All @@ -264,15 +293,31 @@ test("Get DB name from Mongo URI 3", async () => {
const mongodb_uri =
"mongodb+srv://admin:[email protected]/test123";
const expectedDBName = "test123";
const dbName = utils.getDatabaseNameFromMongoURI(mongodb_uri);
const dbName = utils.getDatabaseNameFromUrl(mongodb_uri);

expect(dbName).toEqual(expectedDBName);
});

test("Get DB name from Mongo URI 4", async () => {
const mongodb_uri = "mongodb://appsmith:pAssW0rd!@localhost:27017/appsmith";
const expectedDBName = "appsmith";
const dbName = utils.getDatabaseNameFromMongoURI(mongodb_uri);
const dbName = utils.getDatabaseNameFromUrl(mongodb_uri);

expect(dbName).toEqual(expectedDBName);
});

test("Get DB name from Postgres URL", async () => {
const dbName = utils.getDatabaseNameFromUrl(
"postgresql://user:password@host:5432/postgres_db",
);

expect(dbName).toEqual("postgres_db");
});

test("Get DB name from Postgres URL with query params", async () => {
const dbName = utils.getDatabaseNameFromUrl(
"postgresql://user:password@host:5432/postgres_db?sslmode=disable",
);

expect(dbName).toEqual("postgres_db");
});
12 changes: 11 additions & 1 deletion app/client/packages/rts/src/ctl/backup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ import * as linkClasses from "./links";
import { BackupState } from "./BackupState";

export async function run(args: string[]) {
const url = utils.getDburl();

if (!url.startsWith("mongodb") && !url.startsWith("postgresql")) {
console.error("Only MongoDB and Postgres databases are supported.");
process.exitCode = 1;

return;
}

await utils.ensureSupervisorIsRunning();

const state: BackupState = new BackupState(args);
const state: BackupState = new BackupState(args, url);

const chain: Link[] = [
new linkClasses.BackupFolderLink(state),
new linkClasses.DiskSpaceLink(),
new linkClasses.ManifestLink(state),
new linkClasses.MongoDumpLink(state),
new linkClasses.PostgresDumpLink(state),
new linkClasses.GitStorageLink(state),
new linkClasses.EnvFileLink(state),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ManifestLink implements Link {
const version = await utils.getCurrentAppsmithVersion();
const manifestData = {
appsmithVersion: version,
dbName: utils.getDatabaseNameFromMongoURI(utils.getDburl()),
dbName: utils.getDatabaseNameFromUrl(utils.getDburl()),
};

await fsPromises.writeFile(
Expand Down
10 changes: 7 additions & 3 deletions app/client/packages/rts/src/ctl/backup/links/MongoDumpLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ export class MongoDumpLink implements Link {
constructor(private readonly state: BackupState) {}

async doBackup() {
console.log("Exporting database");
await executeMongoDumpCMD(this.state.backupRootPath, utils.getDburl());
console.log("Exporting database done.");
const url = this.state.dbUrl;

if (url.startsWith("mongodb")) {
console.log("Exporting database");
await executeMongoDumpCMD(this.state.backupRootPath, url);
console.log("Exporting database done.");
}
}
}

Expand Down
150 changes: 150 additions & 0 deletions app/client/packages/rts/src/ctl/backup/links/PostgresDumpLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import type { Link } from ".";
import type { BackupState } from "../BackupState";
import * as utils from "../../utils";

interface ConnectionDetails {
host: string;
port: number;
username: string;
password: string;
database: string;
}

/**
* Backup & restore for Postgres database data using `pg_dump` and `psql`.
*/
export class PostgresDumpLink implements Link {
private postgresUrl: null | ConnectionDetails = null;

constructor(private readonly state: BackupState) {}

async preBackup() {
const url = this.state.dbUrl;

if (url.startsWith("postgresql")) {
this.postgresUrl = parsePostgresUrl(url);

return;
}

if (process.env.APPSMITH_KEYCLOAK_DB_URL) {
const dbUrlFromEnv = process.env.APPSMITH_KEYCLOAK_DB_URL;

if (dbUrlFromEnv.startsWith("postgresql://")) {
this.postgresUrl = parsePostgresUrl(dbUrlFromEnv);
} else if (dbUrlFromEnv.includes("/")) {
// then it's just the hostname and database in there
const [host, database] = dbUrlFromEnv.split("/");
this.postgresUrl = {
host,
port: 5432,
username: process.env.APPSMITH_KEYCLOAK_DB_USERNAME,
password: process.env.APPSMITH_KEYCLOAK_DB_PASSWORD,
database,
};
} else {
// Identify this as an invalid value for this env variable.
// But we ignore this fact for now, since Postgres itself is not a critical component yet.
console.warn(
"APPSMITH_KEYCLOAK_DB_URL is set, but it doesn't start with postgresql://. This is not a valid value for this env variable. But we ignore this fact for now, since Postgres itself is not a critical component yet.",
);
}
} else if (process.env.APPSMITH_ENABLE_EMBEDDED_DB !== "0") {
this.postgresUrl = {
// Get unix_socket_directories from postgresql.conf, like in pg-utils.sh/get_unix_socket_directory.
// Unix socket directory
host: "/var/run/postgresql",
port: 5432,
username: "postgres",
password: process.env.APPSMITH_TEMPORAL_PASSWORD,
database: "appsmith",
};
} else {
throw new Error("No Postgres DB URL found");
}
}

async doBackup() {
if (this.postgresUrl) {
await executePostgresDumpCMD(this.state.backupRootPath, this.postgresUrl);
}
}

async doRestore(restoreContentsPath: string) {
const env = {
...process.env,
};

const cmd = ["psql", "-v", "ON_ERROR_STOP=1"];

const isLocalhost = ["localhost", "127.0.0.1"].includes(
this.postgresUrl.host,
);

if (isLocalhost) {
env.PGHOST = "/var/run/postgresql";
env.PGPORT = "5432";
env.PGUSER = "postgres";
env.PGPASSWORD = process.env.APPSMITH_TEMPORAL_PASSWORD;
env.PGDATABASE = this.postgresUrl.database;
} else {
env.PGHOST = this.postgresUrl.host;
env.PGPORT = this.postgresUrl.port.toString();
env.PGUSER = this.postgresUrl.username;
env.PGPASSWORD = this.postgresUrl.password;
env.PGDATABASE = this.postgresUrl.database;
}

await utils.execCommand(
[
...cmd,
"--command=DROP SCHEMA IF EXISTS public CASCADE; DROP SCHEMA IF EXISTS appsmith CASCADE; DROP SCHEMA IF EXISTS temporal CASCADE;",
],
{ env },
);

await utils.execCommand(
[...cmd, `--file=${restoreContentsPath}/pg-data.sql`],
{ env },
);
console.log("Restoring Postgres database completed");
}
}

function parsePostgresUrl(url: string): ConnectionDetails {
const parsed = new URL(url);
return {
host: parsed.hostname,
port: parseInt(parsed.port || "5432"),
username: parsed.username,
password: decodeURIComponent(parsed.password),
database: parsed.pathname.substring(1),
};
}

export async function executePostgresDumpCMD(
destFolder: string,
details: ConnectionDetails,
) {
const args = [
"pg_dump",
`--host=${details.host}`,
`--port=${details.port || "5432"}`,
`--username=${details.username}`,
`--dbname=${details.database}`,
"--schema=appsmith",
"--schema=public", // Keycloak
"--schema=temporal",
`--file=${destFolder}/pg-data.sql`,
"--verbose",
"--serializable-deferrable",
];

// Set password in environment since it's not allowed in the CLI
const env = {
...process.env,
PGPASSWORD: details.password,
};

return await utils.execCommand(args, { env });
}
1 change: 1 addition & 0 deletions app/client/packages/rts/src/ctl/backup/links/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export * from "./EnvFileLink";
export * from "./GitStorageLink";
export * from "./ManifestLink";
export * from "./MongoDumpLink";
export * from "./PostgresDumpLink";
Loading
Loading