Skip to content

Support connecting with a unix domain socket #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ docker run -d --restart=always -e DB_DUMP_FREQ=60 -e DB_DUMP_BEGIN=2330 -e DB_DU
# or

mysql-backup dump --frequency=60 --begin=2330 --target=/local/file/path --server=my-db-address

# or to connect to a local mysqld via the unix domain socket as the current user

mysql-backup dump --frequency=60 --begin=2330 --target=/local/file/path --server=/run/mysqld/mysqld.sock
````

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

__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__

* `DB_SERVER`: hostname to connect to database. Required.
* `DB_SERVER`: hostname or unix domain socket path (starting with a slash) to connect to database. Required.
* `DB_PORT`: port to use to connect to database. Optional, defaults to `3306`
* `DB_USER`: username for the database
* `DB_PASS`: password for the database
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The following are the environment variables, CLI flags and configuration file op

| Purpose | Backup / Restore | CLI Flag | Env Var | Config Key | Default |
| --- | --- | --- | --- | --- | --- |
| hostname to connect to database. Required. | BR | `server` | `DB_SERVER` | `database.server` | |
| hostname or unix domain socket path (starting with a slash) to connect to database. Required. | BR | `server` | `DB_SERVER` | `database.server` | |
| port to use to connect to database. Optional. | BR | `port` | `DB_PORT` | `database.port` | 3306 |
| username for the database | BR | `user` | `DB_USER` | `database.credentials.username` | |
| password for the database | BR | `pass` | `DB_PASS` | `database.credentials.password` | |
Expand Down
2 changes: 1 addition & 1 deletion docs/database_address.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ In order to perform the actual dump or restore, `mysql-backup` needs to connect
db-server: my-db-address
```

The address itself, in the above example `my-db-address`, can be a container or any database process, as long as it is
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
accessible from where the `mysql-backup` runs.

The default port is `3306`, the normal default port for mysql. You can override the default port of `3306` via
Expand Down
10 changes: 8 additions & 2 deletions pkg/database/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"fmt"
"strings"

mysql "github.com/go-sql-driver/mysql"
)
Expand All @@ -17,8 +18,13 @@ func (c Connection) MySQL() string {
config := mysql.NewConfig()
config.User = c.User
config.Passwd = c.Pass
config.Net = "tcp"
config.Addr = fmt.Sprintf("%s:%d", c.Host, c.Port)
if strings.HasPrefix(c.Host, "/") {
config.Net = "unix"
config.Addr = c.Host
} else {
config.Net = "tcp"
config.Addr = fmt.Sprintf("%s:%d", c.Host, c.Port)
}
config.ParseTime = true
return config.FormatDSN()
}
2 changes: 1 addition & 1 deletion pkg/database/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func init() {
}

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