mirror of
https://github.com/sosedoff/pgweb.git
synced 2024-12-15 11:52:12 +03:00
38 lines
757 B
Go
38 lines
757 B
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"os/exec"
|
|
)
|
|
|
|
type Dump struct {
|
|
Table string
|
|
}
|
|
|
|
func (d *Dump) Export(url string, writer io.Writer) error {
|
|
errOutput := bytes.NewBuffer(nil)
|
|
|
|
opts := []string{
|
|
"--no-owner", // skip restoration of object ownership in plain-text format
|
|
"--clean", // clean (drop) database objects before recreating
|
|
"--compress", "6", // compression level for compressed formats
|
|
}
|
|
|
|
if d.Table != "" {
|
|
opts = append(opts, []string{"--table", d.Table}...)
|
|
}
|
|
|
|
opts = append(opts, url)
|
|
|
|
cmd := exec.Command("pg_dump", opts...)
|
|
cmd.Stdout = writer
|
|
cmd.Stderr = errOutput
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
return fmt.Errorf("error: %s. output: %s", err.Error(), errOutput.Bytes())
|
|
}
|
|
return nil
|
|
}
|