mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	changed backup to simple scheme with one daily, one weekly and one monthly backup, fixes #15
This commit is contained in:
		
							
								
								
									
										0
									
								
								bin/push-docker-image.sh
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										0
									
								
								bin/push-docker-image.sh
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
								
								
									
										9
									
								
								db/migrations/0108__new_backup_options.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								db/migrations/0108__new_backup_options.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | UPDATE options SET name = 'lastDailyBackupDate' WHERE name = 'lastBackupDate'; | ||||||
|  |  | ||||||
|  | INSERT INTO options (name, value, dateCreated, dateModified, isSynced) | ||||||
|  | VALUES ('lastWeeklyBackupDate', '2018-07-29T18:31:00.874Z', '2018-07-29T18:31:00.874Z', '2018-07-29T18:31:00.874Z', 0); | ||||||
|  |  | ||||||
|  | INSERT INTO options (name, value, dateCreated, dateModified, isSynced) | ||||||
|  | VALUES ('lastMonthlyBackupDate', '2018-07-29T18:31:00.874Z', '2018-07-29T18:31:00.874Z', '2018-07-29T18:31:00.874Z', 0); | ||||||
|  |  | ||||||
|  | -- these options are not synced so no need to fix sync rows | ||||||
| @@ -3,7 +3,7 @@ | |||||||
| const build = require('./build'); | const build = require('./build'); | ||||||
| const packageJson = require('../../package'); | const packageJson = require('../../package'); | ||||||
|  |  | ||||||
| const APP_DB_VERSION = 107; | const APP_DB_VERSION = 108; | ||||||
| const SYNC_VERSION = 1; | const SYNC_VERSION = 1; | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   | |||||||
| @@ -10,49 +10,32 @@ const syncMutexService = require('./sync_mutex'); | |||||||
| const cls = require('./cls'); | const cls = require('./cls'); | ||||||
|  |  | ||||||
| async function regularBackup() { | async function regularBackup() { | ||||||
|     const now = new Date(); |     await periodBackup('lastDailyBackupDate', 'daily', 24 * 3600); | ||||||
|     const lastBackupDate = dateUtils.parseDateTime(await optionService.getOption('lastBackupDate')); |  | ||||||
|  |  | ||||||
|     console.log(lastBackupDate); |     await periodBackup('lastWeeklyBackupDate', 'weekly', 7 * 24 * 3600); | ||||||
|  |  | ||||||
|     if (now.getTime() - lastBackupDate.getTime() > 43200 * 1000) { |     await periodBackup('lastMonthlyBackupDate', 'monthly', 30 * 24 * 3600); | ||||||
|         await backupNow(); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     await cleanupOldBackups(); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| async function backupNow() { | async function periodBackup(optionName, fileName, periodInSeconds) { | ||||||
|     // we don't want to backup DB in the middle of sync with potentially inconsistent DB state |     const now = new Date(); | ||||||
|  |     const lastDailyBackupDate = dateUtils.parseDateTime(await optionService.getOption(optionName)); | ||||||
|  |  | ||||||
|  |     if (now.getTime() - lastDailyBackupDate.getTime() > periodInSeconds * 1000) { | ||||||
|  |         await backupNow(fileName); | ||||||
|  |  | ||||||
|  |         await optionService.setOption(optionName, dateUtils.nowDate()); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | async function backupNow(name) { | ||||||
|  |     // we don't want to backup DB in the middle of sync with potentially inconsistent DB state | ||||||
|     await syncMutexService.doExclusively(async () => { |     await syncMutexService.doExclusively(async () => { | ||||||
|         const backupFile = dataDir.BACKUP_DIR + "/" + "backup-" + dateUtils.getDateTimeForFile() + ".db"; |         const backupFile = `${dataDir.BACKUP_DIR}/backup-${name}.db`; | ||||||
|  |  | ||||||
|         fs.copySync(dataDir.DOCUMENT_PATH, backupFile); |         fs.copySync(dataDir.DOCUMENT_PATH, backupFile); | ||||||
|  |  | ||||||
|         log.info("Created backup at " + backupFile); |         log.info("Created backup at " + backupFile); | ||||||
|  |  | ||||||
|         await optionService.setOption('lastBackupDate', dateUtils.nowDate()); |  | ||||||
|     }); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async function cleanupOldBackups() { |  | ||||||
|     const now = new Date(); |  | ||||||
|  |  | ||||||
|     fs.readdirSync(dataDir.BACKUP_DIR).forEach(file => { |  | ||||||
|         const match = file.match(/backup-([0-9 -:]+)\.db/); |  | ||||||
|  |  | ||||||
|         if (match) { |  | ||||||
|             const date_str = match[1]; |  | ||||||
|  |  | ||||||
|             const date = Date.parse(date_str); |  | ||||||
|  |  | ||||||
|             if (now.getTime() - date.getTime() > 30 * 24 * 3600 * 1000) { |  | ||||||
|                 log.info("Removing old backup - " + file); |  | ||||||
|  |  | ||||||
|                 fs.unlink(dataDir.BACKUP_DIR + "/" + file); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -10,7 +10,7 @@ async function migrate() { | |||||||
|     const migrations = []; |     const migrations = []; | ||||||
|  |  | ||||||
|     // backup before attempting migration |     // backup before attempting migration | ||||||
|     await backupService.backupNow(); |     await backupService.backupNow("before-migration"); | ||||||
|  |  | ||||||
|     const currentDbVersion = parseInt(await optionService.getOption('dbVersion')); |     const currentDbVersion = parseInt(await optionService.getOption('dbVersion')); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -31,7 +31,9 @@ async function initSyncedOptions(username, password) { | |||||||
|  |  | ||||||
| async function initNotSyncedOptions(initialized, startNotePath = 'root', syncServerHost = '', syncProxy = '') { | async function initNotSyncedOptions(initialized, startNotePath = 'root', syncServerHost = '', syncProxy = '') { | ||||||
|     await optionService.createOption('startNotePath', startNotePath, false); |     await optionService.createOption('startNotePath', startNotePath, false); | ||||||
|     await optionService.createOption('lastBackupDate', dateUtils.nowDate(), false); |     await optionService.createOption('lastDailyBackupDate', dateUtils.nowDate(), false); | ||||||
|  |     await optionService.createOption('lastWeeklyBackupDate', dateUtils.nowDate(), false); | ||||||
|  |     await optionService.createOption('lastMonthlyBackupDate', dateUtils.nowDate(), false); | ||||||
|     await optionService.createOption('dbVersion', appInfo.dbVersion, false); |     await optionService.createOption('dbVersion', appInfo.dbVersion, false); | ||||||
|  |  | ||||||
|     await optionService.createOption('lastSyncedPull', 0, false); |     await optionService.createOption('lastSyncedPull', 0, false); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user