Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const archiver = require('archiver');
- const path = require('path');
- exports.run = {
- usage: ['backupsc'],
- category: 'owner',
- async: async (m, {
- client,
- command,
- env,
- Func
- }) => {
- try {
- // Create a file to stream archive data to.
- const output = fs.createWriteStream('backup.zip');
- const archive = archiver('zip', {
- zlib: { level: 9 } // Sets the compression level.
- });
- output.on('close', async () => {
- console.log(`Backup.zip has been finalized and the output file descriptor has closed. Total bytes: ${archive.pointer()}`);
- try {
- const filePath = './backup.zip'; // Adjust the path as necessary
- await client.sendFile(m.chat, filePath, 'backup.zip', 'Backup file', m);
- console.log('File sent successfully.');
- } catch (e) {
- console.error('Error sending file:', e);
- client.reply(m.chat, Func.jsonFormat(e), m);
- }
- });
- output.on('end', () => {
- console.log('Data has been drained');
- });
- // Good practice to catch warnings (ie stat failures and other non-blocking errors)
- archive.on('warning', (err) => {
- if (err.code === 'ENOENT') {
- // log warning
- console.warn('Archiver warning:', err);
- } else {
- // throw error
- throw err;
- }
- });
- // Catch this error explicitly
- archive.on('error', (err) => {
- throw err;
- });
- // Pipe archive data to the file
- archive.pipe(output);
- // Append files from a sub-directory and naming it `new-subdir` within the archive (see docs for more options)
- const directoryPath = './';
- const excludeDirs = ['node_modules', '.cache', '.config', '.npm']
- fs.readdirSync(directoryPath).forEach(file => {
- const fullPath = path.join(directoryPath, file);
- if (!excludeDirs.includes(file)) {
- if (fs.lstatSync(fullPath).isDirectory()) {
- archive.directory(fullPath, file);
- } else {
- archive.file(fullPath, { name: file });
- }
- }
- });
- // Finalize the archive (ie we are done appending files but streams have to finish yet)
- await archive.finalize();
- console.log('Archive has been finalized');
- } catch (e) {
- console.error('Error during backup process:', e);
- return client.reply(m.chat, Func.jsonFormat(e), m);
- }
- },
- owner: true,
- cache: true,
- location: __filename
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement