Processes and output | db-dumper-go | Spatie       

 SPATIE  

db-dumper-go
============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Db-dumper-go](https://spatie.be/docs/db-dumper-go/v1)  Processes and output

 Version   v1      

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

  Processes and output    
- [ Introduction ](https://spatie.be/docs/db-dumper-go/v1/introduction)
- [ Requirements ](https://spatie.be/docs/db-dumper-go/v1/requirements)
- [ Installation and setup ](https://spatie.be/docs/db-dumper-go/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/db-dumper-go/v1/usage)
- [ Processes and output ](https://spatie.be/docs/db-dumper-go/v1/processes-and-output)
- [ Testing ](https://spatie.be/docs/db-dumper-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/db-dumper-go/v1/releasing)
- [ Contributing ](https://spatie.be/docs/db-dumper-go/v1/contributing)
- [ Security vulnerabilities ](https://spatie.be/docs/db-dumper-go/v1/security-vulnerabilities)
- [ Credits ](https://spatie.be/docs/db-dumper-go/v1/credits)
- [ License ](https://spatie.be/docs/db-dumper-go/v1/license)
- [ Changelog ](https://spatie.be/docs/db-dumper-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/db-dumper-go/v1/support-us)

 Processes and output
====================

###  On this page 

1. [ Inspecting a command ](#content-inspecting-a-command)
2. [ Replacing process execution ](#content-replacing-process-execution)
3. [ Credentials ](#content-credentials)
4. [ Output and cancellation ](#content-output-and-cancellation)

Inspecting a command
--------------------------------------------------------------------------------------------------------------------

`Prepare(ctx)` replaces PHP's shell-string `getDumpCommand` and Symfony process object:

```
command, err := dumper.Prepare(ctx)
if err != nil {
    return err
}
defer command.Close()

// Inspect command.Program, Args, AdditionalArgs, Environment and Stdin.
return command.DumpToFile(ctx, "dump.sql")
```

Preparation owns temporary credential files until `Close`. Keep the command open while using it. Do not copy an owning command or execute it after closing. Do not mutate its fields during execution. Extra arguments and inherited environment variables may contain secrets, so do not log them. The default string formatting of `Dumper` and `Command` omits their configuration.

Replacing process execution
-----------------------------------------------------------------------------------------------------------------------------------------

Configuration fields are exported. Custom compressors implement `Compressor`. Set `Run` to customize native process execution, for example to select an executable in a container:

```
dumper.Run = func(cmd *exec.Cmd) error {
    // Adjust cmd.Path, cmd.Args or cmd.Env if needed.
    return cmd.Run()
}
```

Every native execution uses this function, including SQLite table discovery, subsequent MySQL passes and compression. The supplied `exec.Cmd` has its context, input, output and credentials configured. A replacement must honor cancellation, wait for completion and return failures. Callers that replace execution own the security and diagnostics of that implementation.

Applications can use an interface with `DumpToFile(context.Context, string) error` to replace the whole dumper without depending on a package-specific base class.

Credentials
-----------------------------------------------------------------------------------------

MySQL and MariaDB use an escaped private option file. PostgreSQL uses an escaped private pgpass file and `PGDATABASE`, with interactive prompting disabled. Inherited `PGPASSWORD`, `PGSERVICE` and `PGSERVICEFILE` are removed. MongoDB uses a private password config file instead of a command-line password.

Files use mode 0600 on Unix. On Windows, secure the account's temporary directory with appropriate ACLs. Credentials remain ordinary Go strings in memory. These protections do not prevent access by an administrator or another process running as the same account.

Native stderr is discarded because a tool can echo credentials. Errors retain the program and wrapped process failure, not arguments or captured diagnostics. Avoid secrets in executable paths and do not add secrets to custom errors. `errors.As` can recover `*dbdumper.ProcessError` and `*exec.ExitError`. `errors.Is` recognizes `ErrInvalidConfig`, `ErrInvalidURL`, `ErrEmptyDump`, `context.Canceled` and `context.DeadlineExceeded`.

Output and cancellation
-----------------------------------------------------------------------------------------------------------------------------

Dumps are staged in private files beside the destination. Empty raw output is rejected before compression, and empty compressed output is rejected too. Dump, filter and compressor failures leave an existing destination untouched. Overwrites use `os.Rename`, subject to the operating system's replacement guarantees. This needs free space for the raw dump and additional filtered or compressed files. It replaces the old file's permissions rather than preserving them. There is no fsync durability guarantee.

Output symlinks and non-regular files are rejected. SQLite opens its source read-only, starts a transaction before `.dump` for a consistent WAL snapshot and rejects source/output aliases including hard links. Excluding every SQLite table produces an empty transaction, not a full dump. Exclusion discovery happens before the snapshot, so avoid concurrent DDL changes.

`DumpToFile` uses a single context deadline for preparation, native dump passes and compression. A prepared command gets a fresh configured timeout when executed. Direct child processes are killed and reaped on cancellation. Independently spawned descendants are not managed. File-system calls and final append copying cannot be interrupted midway. Use trusted source and destination directories. Path checks are not a sandbox against hostile concurrent replacement.

 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)
