-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from Nsttt/add-mysql
Add Mysql support
- Loading branch information
Showing
10 changed files
with
151 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,6 @@ | ||
S3_ENDPOINT= | ||
S3_BUCKET=postgres-backups | ||
S3_ACCESS_KEY= | ||
S3_SECRET_KEY= | ||
|
||
POSTGRES_HOST=postgres | ||
POSTGRES_PORT=5432 | ||
POSTGRES_USER=postgres | ||
POSTGRES_PASSWORD=postgres | ||
POSTGRES_DB=postgres | ||
|
||
EVERY=24h | ||
URLS="postgres://user:password@host:port/dbname,mysql://user:password@host:port/dbname" | ||
S3_ENDPOINT="your_s3_endpoint" | ||
S3_BUCKET="your_s3_bucket" | ||
S3_ACCESS_KEY="your_s3_access_key" | ||
S3_SECRET_KEY="your_s3_secret_key" | ||
INTERVAL="24h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"os/exec" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type connectionOptions struct { | ||
Host string | ||
DbType string | ||
Port int | ||
Database string | ||
Username string | ||
Password string | ||
} | ||
|
||
var ( | ||
PGDumpCmd = "pg_dump" | ||
pgDumpStdOpts = []string{"--no-owner", "--no-acl", "--clean", "--blobs", "-v"} | ||
pgDumpDefaultFormat = "c" | ||
ErrPgDumpNotFound = errors.New("pg_dump not found") | ||
|
||
MysqlDumpCmd = "mysqldump" | ||
mysqlDumpStdOpts = []string{"--compact", "--skip-add-drop-table", "--skip-add-locks", "--skip-disable-keys", "--skip-set-charset", "-v"} | ||
ErrMySqlDumpNotFound = errors.New("mysqldump not found") | ||
|
||
ErrUnsupportedType = errors.New("unsupported database type") | ||
) | ||
|
||
func RunDump(connectionOpts *connectionOptions, outFile string) error { | ||
cmd, err := buildDumpCommand(connectionOpts, outFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return executeCommand(cmd) | ||
} | ||
|
||
func buildDumpCommand(opts *connectionOptions, outFile string) (*exec.Cmd, error) { | ||
switch opts.DbType { | ||
case "postgres": | ||
if !commandExist(PGDumpCmd) { | ||
return nil, ErrPgDumpNotFound | ||
} | ||
options := append( | ||
pgDumpStdOpts, | ||
fmt.Sprintf("-f%s", outFile), | ||
fmt.Sprintf("--dbname=%s", opts.Database), | ||
fmt.Sprintf("--host=%s", opts.Host), | ||
fmt.Sprintf("--port=%d", opts.Port), | ||
fmt.Sprintf("--username=%s", opts.Username), | ||
fmt.Sprintf("--format=%s", pgDumpDefaultFormat), | ||
) | ||
return exec.Command(PGDumpCmd, options...), nil | ||
|
||
case "mysql": | ||
mysqldumpCmd := "mysqldump" | ||
if !commandExist(mysqldumpCmd) { | ||
return nil, ErrMySqlDumpNotFound | ||
} | ||
options := append( | ||
mysqlDumpStdOpts, | ||
"-h", opts.Host, | ||
"-P", strconv.Itoa(opts.Port), | ||
"-u", opts.Username, | ||
fmt.Sprintf("--password=%s", opts.Password), | ||
"--databases", opts.Database, | ||
"-r", outFile, | ||
) | ||
|
||
return exec.Command(mysqldumpCmd, options...), nil | ||
|
||
default: | ||
return nil, ErrUnsupportedType | ||
} | ||
} | ||
|
||
func executeCommand(cmd *exec.Cmd) error { | ||
stderr, err := cmd.StderrPipe() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := cmd.Start(); err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
scanner := bufio.NewScanner(stderr) | ||
for scanner.Scan() { | ||
fmt.Println(scanner.Text()) | ||
} | ||
}() | ||
|
||
if err := cmd.Wait(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func commandExist(command string) bool { | ||
_, err := exec.LookPath(command) | ||
return err == nil | ||
} | ||
|
||
func newFileName(db string, dbType string) string { | ||
switch dbType { | ||
case "postgres": | ||
return fmt.Sprintf(`%v_%v.pgdump`, db, time.Now().Unix()) | ||
case "mysql": | ||
return fmt.Sprintf(`%v_%v.sql`, db, time.Now().Unix()) | ||
} | ||
return fmt.Sprintf(`%v_%v`, db, time.Now().Unix()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.