Home > front end >  MySQL Dump fails in Typescript | discord.js
MySQL Dump fails in Typescript | discord.js

Time:04-15

  • Issue

I have a MySQL database for my Discord bot which I would like to backup every 3 hours. I've given my code below which seems to send a file without the data.

  • Information

When I view the file in Visual Studio Code, I can see the tables, and more information in detail, but the file which is sent has nothing but the following:

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
  • Code
import { CronJob } from 'cron';
import { WebhookClient } from 'discord.js';
import { createReadStream, unlink } from 'fs';
import mysqldump from 'mysqldump';

// ...
   const hook = new WebhookClient({ url: process.env.SQL_HOOK! });
   const key = `${new Date().getDate()}-${new Date().getMonth()}_${new Date().getHours()}-${new Date().getMinutes()}`;
   const { MYSQLUSER, MYSQLPASSWORD, MYSQLDATABASE, MYSQLHOST } = process.env;
   const job = new CronJob('00 */3 * * *', () => {
           mysqldump({
               connection: {
                   user: MYSQLUSER!,
                   password: MYSQLPASSWORD!,
                   host: MYSQLHOST!,
                   database: MYSQLDATABASE!
               },
               dumpToFile: `${process.cwd()}/backups/${key}.sql`
           });

           hook.send({
               files: [{ attachment: createReadStream(Buffer.from(`${process.cwd()}/backups/${key}.sql`)), name: `${key}.sql` }],
               username: `${client.user?.username} | MySQL Dumps`,
               avatarURL: client.user?.avatarURL() as string
           });

           setTimeout(() => {
               unlink(`${process.cwd()}/backups/${key}.sql`, () => {});
           }, 5 * 1000);
       },
       null,
       true,
       'Asia/Calcutta'
   );

   try {
       job.start();
   } catch (error) {
       client.logger.fatal(error);
   }
// ...
  • More

The process.env details are all right, since the database is getting synced well. I used createReadStream() too which also doesn't seem to fix the error. And finally, I'm using unlink() to delete the file 5 seconds after it being sent.

CodePudding user response:

Try using async/await.

You should also not use Buffer.from(filename), as it reads only the filename as a string, not the actual content of the file.

const job = new CronJob('00 */3 * * *', async () => {
        await mysqldump({
            connection: {
                user: MYSQLUSER!,
                password: MYSQLPASSWORD!,
                host: MYSQLHOST!,
                database: MYSQLDATABASE!
            },
            dumpToFile: `${process.cwd()}/backups/${key}.sql`
        });

        await hook.send({
            files: [{ attachment: createReadStream(`${process.cwd()}/backups/${key}.sql`), name: `${key}.sql` }],
            username: `${client.user?.username} | MySQL Dumps`,
            avatarURL: client.user?.avatarURL() as string
        });
         
        unlink(`${process.cwd()}/backups/${key}.sql`, () => {});
    },
    null,
    true,
    'Asia/Calcutta'
);
  • Related