-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump_database.js
59 lines (47 loc) · 1.7 KB
/
dump_database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const mysql = require('mysql');
const fs = require('fs');
// MySQL database configuration
const dbConfig = {
host: 'localhost',
user: 'your_username',
password: 'your_password',
database: 'your_database'
};
// Discord bot configuration
const channelId = 'YOUR_DISCORD_CHANNEL_ID'; // Replace with your Discord channel ID
// Function to connect to MySQL database and dump data
const dumpDatabase = () => {
const connection = mysql.createConnection(dbConfig);
connection.connect(err => {
if (err) {
console.error('Error connecting to MySQL database:', err);
return;
}
console.log('Connected to MySQL database.');
// Execute SQL query to retrieve data
connection.query('SELECT * FROM your_table', (err, results) => {
if (err) {
console.error('Error executing SQL query:', err);
connection.end();
return;
}
// Save dumped data to a file
fs.writeFileSync('database_dump.txt', JSON.stringify(results));
// Send dumped data to Discord channel
const channel = client.channels.cache.get(channelId);
if (channel) {
channel.send({ files: ['database_dump.txt'] })
.then(() => console.log('Database dump sent to Discord channel.'))
.catch(console.error);
} else {
console.error('Channel not found.');
}
connection.end();
});
});
};
// Schedule database dump to run every 12 hours
// Adjust the cron expression as needed
require('node-cron').schedule('0 */12 * * *', () => {
dumpDatabase();
});