🦈드림 걸 Gemoy ~ Info AraBotz
🦈드림 걸 Gemoy ~ Info AraBotz
May 28, 2025 at 07:57 AM
// backup script ke telegram const { createWriteStream, readFileSync, unlink, existsSync, mkdirSync } = require('fs') const moment = require('moment-timezone') const path = require('path') const archiver = require('archiver') const ignore = require('ignore') const glob = require('glob') const axios = require('axios') const FormData = require('form-data') const TELEGRAM_TOKEN = "isi pake token bot lu" // dapatkan disini @BotFather const TELEGRAM_CHAT_ID = "isi id chat tele lu" // dapatkan disini @cekTeleID_bot let handler = async (m, { conn }) => { const zipName = `Backup Script - ${moment().format('DD-MM-YY_HH-mm-ss')}.zip` const zipDir = path.join(process.cwd(), 'tmp') const zipPath = path.join(zipDir, zipName) try { m.reply("Mengirim file backup ke telegram") if (!existsSync(zipDir)) mkdirSync(zipDir) const ig = ignore().add([ 'node_modules', 'tmp/**', '.config/**', '.npm/**', '.cache/**', 'package-lock.json' ]) const output = createWriteStream(zipPath) const archive = archiver('zip', { zlib: { level: 9 } }) output.on('close', async () => { try { const buffer = readFileSync(zipPath) const form = new FormData() form.append('chat_id', TELEGRAM_CHAT_ID) form.append('caption', `📦 Backup file: ${zipName}\n📂 Size: ${Math.round(archive.pointer() / 1024)} KB`) form.append('document', buffer, zipName) await axios.post(`https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendDocument`, form, { headers: form.getHeaders() }) } catch (err) { console.error('Gagal mengirim ke Telegram:', err) } finally { unlink(zipPath, (err) => { if (err) console.error('Gagal menghapus file backup:', err) }) } }) archive.on('error', (e) => { console.log(e) }) archive.pipe(output) const rootDir = path.join(__dirname, '../../') const allFiles = glob.sync('**/*', { cwd: rootDir, dot: true }) const filter = ig.createFilter() for (const file of allFiles) { if (filter(file)) { const filePath = path.join(rootDir, file) archive.file(filePath, { name: file }) } } archive.finalize() } catch (e) { console.log(e) } } handler.command = ["backup"] handler.owner = true module.exports = handler

Comments