Skip to content

Commit

Permalink
add option to backup without locking table on mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
Biptaste committed Jun 5, 2024
1 parent 14712e9 commit 539087c
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 4 deletions.
11 changes: 8 additions & 3 deletions actions/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func makeDump(ctx domain.ExecutionContext, dbBackup domain.DatabaseBackupConfig,
}

if dbType == "mysql" || dbType == "mariadb" {
return mysqlDump(containerID, config.Env, backupDir, dbBackup.Databases, verbose)
return mysqlDump(containerID, config.Env, backupDir, dbBackup.Databases, dbBackup.NoLock, verbose)
} else if dbType == "postgres" {
return postgresDump(containerID, config.Env, backupDir, dbBackup.Databases, verbose)
} else if dbType == "mongo" {
Expand All @@ -203,7 +203,7 @@ func makeDump(ctx domain.ExecutionContext, dbBackup domain.DatabaseBackupConfig,
}
}

func mysqlDump(containerId string, env domain.DockerContainerEnv, backupDir string, databases []string, verbose bool) error {
func mysqlDump(containerId string, env domain.DockerContainerEnv, backupDir string, databases []string, noLock bool, verbose bool) error {

password := ""
if value, ok := env["MYSQL_ROOT_PASSWORD"]; ok {
Expand All @@ -220,7 +220,12 @@ func mysqlDump(containerId string, env domain.DockerContainerEnv, backupDir stri
}

for _, database := range mysqlDatabases {
cmd := domain.NewCommand([]string{"docker", "exec", "-i", containerId, "mysqldump", fmt.Sprintf("--password=%s", password), database}, verbose)
cmdArgs := []string{"docker", "exec", "-i", containerId, "mysqldump", fmt.Sprintf("--password=%s", password), database}
if noLock {
cmdArgs = []string{"docker", "exec", "-i", containerId, "mysqldump", "--single-transaction", "--skip-lock-tables", fmt.Sprintf("--password=%s", password), database}
}

cmd := domain.NewCommand(cmdArgs, verbose)

file, err := ioutil.TempFile(backupDir, "plizdump")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (parsed parserConfig) convertToConfig(config *domain.Config) error {
dbBackupConfig := domain.DatabaseBackupConfig{
Container: parsed.Backup.Databases[i].Container,
Type: parsed.Backup.Databases[i].Type,
NoLock: parsed.Backup.Databases[i].NoLock,
Databases: parsed.Backup.Databases[i].Databases,
}
backupConfig.Databases = append(backupConfig.Databases, dbBackupConfig)
Expand Down
1 change: 1 addition & 0 deletions config/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ type BackupSpec struct {
type DatabaseBackupSpec struct {
Container string `yaml:"container"`
Type string `yaml:"type"`
NoLock bool `yaml:"no_lock"`
Databases []string `yaml:"databases"`
}
1 change: 1 addition & 0 deletions domain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ type Backup struct {
type DatabaseBackupConfig struct {
Container string
Type string
NoLock bool
Databases []string
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {

app := cli.App("pliz", "Manage projects building")

app.Version("v version", "Pliz 13")
app.Version("v version", "Pliz 14-rc1")

// option to change the Pliz env
plizEnv := app.String(cli.StringOpt{
Expand Down
1 change: 1 addition & 0 deletions pliz.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ backup:
databases:
- container: db
type: mysql # mysql|mariadb|postgres|mongo, optional. If not present, the image name is used to try to guess the type
no_lock: false # only for mysql, add --single-transaction --skip-lock-tables arguments to avoid to lock table
databases: # only used for mysql. List of databases to backup
- db
- ghost

0 comments on commit 539087c

Please sign in to comment.