Skip to content

Commit 2468197

Browse files
committed
Support connecting with a unix domain socket
Fixes #285
1 parent ea1580b commit 2468197

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ docker run -d --restart=always -e DB_DUMP_FREQ=60 -e DB_DUMP_BEGIN=2330 -e DB_DU
4646
# or
4747

4848
mysql-backup dump --frequency=60 --begin=2330 --target=/local/file/path --server=my-db-address
49+
50+
# or to connect to a local mysqld via the unix domain socket as the current user
51+
52+
mysql-backup dump --frequency=60 --begin=2330 --target=/local/file/path --server=/run/mysqld/mysqld.sock
4953
````
5054

5155
Or `mysql-backup --config-file=/path/to/config/file.yaml` where `/path/to/config/file.yaml` is a file
@@ -87,7 +91,7 @@ You need only the following environment variables:
8791

8892
__You should consider the [use of `--env-file=`](https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables-e-env-env-file) to keep your secrets out of your shell history__
8993

90-
* `DB_SERVER`: hostname to connect to database. Required.
94+
* `DB_SERVER`: hostname or unix domain socket path (starting with a slash) to connect to database. Required.
9195
* `DB_PORT`: port to use to connect to database. Optional, defaults to `3306`
9296
* `DB_USER`: username for the database
9397
* `DB_PASS`: password for the database

docs/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ The following are the environment variables, CLI flags and configuration file op
6262

6363
| Purpose | Backup / Restore | CLI Flag | Env Var | Config Key | Default |
6464
| --- | --- | --- | --- | --- | --- |
65-
| hostname to connect to database. Required. | BR | `server` | `DB_SERVER` | `database.server` | |
65+
| hostname or unix domain socket path (starting with a slash) to connect to database. Required. | BR | `server` | `DB_SERVER` | `database.server` | |
6666
| port to use to connect to database. Optional. | BR | `port` | `DB_PORT` | `database.port` | 3306 |
6767
| username for the database | BR | `user` | `DB_USER` | `database.credentials.username` | |
6868
| password for the database | BR | `pass` | `DB_PASS` | `database.credentials.password` | |

docs/database_address.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ In order to perform the actual dump or restore, `mysql-backup` needs to connect
99
db-server: my-db-address
1010
```
1111
12-
The address itself, in the above example `my-db-address`, can be a container or any database process, as long as it is
12+
The address itself, in the above example `my-db-address`, can be a hostname, ip address, or path to a unix domain socket , as long as it is
1313
accessible from where the `mysql-backup` runs.
1414

1515
The default port is `3306`, the normal default port for mysql. You can override the default port of `3306` via

pkg/database/connection.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package database
22

33
import (
44
"fmt"
5+
"strings"
56

67
mysql "github.com/go-sql-driver/mysql"
78
)
@@ -17,8 +18,13 @@ func (c Connection) MySQL() string {
1718
config := mysql.NewConfig()
1819
config.User = c.User
1920
config.Passwd = c.Pass
20-
config.Net = "tcp"
21-
config.Addr = fmt.Sprintf("%s:%d", c.Host, c.Port)
21+
if strings.HasPrefix(c.Host, "/") {
22+
config.Net = "unix"
23+
config.Addr = c.Host
24+
} else {
25+
config.Net = "tcp"
26+
config.Addr = fmt.Sprintf("%s:%d", c.Host, c.Port)
27+
}
2228
config.ParseTime = true
2329
return config.FormatDSN()
2430
}

pkg/database/schemas.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func init() {
1717
}
1818

1919
func GetSchemas(dbconn Connection) ([]string, error) {
20-
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/", dbconn.User, dbconn.Pass, dbconn.Host, dbconn.Port))
20+
db, err := sql.Open("mysql", dbconn.MySQL())
2121
if err != nil {
2222
return nil, fmt.Errorf("failed to open connection to database: %v", err)
2323
}

0 commit comments

Comments
 (0)