upload db backups to the same S3 bucket and configured lifecycle rules in AWS S3 web UI to automatically remove older backups
This commit is contained in:
parent
96f4b2869e
commit
3f279634b6
20
db/backup.ts
20
db/backup.ts
@ -10,9 +10,9 @@ import appLogger from "../integrations/logger";
|
|||||||
const logger = appLogger.child({ module: "backup" });
|
const logger = appLogger.child({ module: "backup" });
|
||||||
|
|
||||||
export default async function backup(schedule: "daily" | "weekly" | "monthly") {
|
export default async function backup(schedule: "daily" | "weekly" | "monthly") {
|
||||||
const s3Bucket = `shellphone-${schedule}-backup`;
|
const s3Bucket = "shellphone-backups";
|
||||||
const { database, host, port, user, password } = parseDatabaseUrl(process.env.DATABASE_URL!);
|
const { database, host, port, user, password } = parseDatabaseUrl(process.env.DATABASE_URL!);
|
||||||
const fileName = getFileName(database);
|
const fileName = `${schedule}-${database}.sql.gz`;
|
||||||
|
|
||||||
console.log(`Dumping database ${database}`);
|
console.log(`Dumping database ${database}`);
|
||||||
const pgDumpChild = spawn("pg_dump", [`-U${user}`, `-d${database}`], {
|
const pgDumpChild = spawn("pg_dump", [`-U${user}`, `-d${database}`], {
|
||||||
@ -75,22 +75,6 @@ export default async function backup(schedule: "daily" | "weekly" | "monthly") {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function pad(number: number) {
|
|
||||||
return number.toString().padStart(2, "0");
|
|
||||||
}
|
|
||||||
|
|
||||||
function getFileName(database: string) {
|
|
||||||
const now = new Date();
|
|
||||||
const year = now.getUTCFullYear();
|
|
||||||
const month = pad(now.getUTCMonth() + 1);
|
|
||||||
const day = pad(now.getUTCDate());
|
|
||||||
const hours = pad(now.getUTCHours());
|
|
||||||
const minutes = pad(now.getUTCMinutes());
|
|
||||||
const seconds = pad(now.getUTCSeconds());
|
|
||||||
|
|
||||||
return `${database}-${year}-${month}-${day}_${hours}-${minutes}-${seconds}.sql.gz`; // 2021-09-15_16-00-02.sql.gz
|
|
||||||
}
|
|
||||||
|
|
||||||
type DatabaseUrl = {
|
type DatabaseUrl = {
|
||||||
readonly user: string;
|
readonly user: string;
|
||||||
readonly password: string;
|
readonly password: string;
|
||||||
|
Loading…
Reference in New Issue
Block a user