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 };
|
||||||
81
index.js
81
index.js
@@ -310,44 +310,59 @@ async function runBackupProcess() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run backup immediately when starting
|
// Only run startup logic if this file is executed directly (not imported)
|
||||||
(async () => {
|
if (require.main === module) {
|
||||||
try {
|
// Run backup immediately when starting
|
||||||
const latestBackupTime = await getLatestBackupTime();
|
(async () => {
|
||||||
const now = new Date();
|
try {
|
||||||
let delay = 0;
|
const latestBackupTime = await getLatestBackupTime();
|
||||||
|
const now = new Date();
|
||||||
|
let delay = 0;
|
||||||
|
|
||||||
if (latestBackupTime) {
|
if (latestBackupTime) {
|
||||||
const nextBackupTime = new Date(latestBackupTime.getTime() + 86400000);
|
const nextBackupTime = new Date(latestBackupTime.getTime() + 86400000);
|
||||||
if (nextBackupTime > now) {
|
if (nextBackupTime > now) {
|
||||||
delay = nextBackupTime - now;
|
delay = nextBackupTime - now;
|
||||||
console.log(`Scheduling first backup in ${Math.floor(delay / 3600000)} hours.`);
|
console.log(`Scheduling first backup in ${Math.floor(delay / 3600000)} hours.`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (delay > 0) {
|
if (delay > 0) {
|
||||||
setTimeout(runBackupProcess, delay);
|
setTimeout(runBackupProcess, delay);
|
||||||
} else {
|
} else {
|
||||||
|
runBackupProcess();
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error during startup check, proceeding with backup:', err);
|
||||||
runBackupProcess();
|
runBackupProcess();
|
||||||
}
|
}
|
||||||
} catch (err) {
|
})();
|
||||||
console.error('Error during startup check, proceeding with backup:', err);
|
|
||||||
runBackupProcess();
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
|
console.log('Database backup service started. Running backups every 24 hours.');
|
||||||
|
|
||||||
console.log('Database backup service started. Running backups every 24 hours.');
|
// Startup health notification
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
const when = new Date().toISOString();
|
||||||
|
const host = require('os').hostname();
|
||||||
|
const region = process.env.AWS_REGION || 'n/a';
|
||||||
|
const bucket = process.env.S3_BUCKET_NAME || 'n/a';
|
||||||
|
await sendTelegramBroadcast('all', `Backup service started ✅\nDB: ${process.env.MSSQL_DATABASE}\nHost: ${host}\nAWS: ${region}/${bucket}\nTime: ${when}`);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Startup broadcast failed:', e.message);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
|
||||||
// Startup health notification
|
// Export functions for reuse in other scripts
|
||||||
(async () => {
|
module.exports = {
|
||||||
try {
|
downloadBackupFile,
|
||||||
const when = new Date().toISOString();
|
compressBackupFile,
|
||||||
const host = require('os').hostname();
|
formatBytes,
|
||||||
const region = process.env.AWS_REGION || 'n/a';
|
sendTelegramBroadcast,
|
||||||
const bucket = process.env.S3_BUCKET_NAME || 'n/a';
|
getDbSizeBytes,
|
||||||
await sendTelegramBroadcast('all', `Backup service started ✅\nDB: ${process.env.MSSQL_DATABASE}\nHost: ${host}\nAWS: ${region}/${bucket}\nTime: ${when}`);
|
// Export configurations that might be needed
|
||||||
} catch (e) {
|
smbConfig,
|
||||||
console.warn('Startup broadcast failed:', e.message);
|
config: config, // MSSQL config
|
||||||
}
|
s3 // S3 client
|
||||||
})();
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user