Integration · Discord bots
Your bot’s data,
safe off the server.
A discord.js bot that backs up its data file to OzVault on a schedule, encrypted with your vault key before it leaves the machine. It keeps the newest backups, deletes old ones for good, and restores with one slash command.
// bot.js (excerpt): one backup at a time, keep the newest BACKUP_KEEP
function backupNow(reason) {
running ??= (async () => {
try {
const data = await readFile(DATA_FILE);
const result = await vault.backup(BUCKET, new Blob([data]), VAULT_KEY, {
name: NAME, // e.g. bot-data.json
keep: KEEP, // older backups are deleted for good
});
console.log(`[backup] ${reason}: ${result.object.name}`);
return result;
} finally {
running = null;
}
})();
return running;
}
setInterval(() => backupNow("scheduled"), EVERY_HOURS * 3600_000);What it does
Backups you don’t have to remember.
On a schedule
Every few hours (default 6), a minute after start-up, and once more on shutdown.Rotating
Keeps the newest 14 by default. Older backups are deleted for good, freeing your quota.Encrypted
AES-256-GCM on your machine. OzVault stores only ciphertext.Slash commands
/backup, /backups and /restore confirm:true, for administrators only.
Set up
Five minutes, start to finish.
- 01
Create the project
ozvault init discord-js my-bot-backups - 02
Fill in .env
Your bot token and IDs, an OzVault API key (objects:read and objects:write, limited to one bucket), your vault key (ozvault keygen), the bucket ID, andBACKUP_FILE: the file your bot keeps its data in. - 03
Register commands and start
npm install && npm run register && npm start
Good to know
Backing up live data safely.
SQLite and other live databases: copying a database file while it’s being written can capture a half-written state. Back up a consistent snapshot instead, e.g.
VACUUM INTO 'data/snapshot.sqlite', and point BACKUP_FILE at the snapshot.Keep the vault key off the bot’s machine too. If the server dies, you need the key to decrypt your backups, and nobody, including OzVault, can recover it for you.
Back up anything else from a terminal.
One cron line with the ozvault CLI keeps rotating, encrypted backups of any file.