Grunt-mysqldump is a Grunt plugin for dumping and archiving MySQL databases. It supports exporting and archiving multiple databases in a single operation, runs asynchronously, and is extremely fast. Outputs export and compression information as each operation completes. Tested on moderate to large size databases without any problems.
Install the plugin with this command:
npm install grunt-mysqldump --save-dev
Enable the plugin inside your Gruntfile:
grunt.loadNpmTasks('grunt-mysqldump');
To run the task, issue the following command:
grunt mysqldump
In your project's Gruntfile, add a section named mysqldump
to the data object passed into grunt.initConfig()
.
db: grunt.file.readJSON('config/database.json'),
mysqldump: {
dist: {
user: '<%= db.local.user %>',
pass: '<%= db.local.pass %>',
host: '<%= db.local.host %>',
port: '<%= db.local.port %>',
dest: 'exports/',
options: {
compress: 'gzip'
},
databases: [
'sakila',
'world',
'employees'
],
},
},
Example config/database.json
{
"local": {
"username": "root",
"password": "password",
"hostname": "127.0.0.1",
"port": "3306"
}
}
Dump all your databases by using an asterisk aka wildcard.
Note: When using the wildcard flag you have the option to ignore specific databases by creating a forget array.
mysqldump: {
dist: {
user: '<%= db.local.user %>',
pass: '<%= db.local.pass %>',
host: '<%= db.local.host %>',
port: '<%= db.local.port %>',
dest: 'backups/',
options: {
compress: true,
algorithm: 'zip',
level: 5,
data_only: true
},
databases: [
'*'
],
forget: [
'information_schema',
'performance_schema',
'phpmyadmin',
'mysql',
'sakila',
'world'
],
},
},
This section provides details about the configuration parameters that you can set for the grunt-mysqldump
task.
This parameter specifies the username for the MySQL database connection.
This parameter specifies the password associated with the username for the MySQL database connection.
This parameter specifies the hostname or IP address of the machine where the MySQL server is running.
This parameter specifies the port number on which the MySQL server is listening for connections. The default MySQL port number is 3306
.
This parameter specifies the destination directory where the database dump files will be written. Please ensure that this directory has the necessary write permissions.
Set to false for no compression. Will only perform a mysqldump of the target database files.
- Type:
Boolean
- Default:
false
- Required:
false
Currently supports gzip
, deflate
, deflateRaw
, tar
, tgz
and zip
.
- Type:
String
- Default:
zip
- Required:
false
Sets the zlib
compression level. This is an integer in the range of 0 to 9.
- Type:
Integer
- Default:
8
- Required:
false
Here's what each level means:
Level | Description |
---|---|
0 | No compression |
1 | Best speed |
2-8 | A compromise between speed and compression |
9 | Best compression |
Suppress the CREATE TABLE
statements from the output.
- Type:
Boolean
- Default:
false
- Required:
false
An array of databases to export.
- Type:
Array
An array of databases to ignore when using a databases
wildcard.
- Type:
Array
This project wouldn't be possible without these wonderful libraries:
-
shelljs: Provides portable Unix shell commands for Node.js. It's our go-to library for handling shell commands.
-
node-archiver: A powerful library that provides a streaming interface for archive generation. It's what we use to create the database dump archives.
-
bytes.js: This library helps us parse byte strings in Node.js. It's a small but crucial part of our toolkit.
-
mysql: A pure Node.js JavaScript client implementing the MySQL protocol. It's the backbone of our MySQL operations.
We're grateful to the developers and contributors of these libraries. 🙏
The MIT License (MIT). See License File for more information.