MySQL 在 Typescript 中转储失败 | discord.js
MySQL Dump fails in Typescript | discord.js
- 问题
我的 Discord 机器人有一个 MySQL 数据库,我想每 3 小时备份一次。我在下面给出了我的代码,它似乎发送了一个没有数据的文件。
- 信息
当我在 Visual Studio 代码中查看文件时,我可以看到表格和更多详细信息,但是发送的文件只有以下内容:
/*!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 */;
- 代码
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);
}
// ...
- 更多
process.env
细节没问题,因为数据库正在同步。我也使用了 createReadStream()
似乎也没有修复错误。最后,我使用 unlink()
在文件发送 5 秒后将其删除。
尝试使用 async
/await
。
您也不应该使用 Buffer.from(filename)
,因为它只读取文件名作为字符串,而不是文件的实际内容。
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'
);
- 问题
我的 Discord 机器人有一个 MySQL 数据库,我想每 3 小时备份一次。我在下面给出了我的代码,它似乎发送了一个没有数据的文件。
- 信息
当我在 Visual Studio 代码中查看文件时,我可以看到表格和更多详细信息,但是发送的文件只有以下内容:
/*!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 */;
- 代码
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);
}
// ...
- 更多
process.env
细节没问题,因为数据库正在同步。我也使用了 createReadStream()
似乎也没有修复错误。最后,我使用 unlink()
在文件发送 5 秒后将其删除。
尝试使用 async
/await
。
您也不应该使用 Buffer.from(filename)
,因为它只读取文件名作为字符串,而不是文件的实际内容。
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'
);