A MYSQL database backup grunt task.
npm install grunt-mysqldumpshell
npm install grunt-mysqldump --save-dev
`
Enable the plugin inside your Gruntfile:
`js
grunt.loadNpmTasks('grunt-mysqldump');
`
To run the task, issue the following command:
`sh
grunt mysqldump
`
Configuration
In your project's Gruntfile, add a section named mysqldump to the data object passed into grunt.initConfig().
`js
db: grunt.file.readJSON('config/database.json'),
`
`js
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
`json
{
"local": {
"username": "root",
"password": "password",
"hostname": "127.0.0.1",
"port": "3306"
}
}
`
$3
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.
`js
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'
],
},
},
`
$3
#### user
The database user.
#### pass
The user's password.
#### host
The host of the database.
#### port
The port where the database is running normally 3306.
#### dest
The destination folder to write the dump to.
$3
#### compress
Set to false for no compression. Will only perform a mysqldump of the target database files.
- Type: Boolean
- Default: false
- Required: false
#### algorithm
Currently supports gzip, deflate, deflateRaw, tar, tgz and zip.
- Type: String
- Default: zip
- Required: false
#### level
Sets the zlib compression level. This is an integer in the range of 0 to 9.
- Type: Integer
- Default: 1
- 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 |
#### data_only
Suppress the CREATE TABLE statements from the output.
- Type: Boolean
- Default: false
- Required: false
#### databases
An array of databases to export.
- Type: Array
#### forget
An array of databases to ignore when using a databases wildcard.
- Type: Array
Libraries Used
+ shelljs - Portable Unix shell commands for Node.js.
+ node-archiver - A streaming interface for archive generation.
+ bytes.js - Node byte string parser.
+ mysql - A pure node.js JavaScript Client implementing the MySql protocol.
License
The MIT License (MIT). See License File for more information.
``