x
This commit is contained in:
97
index.js
97
index.js
@@ -7,6 +7,7 @@ const path = require('path');
|
||||
const zlib = require('zlib');
|
||||
const { pipeline } = require('stream/promises');
|
||||
const SambaClient = require('samba-client');
|
||||
const { Client } = require('ssh2');
|
||||
|
||||
// AWS S3 Configuration (v3 client)
|
||||
const s3 = new S3Client({
|
||||
@@ -36,7 +37,15 @@ const config = {
|
||||
// Backup file path
|
||||
const backupFilePath = process.env.BACKUP_FILE_PATH;
|
||||
|
||||
// SMB Configuration
|
||||
// SCP Configuration (SSH key authentication)
|
||||
const scpConfig = {
|
||||
host: process.env.SCP_HOST,
|
||||
port: parseInt(process.env.SCP_PORT) || 22,
|
||||
username: process.env.SCP_USERNAME,
|
||||
privateKey: fs.readFileSync(require('path').resolve(process.env.SCP_KEY_PATH))
|
||||
};
|
||||
|
||||
// Legacy SMB Configuration (deprecated)
|
||||
const smbConfig = {
|
||||
address: process.env.SMB_ADDRESS,
|
||||
username: process.env.SMB_USERNAME,
|
||||
@@ -45,8 +54,8 @@ const smbConfig = {
|
||||
};
|
||||
|
||||
// Ensure download directory exists
|
||||
const downloadFile = process.env.SMB_DOWNLOAD_FILE;
|
||||
const localDownloadFile = process.env.SMB_LOCAL_DOWNLOAD_FILE;
|
||||
const downloadFile = process.env.SCP_REMOTE_PATH || process.env.SMB_DOWNLOAD_FILE;
|
||||
const localDownloadFile = process.env.SCP_LOCAL_PATH || process.env.SMB_LOCAL_DOWNLOAD_FILE;
|
||||
|
||||
|
||||
// Admin Telegram Broadcast (env-configured)
|
||||
@@ -153,19 +162,83 @@ async function createDatabaseBackup() {
|
||||
}
|
||||
}
|
||||
|
||||
// Function to download backup file from SMB share
|
||||
// Function to download backup file via SCP
|
||||
async function downloadBackupFile() {
|
||||
try {
|
||||
console.log('Downloading backup file via SCP...');
|
||||
|
||||
const remotePath = process.env.SCP_REMOTE_PATH || '/sharemnt/ez.bak';
|
||||
const localPath = process.env.SCP_LOCAL_PATH || 'ez.bak';
|
||||
|
||||
console.log(`From: ${scpConfig.username}@${scpConfig.host}:${remotePath}`);
|
||||
console.log(`To: ${localPath}`);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const conn = new Client();
|
||||
|
||||
conn.on('ready', () => {
|
||||
console.log('SSH connection established');
|
||||
|
||||
conn.sftp((err, sftp) => {
|
||||
if (err) {
|
||||
console.error('SFTP error:', err);
|
||||
conn.end();
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
const readStream = sftp.createReadStream(remotePath);
|
||||
const writeStream = fs.createWriteStream(localPath);
|
||||
|
||||
readStream.on('error', (err) => {
|
||||
console.error('Read stream error:', err);
|
||||
conn.end();
|
||||
reject(err);
|
||||
});
|
||||
|
||||
writeStream.on('error', (err) => {
|
||||
console.error('Write stream error:', err);
|
||||
conn.end();
|
||||
reject(err);
|
||||
});
|
||||
|
||||
writeStream.on('finish', () => {
|
||||
console.log('Backup file downloaded successfully to:', localPath);
|
||||
conn.end();
|
||||
resolve(localPath);
|
||||
});
|
||||
|
||||
readStream.pipe(writeStream);
|
||||
});
|
||||
});
|
||||
|
||||
conn.on('error', (err) => {
|
||||
console.error('SSH connection error:', err);
|
||||
reject(err);
|
||||
});
|
||||
|
||||
conn.connect(scpConfig);
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
console.error('Error downloading backup file via SCP:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// Legacy function to download backup file from SMB share (deprecated)
|
||||
async function downloadBackupFileSMB() {
|
||||
try {
|
||||
console.log('Downloading backup file from SMB share...');
|
||||
|
||||
|
||||
// Create SMB client
|
||||
const client = new SambaClient(smbConfig);
|
||||
|
||||
|
||||
// Download file from SMB share
|
||||
await client.getFile(downloadFile, localDownloadFile);
|
||||
|
||||
console.log('Backup file downloaded successfully to:', localDownloadFile);
|
||||
return localDownloadFile;
|
||||
await client.getFile(process.env.SMB_DOWNLOAD_FILE, process.env.SMB_LOCAL_DOWNLOAD_FILE);
|
||||
|
||||
console.log('Backup file downloaded successfully to:', process.env.SMB_LOCAL_DOWNLOAD_FILE);
|
||||
return process.env.SMB_LOCAL_DOWNLOAD_FILE;
|
||||
} catch (err) {
|
||||
console.error('Error downloading backup file from SMB share:', err);
|
||||
throw err;
|
||||
@@ -358,12 +431,14 @@ if (require.main === module) {
|
||||
module.exports = {
|
||||
createDatabaseBackup,
|
||||
downloadBackupFile,
|
||||
downloadBackupFileSMB, // Legacy SMB function
|
||||
compressBackupFile,
|
||||
formatBytes,
|
||||
sendTelegramBroadcast,
|
||||
getDbSizeBytes,
|
||||
// Export configurations that might be needed
|
||||
smbConfig,
|
||||
scpConfig,
|
||||
smbConfig, // Legacy SMB config
|
||||
config: config, // MSSQL config
|
||||
s3 // S3 client
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user