Skip to content

Commit 75cde86

Browse files
committed
untested db backup and restore dev scripts
1 parent fb358c4 commit 75cde86

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

Server/scripts/backupDB.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { execSync } from "child_process";
2+
import { stdin, stdout } from "process";
3+
import { createInterface } from "readline";
4+
5+
if (process.env.NODE_ENV !== "development") {
6+
throw new Error("\x1b[41;1mTo prevent disasters, you can only run this script using NODE_ENV=development; you can't run this script in production.\nIf you are running this as a developer, run the script by writing \x1b[0m\x1b[42;1mNODE_ENV=development npm run backup\x1b[0m");
7+
}
8+
9+
function backupDatabase (backupName: string) {
10+
11+
const dockerExecCommand = `docker-compose exec database bash -c "mysqldump -u corsace -p corsace > /bitnami/mariadb/${backupName}.sql"`;
12+
13+
try {
14+
execSync(dockerExecCommand, { stdio: "inherit" });
15+
console.log(`Database backed up to ${backupName}.sql\nYou can run \`NODE_ENV=development npm run restore\` and input ${backupName} to restore the database to this state.`);
16+
} catch (error) {
17+
console.error(`Error executing command: ${dockerExecCommand}`, error);
18+
}
19+
}
20+
21+
createInterface(stdin, stdout).question("Enter backup name: ", backupName => {
22+
backupDatabase(backupName.replace(".sql", "").trim());
23+
process.exit(0);
24+
});

Server/scripts/init.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ readFile(configPath, "utf-8", async (err, data) => {
9999
if (!configData.database.host || !configData.database.port || !configData.database.database || !configData.database.username || !configData.database.password) {
100100
await getInfo("Database is not set, use default settings? (y/n) Default: y ", async (useDefault: string) => {
101101
if (useDefault.toLowerCase() === "n") {
102-
await getInfo("Database host: ", (host: string) => configData.database.host = host);
103-
await getInfo("Database port: ", (port: string) => configData.database.port = parseInt(port));
104-
await getInfo("Database name: ", (database: string) => configData.database.database = database);
105-
await getInfo("Database username: ", (username: string) => configData.database.username = username);
106-
await getInfo("Database password: ", (password: string) => configData.database.password = password);
102+
await getInfo("Database host (Example: localhost): ", (host: string) => configData.database.host = host);
103+
await getInfo("Database port (Example: 3306): ", (port: string) => configData.database.port = parseInt(port));
104+
await getInfo("Database name (Example: corsace): ", (database: string) => configData.database.database = database);
105+
await getInfo("Database username (Example: corsace): ", (username: string) => configData.database.username = username);
106+
await getInfo("Database password (Example: corsace): ", (password: string) => configData.database.password = password);
107107
return;
108108
}
109109

@@ -118,7 +118,7 @@ readFile(configPath, "utf-8", async (err, data) => {
118118
}
119119

120120
// Migration stuff
121-
await getInfo(`Start the database service from docker? ${redBackground}(Recommended)${resetCode} (y/n) Default: y `, (isDocker: string) => {
121+
await getInfo(`Start the database service from docker/Do you have docker installed? ${redBackground}(Recommended)${resetCode} (y/n) Default: y `, (isDocker: string) => {
122122
if (isDocker.toLowerCase() !== "n")
123123
exec("npm run database");
124124
});
@@ -189,4 +189,10 @@ readFile(configPath, "utf-8", async (err, data) => {
189189
exec("npm run init:tournament");
190190
});
191191
});
192+
193+
// Backup
194+
await getInfo(`Do you want to backup the database? ${redBackground}(Recommended)${resetCode} (y/n) Default: y `, (backup: string) => {
195+
if (backup.toLowerCase() !== "n")
196+
exec("NODE_ENV=development npm run backup");
197+
});
192198
});

Server/scripts/restoreDB.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { createInterface } from "readline";
2+
import ormConfig from "../../ormconfig";
3+
import { stdin, stdout } from "process";
4+
import { execSync } from "child_process";
5+
6+
if (process.env.NODE_ENV !== "development") {
7+
throw new Error("\x1b[41;1mTo prevent disasters, you can only run this script using NODE_ENV=development; you can't run this script in production.\nIf you are running this as a developer, run the script by writing \x1b[0m\x1b[42;1mNODE_ENV=development npm run restore\x1b[0m");
8+
}
9+
10+
ormConfig.initialize().then(async (dataSource) => {
11+
// Delete and recreate database
12+
await dataSource.dropDatabase();
13+
console.log("Database dropped");
14+
await dataSource.createQueryRunner().createDatabase("corsace");
15+
console.log("Database recreated");
16+
17+
createInterface(stdin, stdout).question("Enter backup name: ", backupName => {
18+
const dockerExecCommand = `docker-compose exec database bash -c "mysql -u corsace -p corsace < /bitnami/mariadb/${backupName.replace(".sql", "").trim()}.sql"`;
19+
20+
try {
21+
execSync(dockerExecCommand, { stdio: "inherit" });
22+
console.log(`Database restored from ${backupName.replace(".sql", "").trim()}.sql`);
23+
} catch (error) {
24+
console.error(`Error executing command: ${dockerExecCommand}`, error);
25+
}
26+
});
27+
28+
}).catch(console.error);

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@
9595
"init:tournament": "ts-node --files --project Server/tsconfig.json ./Server/scripts/initTournament.ts",
9696
"init:mca": "ts-node --files --project Server/tsconfig.json ./Server/scripts/initMCA.ts",
9797

98+
"backup": "ts-node --files --project Server/tsconfig.json ./Server/scripts/backupDB.ts",
99+
"restore": "ts-node --files --project Server/tsconfig.json ./Server/scripts/restoreDB.ts",
100+
98101
"build:ayim": "cd ./AYIM && nuxt build",
99102
"build:mca": "cd ./MCA && nuxt build",
100103
"build:main": "cd ./Main && nuxt build",

0 commit comments

Comments
 (0)