Usage | db-dumper-rust | Spatie       

 SPATIE  

db-dumper-rust
==============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Db-dumper-rust](https://spatie.be/docs/db-dumper-rust/v1)  Usage

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/db-dumper-rust/v1) 

  Usage    
- [ Introduction ](https://spatie.be/docs/db-dumper-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/db-dumper-rust/v1/support-us)
- [ Requirements ](https://spatie.be/docs/db-dumper-rust/v1/requirements)
- [ Installation ](https://spatie.be/docs/db-dumper-rust/v1/installation)
- [ Usage ](https://spatie.be/docs/db-dumper-rust/v1/usage)
- [ Testing ](https://spatie.be/docs/db-dumper-rust/v1/testing)
- [ Changelog ](https://spatie.be/docs/db-dumper-rust/v1/changelog)
- [ Contributing ](https://spatie.be/docs/db-dumper-rust/v1/contributing)
- [ Security Vulnerabilities ](https://spatie.be/docs/db-dumper-rust/v1/security-vulnerabilities)
- [ Credits ](https://spatie.be/docs/db-dumper-rust/v1/credits)
- [ License ](https://spatie.be/docs/db-dumper-rust/v1/license)
- [ PHP parity ](https://spatie.be/docs/db-dumper-rust/v1/parity)

 Usage
=====

Builder methods consume and return the dumper. Dumping returns `Result`. Extra options are literal arguments, not shell fragments. See [PARITY](parity) for the complete PHP API mapping and safety differences.

This is the simplest way to create a dump of a MySql db:

```
use spatie_db_dumper::{MySql};

MySql::create()
    .set_db_name(database_name)
    .set_user_name(user_name)
    .set_password(password)
    .dump_to_file("dump.sql")?;
```

If you're working with PostgreSQL just use that dumper, most methods are available on both the MySql. and PostgreSql-dumper.

```
use spatie_db_dumper::{PostgreSql};

PostgreSql::create()
    .set_db_name(database_name)
    .set_user_name(user_name)
    .set_password(password)
    .dump_to_file("dump.sql")?;
```

If the `mysqldump` (or `pg_dump`) binary is installed in a non default location you can let the package know by using the`set_dump_binary_path()`-function:

```
use spatie_db_dumper::{MySql};

MySql::create()
    .set_dump_binary_path("/custom/location")
    .set_db_name(database_name)
    .set_user_name(user_name)
    .set_password(password)
    .dump_to_file("dump.sql")?;
```

If your application is deployed and you need to change the host (default is 127.0.0.1), you can add the `set_host()`-function:

```
use spatie_db_dumper::{MySql};

MySql::create()
    .set_db_name(database_name)
    .set_user_name(user_name)
    .set_password(password)
    .set_host(host)
    .dump_to_file("dump.sql")?;
```

### Handling AUTO\_INCREMENT Values in Dumps

When creating a database dump, you might need to control the inclusion of AUTO\_INCREMENT values. This can be crucial for avoiding primary key conflicts or for maintaining ID consistency when transferring data across environments.

#### Skipping AUTO\_INCREMENT Values

To omit the AUTO\_INCREMENT values from the tables in your dump, use the skip\_auto\_increment method. This is particularly useful to prevent conflicts when importing the dump into another database where those specific AUTO\_INCREMENT values might already exist, or when the exact values are not relevant.

```
use spatie_db_dumper::{MySql};

MySql::create()
    .set_db_name("dbname")
    .set_user_name("username")
    .set_password("password")
    .skip_auto_increment()
    .dump_to_file("dump.sql")?;
```

### Including AUTO\_INCREMENT values in the dump

By default, the AUTO\_INCREMENT values are included in the dump. However, if you previously used the `skip_auto_increment` method and wish to ensure that the AUTO\_INCREMENT values are included in a subsequent dump, use the `do_not_skip_auto_increment` method to explicitly include them.

```
use spatie_db_dumper::{MySql};

MySql::create()
    .set_db_name("dbname")
    .set_user_name("username")
    .set_password("password")
    .do_not_skip_auto_increment()
    .dump_to_file("dump.sql")?;
```

### Use a Database URL

In some applications or environments, database credentials are provided as URLs instead of individual components. In this case, you can use the `set_database_url` method instead of the individual methods.

```
use spatie_db_dumper::{MySql};

MySql::create()
    .set_database_url(database_url)?
    .dump_to_file("dump.sql")?;
```

When providing a URL, the package will automatically parse it and provide the individual components to the applicable dumper.

For example, if you provide the URL `mysql://username:password@hostname:3306/dbname`, the dumper will use the `hostname` host, running on port `3306`, and will connect to `dbname` with `username` and `password`.

### Dump specific tables

Using an array:

```
use spatie_db_dumper::{MySql};

MySql::create()
    .set_db_name(database_name)
    .set_user_name(user_name)
    .set_password(password)
    .include_tables(["table1", "table2", "table3"])
    .dump_to_file("dump.sql")?;
```

Using a string:

```
use spatie_db_dumper::{MySql};

MySql::create()
    .set_db_name(database_name)
    .set_user_name(user_name)
    .set_password(password)
    .include_tables("table1, table2, table3")
    .dump_to_file("dump.sql")?;
```

### Don't use column\_statics table with some old version of MySql service.

In order to use "*--column-statistics=0*" as option in mysqldump command you can use *do\_not\_use\_column\_statistics()* method.

If you have installed *mysqldump 8*, it queries by default *column\_statics* table in *information\_schema* database. In some old version of MySql (service) like 5.7, this table doesn't exist. So you could have an exception during the execution of mysqldump. To avoid this, you could use *do\_not\_use\_column\_statistics()* method.

```
use spatie_db_dumper::{MySql};

MySql::create()
    .set_db_name(database_name)
    .set_user_name(user_name)
    .set_password(password)
    .do_not_use_column_statistics()
    .dump_to_file("dump.sql")?;
```

### Excluding tables from the dump

You can exclude tables from the dump by using an array:

```
use spatie_db_dumper::{MySql};

MySql::create()
    .set_db_name(database_name)
    .set_user_name(user_name)
    .set_password(password)
    .exclude_tables(["table1", "table2", "table3"])
    .dump_to_file("dump.sql")?;
```

Or by using a string:

```
use spatie_db_dumper::{MySql};

MySql::create()
    .set_db_name(database_name)
    .set_user_name(user_name)
    .set_password(password)
    .exclude_tables("table1, table2, table3")
    .dump_to_file("dump.sql")?;
```

### Excluding table data from the dump

If you want to dump the structure of certain tables but not their data (e.g. log tables), you can use `exclude_tables_data`. This works with both MySQL and MariaDB.

Using an array:

```
use spatie_db_dumper::{MySql};

MySql::create()
    .set_db_name(database_name)
    .set_user_name(user_name)
    .set_password(password)
    .exclude_tables_data(["logs", "sessions"])
    .dump_to_file("dump.sql")?;
```

You can combine this with `exclude_tables` to fully exclude some tables and only skip data for others:

```
use spatie_db_dumper::{MySql};

MySql::create()
    .set_db_name(database_name)
    .set_user_name(user_name)
    .set_password(password)
    .exclude_tables(["cache"])
    .exclude_tables_data(["logs", "sessions"])
    .dump_to_file("dump.sql")?;
```

### Including routines in the dump

To include stored procedures and functions in your MySQL or MariaDB dump, use the `include_routines` method:

```
use spatie_db_dumper::{MySql};

MySql::create()
    .set_db_name(database_name)
    .set_user_name(user_name)
    .set_password(password)
    .include_routines()
    .dump_to_file("dump.sql")?;
```

### Do not write CREATE TABLE statements that create each dumped table

You can use `do_not_create_tables` to prevent writing create statements.

```
use spatie_db_dumper::{MySql};

let dump_command = MySql::create()
    .set_db_name("dbname")
    .set_user_name("username")
    .set_password("password")
    .do_not_create_tables()
    .get_dump_command()?;
```

### Do not write row data

You can use `do_not_dump_data` to prevent writing row data.

```
use spatie_db_dumper::{MySql};

let dump_command = MySql::create()
    .set_db_name("dbname")
    .set_user_name("username")
    .set_password("password")
    .do_not_dump_data()
    .get_dump_command()?;
```

### Append instead of overwriting a dump file

You can use `use_append_mode` with MySQL to append to the file instead of overwriting it. This is useful for two-step dumps when you want to dump the whole schema but only some of the data (for example: only migrations, or only product but not customer data).

```
use spatie_db_dumper::{MySql};

let dump_command = MySql::create()
    .set_db_name("dbname")
    .set_user_name("username")
    .set_password("password")
    .use_append_mode()
    .get_dump_command()?;
```

### Adding extra options

If you want to add an arbitrary option to the dump command you can use `add_extra_option`

```
use spatie_db_dumper::{MySql};

let dump_command = MySql::create()
    .set_db_name("dbname")
    .set_user_name("username")
    .set_password("password")
    .add_extra_option("--xml")
    .get_dump_command()?;
```

If you're working with MySql you can set the database name using `--databases` as an extra option. This is particularly useful when used in conjunction with the `--add-drop-database` `mysqldump` option (see the [mysqldump docs](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_add-drop-database)).

```
use spatie_db_dumper::{MySql};

let dump_command = MySql::create()
    .set_user_name("username")
    .set_password("password")
    .add_extra_option("--databases")
    .add_extra_option("dbname")
    .add_extra_option("--add-drop-database")
    .get_dump_command()?;
```

With MySql, you also have the option to use the `--all-databases` extra option. This is useful when you want to run a full backup of all the databases in the specified MySQL connection.

```
use spatie_db_dumper::{MySql};

let dump_command = MySql::create()
    .set_user_name("username")
    .set_password("password")
    .add_extra_option("--all-databases")
    .get_dump_command()?;
```

Please note that using the `.add_extra_option("--databases").add_extra_option("dbname")` or `.add_extra_option('--all-databases')` will override the database name set on a previous `.set_db_name()` call.

### Using compression

If you want the output file to be compressed, you can use a compressor.

There are two compressors that come out of the box:

- `GzipCompressor` - This will compress your db dump with `gzip`. Make sure `gzip` is installed on your system before using this.
- `Bzip2Compressor` - This will compress your db dump with `bzip2`. Make sure `bzip2` is installed on your system before using this.

```
use spatie_db_dumper::{MySql, GzipCompressor};

let dump_command = MySql::create()
    .set_db_name("dbname")
    .set_user_name("username")
    .set_password("password")
    .use_compressor(GzipCompressor) // or `Bzip2Compressor`
    .dump_to_file("dump.sql.gz")?;
```

### Creating your own compressor

You can create your own compressor implementing the `Compressor` trait. Here's how that trait looks like:

```
use std::{ffi::OsString, path::PathBuf};

pub trait Compressor: Send + Sync {
    fn program(&self) -> PathBuf;
    fn args(&self) -> Vec { Vec::new() }
    fn use_extension(&self) -> &str;
}
```

The `program` should simply return the compression executable the db dump will get pumped to. Each entry returned by `args` is one literal argument. Here's the implementation of `GzipCompressor`.

```
use spatie_db_dumper::Compressor;
use std::path::PathBuf;

pub struct GzipCompressor;

impl Compressor for GzipCompressor {
    fn program(&self) -> PathBuf {
        "gzip".into()
    }

    fn use_extension(&self) -> &str {
        "gz"
    }
}
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
