refactor: encapsulate startup logic and improve backup scheduling in index.js
This commit is contained in:
45
download.js
Normal file
45
download.js
Normal file
@@ -0,0 +1,45 @@
|
||||
require('dotenv').config();
|
||||
const {
|
||||
downloadBackupFile,
|
||||
compressBackupFile,
|
||||
formatBytes,
|
||||
sendTelegramBroadcast
|
||||
} = require('./index.js');
|
||||
|
||||
async function downloadOnly() {
|
||||
try {
|
||||
console.log('Starting download process...');
|
||||
|
||||
// Download backup file from SMB share
|
||||
const localBackupFile = await downloadBackupFile();
|
||||
console.log('Download completed:', localBackupFile);
|
||||
|
||||
// Optionally compress the file
|
||||
if (process.argv.includes('--compress')) {
|
||||
const compressedFile = await compressBackupFile(localBackupFile);
|
||||
console.log('Compression completed:', compressedFile);
|
||||
|
||||
// Optionally send notification
|
||||
if (process.argv.includes('--notify')) {
|
||||
await sendTelegramBroadcast('all', `Backup downloaded and compressed ✅\nFile: ${compressedFile}`);
|
||||
}
|
||||
} else if (process.argv.includes('--notify')) {
|
||||
await sendTelegramBroadcast('all', `Backup downloaded ✅\nFile: ${localBackupFile}`);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Download failed:', error);
|
||||
|
||||
// Optionally notify on error
|
||||
if (process.argv.includes('--notify')) {
|
||||
await sendTelegramBroadcast('errors', `Download failed 🔴\nError: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Run if called directly
|
||||
if (require.main === module) {
|
||||
downloadOnly();
|
||||
}
|
||||
|
||||
module.exports = { downloadOnly };
|
||||
17
index.js
17
index.js
@@ -310,6 +310,8 @@ async function runBackupProcess() {
|
||||
}
|
||||
}
|
||||
|
||||
// Only run startup logic if this file is executed directly (not imported)
|
||||
if (require.main === module) {
|
||||
// Run backup immediately when starting
|
||||
(async () => {
|
||||
try {
|
||||
@@ -336,7 +338,6 @@ async function runBackupProcess() {
|
||||
}
|
||||
})();
|
||||
|
||||
|
||||
console.log('Database backup service started. Running backups every 24 hours.');
|
||||
|
||||
// Startup health notification
|
||||
@@ -351,3 +352,17 @@ console.log('Database backup service started. Running backups every 24 hours.');
|
||||
console.warn('Startup broadcast failed:', e.message);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
// Export functions for reuse in other scripts
|
||||
module.exports = {
|
||||
downloadBackupFile,
|
||||
compressBackupFile,
|
||||
formatBytes,
|
||||
sendTelegramBroadcast,
|
||||
getDbSizeBytes,
|
||||
// Export configurations that might be needed
|
||||
smbConfig,
|
||||
config: config, // MSSQL config
|
||||
s3 // S3 client
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user