-
-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:koush/scrypted
- Loading branch information
Showing
4 changed files
with
70 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import Level from '../level'; | ||
import { sleep } from '../sleep'; | ||
import { getScryptedVolume } from '../plugin/plugin-volume'; | ||
import AdmZip from 'adm-zip'; | ||
import { ScryptedRuntime } from '../runtime'; | ||
|
||
export class Backup { | ||
constructor(public runtime: ScryptedRuntime) {} | ||
|
||
async createBackup(): Promise<Buffer> { | ||
const volumeDir = getScryptedVolume(); | ||
|
||
const backupDbPath = path.join(volumeDir, 'backup.db'); | ||
await fs.promises.rm(backupDbPath, { | ||
recursive: true, | ||
force: true, | ||
}); | ||
|
||
const backupDb = new Level(backupDbPath); | ||
await backupDb.open(); | ||
for await (const [key, value] of this.runtime.datastore.iterator()) { | ||
await backupDb.put(key, value); | ||
} | ||
await backupDb.close(); | ||
|
||
const backupZip = path.join(volumeDir, 'backup.zip'); | ||
await fs.promises.rm(backupZip, { | ||
recursive: true, | ||
force: true, | ||
}); | ||
|
||
const zip = new AdmZip(); | ||
await zip.addLocalFolderPromise(backupDbPath, {}); | ||
return zip.toBufferPromise(); | ||
} | ||
|
||
async restore(b: Buffer): Promise<void> { | ||
const volumeDir = getScryptedVolume(); | ||
const dbPath = path.join(volumeDir, 'scrypted.db'); | ||
|
||
const zip = new AdmZip(b); | ||
if (!zip.test()) | ||
throw new Error('backup zip test failed.'); | ||
|
||
this.runtime.kill(); | ||
await sleep(5000); | ||
await this.runtime.datastore.close(); | ||
|
||
await fs.promises.rm(volumeDir, { | ||
recursive: true, | ||
force: true, | ||
}); | ||
|
||
await fs.promises.mkdir(volumeDir, { | ||
recursive: true | ||
}); | ||
|
||
zip.extractAllTo(dbPath, true); | ||
this.runtime.serviceControl.restart(); | ||
} | ||
} |