feat: export createDatabaseBackup function for reuse in other scripts
This commit is contained in:
93
backup.js
Normal file
93
backup.js
Normal file
@@ -0,0 +1,93 @@
|
||||
require('dotenv').config();
|
||||
const {
|
||||
createDatabaseBackup,
|
||||
downloadBackupFile,
|
||||
sendTelegramBroadcast,
|
||||
formatBytes,
|
||||
config: sqlConfig
|
||||
} = require('./index.js');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
async function createAndDownloadBackup() {
|
||||
try {
|
||||
console.log('='.repeat(50));
|
||||
console.log('FRESH BACKUP CREATION');
|
||||
console.log('='.repeat(50));
|
||||
console.log(`Database: ${process.env.MSSQL_DATABASE}`);
|
||||
console.log(`Server: ${process.env.MSSQL_SERVER}:${process.env.MSSQL_PORT}`);
|
||||
console.log('');
|
||||
|
||||
// Step 1: Create fresh backup on SQL Server
|
||||
console.log('Step 1: Creating fresh database backup on SQL Server...');
|
||||
const serverBackupPath = await createDatabaseBackup();
|
||||
console.log(`Backup created on server: ${serverBackupPath}`);
|
||||
console.log('');
|
||||
|
||||
// Step 2: Download the backup file to local folder
|
||||
console.log('Step 2: Downloading backup file to local folder...');
|
||||
const localBackupPath = await downloadBackupFile();
|
||||
console.log(`Backup downloaded to: ${localBackupPath}`);
|
||||
console.log('');
|
||||
|
||||
// Step 3: Check local file and show details
|
||||
if (fs.existsSync(localBackupPath)) {
|
||||
const stats = fs.statSync(localBackupPath);
|
||||
const absolutePath = path.resolve(localBackupPath);
|
||||
|
||||
console.log('='.repeat(50));
|
||||
console.log('BACKUP COMPLETED SUCCESSFULLY');
|
||||
console.log('='.repeat(50));
|
||||
console.log(`Local file: ${absolutePath}`);
|
||||
console.log(`File size: ${formatBytes(stats.size)}`);
|
||||
console.log(`Created: ${stats.ctime.toISOString()}`);
|
||||
console.log('');
|
||||
|
||||
// Send success notification
|
||||
const when = new Date().toISOString();
|
||||
const msg = `Fresh backup created and downloaded ✅\nDB: ${process.env.MSSQL_DATABASE}\nFile: ${localBackupPath}\nSize: ${formatBytes(stats.size)}\nServer: ${process.env.MSSQL_SERVER}:${process.env.MSSQL_PORT}\nTime: ${when}`;
|
||||
await sendTelegramBroadcast('all', msg);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
localPath: absolutePath,
|
||||
serverPath: serverBackupPath,
|
||||
size: stats.size,
|
||||
database: process.env.MSSQL_DATABASE
|
||||
};
|
||||
} else {
|
||||
throw new Error(`Local backup file not found: ${localBackupPath}`);
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.error('');
|
||||
console.error('='.repeat(50));
|
||||
console.error('BACKUP FAILED');
|
||||
console.error('='.repeat(50));
|
||||
console.error('Error:', err.message);
|
||||
console.error('');
|
||||
|
||||
// Send error notification
|
||||
const when = new Date().toISOString();
|
||||
const msg = `Fresh backup failed 🔴\nDB: ${process.env.MSSQL_DATABASE}\nServer: ${process.env.MSSQL_SERVER}:${process.env.MSSQL_PORT}\nTime: ${when}\nError: ${err.message}`;
|
||||
await sendTelegramBroadcast('errors', msg);
|
||||
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// Run if called directly
|
||||
if (require.main === module) {
|
||||
createAndDownloadBackup()
|
||||
.then((result) => {
|
||||
console.log('Fresh backup process completed successfully!');
|
||||
console.log(`Ready to use: ${result.localPath}`);
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Fresh backup process failed:', error.message);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { createAndDownloadBackup };
|
||||
Reference in New Issue
Block a user