Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into taproot-swaps
  • Loading branch information
michael1011 committed Jan 14, 2024
2 parents 798b4a7 + 13c9b4f commit ce6c526
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
8 changes: 8 additions & 0 deletions docker/boltz/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ RUN npm run compile

FROM node:${NODE_VERSION} AS final

RUN apt-get update && apt-get -y upgrade && \
apt-get -y install gnupg2 wget lsb-release && \
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' && \
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
apt-get update && \
apt-get -y install postgresql-client-14 postgresql-client-common && \
apt-get clean all && rm -rf /var/lib/apt/lists/*

COPY --from=builder /boltz-backend/bin /boltz-backend/bin
COPY --from=builder /boltz-backend/dist /boltz-backend/dist
COPY --from=builder /boltz-backend/node_modules /boltz-backend/node_modules
Expand Down
1 change: 1 addition & 0 deletions lib/Boltz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class Boltz {
this.backup = new BackupScheduler(
this.logger,
this.config.dbpath,
this.config.postgres,
this.config.backup,
this.service.eventHandler,
);
Expand Down
45 changes: 38 additions & 7 deletions lib/backup/BackupScheduler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { exec } from 'child_process';
import { unlinkSync } from 'fs';
import { scheduleJob } from 'node-schedule';
import { BackupConfig } from '../Config';
import { BackupConfig, PostgresConfig } from '../Config';
import Logger from '../Logger';
import { formatError } from '../Utils';
import Database, { DatabaseType } from '../db/Database';
import EventHandler from '../service/EventHandler';
import Errors from './Errors';
import GoogleCloud from './providers/GoogleCloud';
Expand All @@ -16,10 +19,11 @@ class BackupScheduler {
private readonly providers: BackupProvider[] = [];

constructor(
private logger: Logger,
private dbpath: string,
private config: BackupConfig,
private eventHandler: EventHandler,
private readonly logger: Logger,
private readonly dbpath: string,
private readonly postgresConfig: PostgresConfig | undefined,
private readonly config: BackupConfig,
private readonly eventHandler: EventHandler,
) {
try {
if (config.gcloud && GoogleCloud.configValid(config.gcloud)) {
Expand Down Expand Up @@ -90,9 +94,36 @@ class BackupScheduler {
}

const dateString = BackupScheduler.getDate(date);
this.logger.silly(`Backing up databases at: ${dateString}`);
this.logger.silly(`Backing up ${Database.type} database at: ${dateString}`);

await this.uploadFile(`backend/database-${dateString}.db`, this.dbpath);
const backupPath = `backend/database-${dateString}.${Database.type === DatabaseType.SQLite ? 'db' : 'sql.gz'}`;

if (Database.type === DatabaseType.SQLite) {
await this.uploadFile(backupPath, this.dbpath);
} else {
const tempFilePath = `sql-backup-${Date.now().toString()}.temp`;

return new Promise<void>((resolve, reject) => {
const backupChild = exec(
`PGPASSWORD="${this.postgresConfig!.password}" pg_dump -U ${this.postgresConfig!.username} -h ${this.postgresConfig!.host} -p ${this.postgresConfig!.port} -d ${this.postgresConfig!.database} | gzip > ${tempFilePath}`,
);
backupChild.on('exit', (code) => {
if (code !== 0) {
reject(
`creating ${DatabaseType.PostgreSQL} backup failed with code: ${code}`,
);
return;
}

this.uploadFile(backupPath, tempFilePath)
.then(() => {
unlinkSync(tempFilePath);
resolve();
})
.catch(reject);
});
});
}
};

private uploadFile = async (path: string, file: string) => {
Expand Down
4 changes: 4 additions & 0 deletions test/unit/backup/BackupScheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BackupConfig } from '../../../lib/Config';
import Logger from '../../../lib/Logger';
import BackupScheduler from '../../../lib/backup/BackupScheduler';
import Webdav from '../../../lib/backup/providers/Webdav';
import Database, { DatabaseType } from '../../../lib/db/Database';
import EventHandler from '../../../lib/service/EventHandler';

type callback = (currency: string, channelBackup: string) => void;
Expand Down Expand Up @@ -71,11 +72,13 @@ describe('BackupScheduler', () => {
const backupScheduler = new BackupScheduler(
mockedLogger(),
dbPath,
undefined,
backupConfig,
eventHandler,
);

beforeAll(() => {
Database.type = DatabaseType.SQLite;
backupScheduler['providers'].push(mockedWebdav());
});

Expand Down Expand Up @@ -147,6 +150,7 @@ describe('BackupScheduler', () => {
new BackupScheduler(
mockedLogger(),
dbPath,
undefined,
{
interval: '0 0 * * *',

Expand Down

0 comments on commit ce6c526

Please sign in to comment.