Advertisement
AnshorFalahi

backup sc

Jun 25th, 2024 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.77 KB | Source Code | 0 0
  1. const fs = require('fs');
  2. const archiver = require('archiver');
  3. const path = require('path');
  4.  
  5. exports.run = {
  6.    usage: ['backupsc'],
  7.    category: 'owner',
  8.    async: async (m, {
  9.       client,
  10.       command,
  11.       env,
  12.       Func
  13.    }) => {
  14.       try {
  15.          // Create a file to stream archive data to.
  16.          const output = fs.createWriteStream('backup.zip');
  17.          const archive = archiver('zip', {
  18.             zlib: { level: 9 } // Sets the compression level.
  19.          });
  20.  
  21.         output.on('close', async () => {
  22.             console.log(`Backup.zip has been finalized and the output file descriptor has closed. Total bytes: ${archive.pointer()}`);
  23.             try {
  24.                 const filePath = './backup.zip'; // Adjust the path as necessary
  25.                 await client.sendFile(m.chat, filePath, 'backup.zip', 'Backup file', m);
  26.                 console.log('File sent successfully.');
  27.             } catch (e) {
  28.                 console.error('Error sending file:', e);
  29.                 client.reply(m.chat, Func.jsonFormat(e), m);
  30.             }
  31.         });
  32.  
  33.  
  34.          output.on('end', () => {
  35.             console.log('Data has been drained');
  36.          });
  37.  
  38.          // Good practice to catch warnings (ie stat failures and other non-blocking errors)
  39.          archive.on('warning', (err) => {
  40.             if (err.code === 'ENOENT') {
  41.                // log warning
  42.                console.warn('Archiver warning:', err);
  43.             } else {
  44.                // throw error
  45.                throw err;
  46.             }
  47.          });
  48.  
  49.          // Catch this error explicitly
  50.          archive.on('error', (err) => {
  51.             throw err;
  52.          });
  53.  
  54.          // Pipe archive data to the file
  55.          archive.pipe(output);
  56.  
  57.          // Append files from a sub-directory and naming it `new-subdir` within the archive (see docs for more options)
  58.          const directoryPath = './';
  59.          const excludeDirs = ['node_modules', '.cache', '.config', '.npm']
  60.  
  61.          fs.readdirSync(directoryPath).forEach(file => {
  62.             const fullPath = path.join(directoryPath, file);
  63.             if (!excludeDirs.includes(file)) {
  64.                if (fs.lstatSync(fullPath).isDirectory()) {
  65.                   archive.directory(fullPath, file);
  66.                } else {
  67.                   archive.file(fullPath, { name: file });
  68.                }
  69.             }
  70.          });
  71.  
  72.          // Finalize the archive (ie we are done appending files but streams have to finish yet)
  73.          await archive.finalize();
  74.          console.log('Archive has been finalized');
  75.       } catch (e) {
  76.          console.error('Error during backup process:', e);
  77.          return client.reply(m.chat, Func.jsonFormat(e), m);
  78.       }
  79.    },
  80.    owner: true,
  81.    cache: true,
  82.    location: __filename
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement