Skip to content

Commit f5aab4b

Browse files
Merge pull request #26 from Nsttt/add-mysql
Add Mysql support
2 parents e50464f + ce2cf99 commit f5aab4b

File tree

10 files changed

+151
-150
lines changed

10 files changed

+151
-150
lines changed

.env.dist

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
S3_ENDPOINT=
2-
S3_BUCKET=postgres-backups
3-
S3_ACCESS_KEY=
4-
S3_SECRET_KEY=
5-
6-
POSTGRES_HOST=postgres
7-
POSTGRES_PORT=5432
8-
POSTGRES_USER=postgres
9-
POSTGRES_PASSWORD=postgres
10-
POSTGRES_DB=postgres
11-
12-
EVERY=24h
1+
URLS="postgres://user:password@host:port/dbname,mysql://user:password@host:port/dbname"
2+
S3_ENDPOINT="your_s3_endpoint"
3+
S3_BUCKET="your_s3_bucket"
4+
S3_ACCESS_KEY="your_s3_access_key"
5+
S3_SECRET_KEY="your_s3_secret_key"
6+
INTERVAL="24h"

.github/workflows/ghcr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ on:
1212

1313
env:
1414
REGISTRY: ghcr.io
15-
FQDN: "ghcr.io/thedevminertv/postgres-s3-backup"
15+
FQDN: "ghcr.io/thedevminertv/database-s3-backup"
1616

1717
jobs:
1818
build:

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77

88
env:
99
REGISTRY: ghcr.io
10-
FQDN: "ghcr.io/thedevminertv/postgres-s3-backup"
10+
FQDN: "ghcr.io/thedevminertv/database-s3-backup"
1111

1212
jobs:
1313
build:

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
1010
FROM alpine:latest
1111
WORKDIR /root/
1212

13-
RUN apk --no-cache add ca-certificates postgresql-client
13+
RUN apk --no-cache add ca-certificates postgresql-client mysql-client
1414

15-
COPY --from=builder /app/main /bin/postgres-s3-backup
15+
COPY --from=builder /app/main /bin/database-s3-backup
1616

17-
CMD ["/bin/postgres-s3-backup"]
17+
CMD ["/bin/database-s3-backup"]

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# PostgreSQL Backup to S3 with Docker
1+
# Database Backup to S3 with Docker
22

3-
This application automates the process of backing up PostgreSQL databases and uploading them to an S3-compatible storage service, utilizing Docker for easy deployment and scheduling.
3+
This application automates the process of backing up PostgreSQL and MySQL databases and uploading them to an S3-compatible storage service, utilizing Docker for easy deployment and scheduling.
44

55
## Features
66

77
- Easy deployment with Docker and Docker Compose.
8-
- Support for multiple PostgreSQL databases.
8+
- Support for multiple PostgreSQL and MySQL databases.
99
- Customizable backup intervals.
1010
- Direct upload of backups to an S3-compatible storage bucket.
1111
- Environment variable and command-line configuration for flexibility.
@@ -14,7 +14,7 @@ This application automates the process of backing up PostgreSQL databases and up
1414
## Prerequisites
1515

1616
- Docker and Docker Compose installed on your system.
17-
- Access to a PostgreSQL database.
17+
- Access to PostgreSQL and/or MySQL databases.
1818
- Access to an S3-compatible storage service.
1919

2020
## Configuration
@@ -25,7 +25,7 @@ Before running the application, you need to configure it either by setting envir
2525

2626
Create a `.env` file in the project directory with the following variables:
2727

28-
- `URLS`: Comma-separated list of PostgreSQL database URLs to backup. Format: `postgres://<user>:<password>@<host>[:<port>]/<dbname>`
28+
- `URLS`: Comma-separated list of database URLs to backup. Format for PostgreSQL: `postgres://<user>:<password>@<host>[:<port>]/<dbname>` and for MySQL: `mysql://<user>:<password>@<host>[:<port>]/<dbname>`
2929
- `S3_ENDPOINT`: The endpoint URL of your S3-compatible storage service.
3030
- `S3_BUCKET`: The name of the bucket where backups will be stored.
3131
- `S3_ACCESS_KEY`: Your S3 access key.
@@ -41,7 +41,7 @@ services:
4141
app:
4242
build: .
4343
environment:
44-
URLS: "postgres://user:password@host:port/dbname"
44+
URLS: "postgres://user:password@host:port/dbname,mysql://user:password@host:port/dbname"
4545
S3_ENDPOINT: "your_s3_endpoint"
4646
S3_BUCKET: "your_s3_bucket"
4747
S3_ACCESS_KEY: "your_s3_access_key"
@@ -51,7 +51,7 @@ services:
5151
5252
## Running the Application with Docker
5353
54-
There is an image available on `ghcr.io/thedevminertv/postgres_s3_backup` that you can use.
54+
There is an image available on `ghcr.io/thedevminertv/database-s3-backup` that you can use.
5555

5656
Alternatively, you can build the image yourself:
5757

@@ -67,7 +67,7 @@ Alternatively, you can build the image yourself:
6767
docker compose up -d
6868
```
6969

70-
This will start the application in the background. It will automatically perform backups based on the configured interval and upload them to the specified S3 bucket.
70+
This will start the application in the background. It will automatically perform backups for both PostgreSQL and MySQL databases based on the configured interval and upload them to the specified S3 bucket.
7171

7272
## Monitoring and Logs
7373

dump.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"errors"
6+
"fmt"
7+
"os/exec"
8+
"strconv"
9+
"time"
10+
)
11+
12+
type connectionOptions struct {
13+
Host string
14+
DbType string
15+
Port int
16+
Database string
17+
Username string
18+
Password string
19+
}
20+
21+
var (
22+
PGDumpCmd = "pg_dump"
23+
pgDumpStdOpts = []string{"--no-owner", "--no-acl", "--clean", "--blobs", "-v"}
24+
pgDumpDefaultFormat = "c"
25+
ErrPgDumpNotFound = errors.New("pg_dump not found")
26+
27+
MysqlDumpCmd = "mysqldump"
28+
mysqlDumpStdOpts = []string{"--compact", "--skip-add-drop-table", "--skip-add-locks", "--skip-disable-keys", "--skip-set-charset", "-v"}
29+
ErrMySqlDumpNotFound = errors.New("mysqldump not found")
30+
31+
ErrUnsupportedType = errors.New("unsupported database type")
32+
)
33+
34+
func RunDump(connectionOpts *connectionOptions, outFile string) error {
35+
cmd, err := buildDumpCommand(connectionOpts, outFile)
36+
if err != nil {
37+
return err
38+
}
39+
40+
return executeCommand(cmd)
41+
}
42+
43+
func buildDumpCommand(opts *connectionOptions, outFile string) (*exec.Cmd, error) {
44+
switch opts.DbType {
45+
case "postgres":
46+
if !commandExist(PGDumpCmd) {
47+
return nil, ErrPgDumpNotFound
48+
}
49+
options := append(
50+
pgDumpStdOpts,
51+
fmt.Sprintf("-f%s", outFile),
52+
fmt.Sprintf("--dbname=%s", opts.Database),
53+
fmt.Sprintf("--host=%s", opts.Host),
54+
fmt.Sprintf("--port=%d", opts.Port),
55+
fmt.Sprintf("--username=%s", opts.Username),
56+
fmt.Sprintf("--format=%s", pgDumpDefaultFormat),
57+
)
58+
return exec.Command(PGDumpCmd, options...), nil
59+
60+
case "mysql":
61+
mysqldumpCmd := "mysqldump"
62+
if !commandExist(mysqldumpCmd) {
63+
return nil, ErrMySqlDumpNotFound
64+
}
65+
options := append(
66+
mysqlDumpStdOpts,
67+
"-h", opts.Host,
68+
"-P", strconv.Itoa(opts.Port),
69+
"-u", opts.Username,
70+
fmt.Sprintf("--password=%s", opts.Password),
71+
"--databases", opts.Database,
72+
"-r", outFile,
73+
)
74+
75+
return exec.Command(mysqldumpCmd, options...), nil
76+
77+
default:
78+
return nil, ErrUnsupportedType
79+
}
80+
}
81+
82+
func executeCommand(cmd *exec.Cmd) error {
83+
stderr, err := cmd.StderrPipe()
84+
if err != nil {
85+
return err
86+
}
87+
88+
if err := cmd.Start(); err != nil {
89+
return err
90+
}
91+
92+
go func() {
93+
scanner := bufio.NewScanner(stderr)
94+
for scanner.Scan() {
95+
fmt.Println(scanner.Text())
96+
}
97+
}()
98+
99+
if err := cmd.Wait(); err != nil {
100+
return err
101+
}
102+
return nil
103+
}
104+
105+
func commandExist(command string) bool {
106+
_, err := exec.LookPath(command)
107+
return err == nil
108+
}
109+
110+
func newFileName(db string, dbType string) string {
111+
switch dbType {
112+
case "postgres":
113+
return fmt.Sprintf(`%v_%v.pgdump`, db, time.Now().Unix())
114+
case "mysql":
115+
return fmt.Sprintf(`%v_%v.sql`, db, time.Now().Unix())
116+
}
117+
return fmt.Sprintf(`%v_%v`, db, time.Now().Unix())
118+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ require (
2020
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2121
github.com/modern-go/reflect2 v1.0.2 // indirect
2222
github.com/rs/xid v1.5.0 // indirect
23-
github.com/sirupsen/logrus v1.9.3 // indirect
23+
github.com/stretchr/testify v1.7.0 // indirect
2424
golang.org/x/crypto v0.19.0 // indirect
2525
golang.org/x/net v0.21.0 // indirect
2626
golang.org/x/sys v0.17.0 // indirect

go.sum

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,19 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
44
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
55
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
66
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
7-
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
8-
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
9-
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
10-
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
117
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
128
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
139
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
1410
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
1511
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
1612
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
17-
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
18-
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
19-
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
20-
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
2113
github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2eRGjI=
2214
github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
2315
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
24-
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
25-
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
2616
github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
2717
github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
2818
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
2919
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
30-
github.com/minio/minio-go/v7 v7.0.63 h1:GbZ2oCvaUdgT5640WJOpyDhhDxvknAJU2/T3yurwcbQ=
31-
github.com/minio/minio-go/v7 v7.0.63/go.mod h1:Q6X7Qjb7WMhvG65qKf4gUgA5XaiSox74kR1uAEjxRS4=
32-
github.com/minio/minio-go/v7 v7.0.64 h1:Zdza8HwOzkld0ZG/og50w56fKi6AAyfqfifmasD9n2Q=
33-
github.com/minio/minio-go/v7 v7.0.64/go.mod h1:R4WVUR6ZTedlCcGwZRauLMIKjgyaWxhs4Mqi/OMPmEc=
34-
github.com/minio/minio-go/v7 v7.0.65 h1:sOlB8T3nQK+TApTpuN3k4WD5KasvZIE3vVFzyyCa0go=
35-
github.com/minio/minio-go/v7 v7.0.65/go.mod h1:R4WVUR6ZTedlCcGwZRauLMIKjgyaWxhs4Mqi/OMPmEc=
36-
github.com/minio/minio-go/v7 v7.0.66 h1:bnTOXOHjOqv/gcMuiVbN9o2ngRItvqE774dG9nq0Dzw=
37-
github.com/minio/minio-go/v7 v7.0.66/go.mod h1:DHAgmyQEGdW3Cif0UooKOyrT3Vxs82zNdV6tkKhRtbs=
38-
github.com/minio/minio-go/v7 v7.0.67 h1:BeBvZWAS+kRJm1vGTMJYVjKUNoo0FoEt/wUWdUtfmh8=
39-
github.com/minio/minio-go/v7 v7.0.67/go.mod h1:+UXocnUeZ3wHvVh5s95gcrA4YjMIbccT6ubB+1m054A=
40-
github.com/minio/minio-go/v7 v7.0.68 h1:hTqSIfLlpXaKuNy4baAp4Jjy2sqZEN9hRxD0M4aOfrQ=
41-
github.com/minio/minio-go/v7 v7.0.68/go.mod h1:XAvOPJQ5Xlzk5o3o/ArO2NMbhSGkimC+bpW/ngRKDmQ=
4220
github.com/minio/minio-go/v7 v7.0.69 h1:l8AnsQFyY1xiwa/DaQskY4NXSLA2yrGsW5iD9nRPVS0=
4321
github.com/minio/minio-go/v7 v7.0.69/go.mod h1:XAvOPJQ5Xlzk5o3o/ArO2NMbhSGkimC+bpW/ngRKDmQ=
4422
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
@@ -52,36 +30,17 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
5230
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5331
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
5432
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
55-
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
56-
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
5733
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5834
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
5935
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
6036
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
61-
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
62-
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
63-
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
64-
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
65-
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
66-
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
6737
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
6838
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
69-
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
70-
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
71-
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
72-
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
7339
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
7440
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
75-
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7641
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
77-
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
78-
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
79-
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
80-
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
8142
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
8243
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
83-
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
84-
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
8544
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
8645
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
8746
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

main.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package main
33
import (
44
"context"
55
"flag"
6-
"github.com/joho/godotenv"
7-
"github.com/minio/minio-go/v7"
8-
"github.com/minio/minio-go/v7/pkg/credentials"
96
"log"
107
"net/url"
118
"os"
129
"strconv"
1310
"strings"
1411
"time"
12+
13+
"github.com/joho/godotenv"
14+
"github.com/minio/minio-go/v7"
15+
"github.com/minio/minio-go/v7/pkg/credentials"
1516
)
1617

1718
func main() {
@@ -42,7 +43,13 @@ func main() {
4243
log.Fatalf("Failed to parse url %s: %s", rawUrl, err)
4344
}
4445

45-
port := 5432
46+
port := 0
47+
switch parsedUrl.Scheme {
48+
case "postgres":
49+
port = 5432
50+
case "mysql":
51+
port = 3306
52+
}
4653
rawPort := parsedUrl.Port()
4754
if rawPort != "" {
4855
port, err = strconv.Atoi(rawPort)
@@ -58,6 +65,7 @@ func main() {
5865

5966
urls[i] = connectionOptions{
6067
Host: parsedUrl.Hostname(),
68+
DbType: parsedUrl.Scheme,
6169
Port: port,
6270
Database: strings.TrimPrefix(parsedUrl.Path, "/"),
6371
Username: parsedUrl.User.Username(),
@@ -79,8 +87,7 @@ func main() {
7987
for {
8088
for _, u := range urls {
8189
log.Printf("Backing up %s", u.Database)
82-
83-
file := newFileName(u.Database)
90+
file := newFileName(u.Database, u.DbType)
8491

8592
if err = RunDump(&u, file); err != nil {
8693
log.Printf("WARNING: Failed to dump database: %s", err)

0 commit comments

Comments
 (0)