mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Write settings backup on starting the server
This commit is contained in:
@ -3,4 +3,5 @@ node_modules
|
|||||||
npm-debug.log
|
npm-debug.log
|
||||||
readme*
|
readme*
|
||||||
Start.bat
|
Start.bat
|
||||||
/dist
|
/dist
|
||||||
|
/backups/
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -20,3 +20,4 @@ whitelist.txt
|
|||||||
secrets.json
|
secrets.json
|
||||||
/dist
|
/dist
|
||||||
poe_device.json
|
poe_device.json
|
||||||
|
/backups/
|
||||||
|
@ -5,3 +5,4 @@ node_modules/
|
|||||||
secrets.json
|
secrets.json
|
||||||
/dist
|
/dist
|
||||||
poe_device.json
|
poe_device.json
|
||||||
|
/backups/
|
||||||
|
37
server.js
37
server.js
@ -256,6 +256,7 @@ const directories = {
|
|||||||
extensions: 'public/scripts/extensions',
|
extensions: 'public/scripts/extensions',
|
||||||
instruct: 'public/instruct',
|
instruct: 'public/instruct',
|
||||||
context: 'public/context',
|
context: 'public/context',
|
||||||
|
backups: 'backups/',
|
||||||
};
|
};
|
||||||
|
|
||||||
// CSRF Protection //
|
// CSRF Protection //
|
||||||
@ -3335,6 +3336,7 @@ const setupTasks = async function () {
|
|||||||
|
|
||||||
console.log(`SillyTavern ${version.pkgVersion}` + (version.gitBranch ? ` '${version.gitBranch}' (${version.gitRevision})` : ''));
|
console.log(`SillyTavern ${version.pkgVersion}` + (version.gitBranch ? ` '${version.gitBranch}' (${version.gitRevision})` : ''));
|
||||||
|
|
||||||
|
backupSettings();
|
||||||
migrateSecrets();
|
migrateSecrets();
|
||||||
ensurePublicDirectoriesExist();
|
ensurePublicDirectoriesExist();
|
||||||
await ensureThumbnailCache();
|
await ensureThumbnailCache();
|
||||||
@ -3426,6 +3428,41 @@ async function convertWebp() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function backupSettings() {
|
||||||
|
const MAX_BACKUPS = 25;
|
||||||
|
|
||||||
|
function generateTimestamp() {
|
||||||
|
const now = new Date();
|
||||||
|
const year = now.getFullYear();
|
||||||
|
const month = String(now.getMonth() + 1).padStart(2, '0');
|
||||||
|
const day = String(now.getDate()).padStart(2, '0');
|
||||||
|
const hours = String(now.getHours()).padStart(2, '0');
|
||||||
|
const minutes = String(now.getMinutes()).padStart(2, '0');
|
||||||
|
const seconds = String(now.getSeconds()).padStart(2, '0');
|
||||||
|
|
||||||
|
return `${year}${month}${day}-${hours}${minutes}${seconds}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!fs.existsSync(directories.backups)) {
|
||||||
|
fs.mkdirSync(directories.backups);
|
||||||
|
}
|
||||||
|
|
||||||
|
const backupFile = path.join(directories.backups, `settings_${generateTimestamp()}.json`);
|
||||||
|
fs.copyFileSync(SETTINGS_FILE, backupFile);
|
||||||
|
|
||||||
|
let files = fs.readdirSync(directories.backups);
|
||||||
|
if (files.length > MAX_BACKUPS) {
|
||||||
|
files = files.map(f => path.join(directories.backups, f));
|
||||||
|
files.sort((a, b) => fs.statSync(a).mtimeMs - fs.statSync(b).mtimeMs);
|
||||||
|
|
||||||
|
fs.rmSync(files[0]);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.log('Could not backup settings file', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function ensurePublicDirectoriesExist() {
|
function ensurePublicDirectoriesExist() {
|
||||||
for (const dir of Object.values(directories)) {
|
for (const dir of Object.values(directories)) {
|
||||||
if (!fs.existsSync(dir)) {
|
if (!fs.existsSync(dir)) {
|
||||||
|
Reference in New Issue
Block a user