Skip to content

Commit e160c24

Browse files
authored
Merge pull request #434 from databacker/triggers-and-functions
support for dumping triggers and functions and procedures
2 parents c010217 + 3bf90a9 commit e160c24

File tree

11 files changed

+394
-44
lines changed

11 files changed

+394
-44
lines changed

cmd/dump.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ func dumpCmd(passedExecs execs, cmdConfig *cmdConfiguration) (*cobra.Command, er
111111
if !v.IsSet("compact") && dumpConfig != nil && dumpConfig.Compact != nil {
112112
compact = *dumpConfig.Compact
113113
}
114+
// should we dump triggers and functions and procedures?
115+
triggers := v.GetBool("triggers")
116+
if !v.IsSet("triggers") && dumpConfig != nil && dumpConfig.Triggers != nil {
117+
triggers = *dumpConfig.Triggers
118+
}
119+
routines := v.GetBool("routines")
120+
if !v.IsSet("routines") && dumpConfig != nil && dumpConfig.Routines != nil {
121+
routines = *dumpConfig.Routines
122+
}
114123
maxAllowedPacket := v.GetInt("max-allowed-packet")
115124
if !v.IsSet("max-allowed-packet") && dumpConfig != nil && dumpConfig.MaxAllowedPacket != nil && *dumpConfig.MaxAllowedPacket != 0 {
116125
maxAllowedPacket = *dumpConfig.MaxAllowedPacket
@@ -242,6 +251,8 @@ func dumpCmd(passedExecs execs, cmdConfig *cmdConfiguration) (*cobra.Command, er
242251
PostBackupScripts: postBackupScripts,
243252
SuppressUseDatabase: noDatabaseName,
244253
Compact: compact,
254+
Triggers: triggers,
255+
Routines: routines,
245256
MaxAllowedPacket: maxAllowedPacket,
246257
Run: uid,
247258
FilenamePattern: filenamePattern,
@@ -318,6 +329,9 @@ S3: If it is a URL of the format s3://bucketname/path then it will connect via S
318329
// max-allowed-packet size
319330
flags.Int("max-allowed-packet", defaultMaxAllowedPacket, "Maximum size of the buffer for client/server communication, similar to mysqldump's max_allowed_packet. 0 means to use the default size.")
320331

332+
// whether to include triggers and functions
333+
flags.Bool("triggers-and-functions", false, "Whether to include triggers and functions in the dump.")
334+
321335
cmd.MarkFlagsMutuallyExclusive("once", "cron")
322336
cmd.MarkFlagsMutuallyExclusive("once", "begin")
323337
cmd.MarkFlagsMutuallyExclusive("once", "frequency")

docs/configuration.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ The following are the environment variables, CLI flags and configuration file op
8888
| SMB username, used only if a target does not have one | BRP | `smb-user` | `SMB_USER` | `dump.targets[smb-target].username` | |
8989
| SMB password, used only if a target does not have one | BRP | `smb-pass` | `SMB_PASS` | `dump.targets[smb-target].password` | |
9090
| compression to use, one of: `bzip2`, `gzip` | BP | `compression` | `DB_DUMP_COMPRESSION` | `dump.compression` | `gzip` |
91+
| whether to include triggers, procedures and functions | B | `triggers-and-functions` | `DB_DUMP_TRIGGERS_AND_FUNCTIONS` | `dump.triggersAndFunctions` | `false` |
9192
| when in container, run the dump or restore with `nice`/`ionice` | BR | `` | `NICE` | `` | `false` |
9293
| filename to save the target backup file | B | `dump --filename-pattern` | `DB_DUMP_FILENAME_PATTERN` | `dump.filenamePattern` | |
9394
| directory with scripts to execute before backup | B | `dump --pre-backup-scripts` | `DB_DUMP_PRE_BACKUP_SCRIPTS` | `dump.scripts.preBackup` | in container, `/scripts.d/pre-backup/` |
@@ -135,6 +136,7 @@ for details of each.
135136
* `once`: run once and exit
136137
* `compression`: the compression to use
137138
* `compact`: compact the dump
139+
* `triggersAndFunctions`: include triggers and functions and procedures in the dump
138140
* `maxAllowedPacket`: max packet size
139141
* `filenamePattern`: the filename pattern
140142
* `scripts`:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ require (
3131
)
3232

3333
require (
34-
github.com/databacker/api/go/api v0.0.0-20250418100420-12e1adda1303
34+
github.com/databacker/api/go/api v0.0.0-20250423183243-7775066c265e
3535
github.com/google/go-cmp v0.6.0
3636
go.opentelemetry.io/otel v1.31.0
3737
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ github.com/databacker/api/go/api v0.0.0-20250418091750-e67e3226ca5f h1:vuPsDEgli
8181
github.com/databacker/api/go/api v0.0.0-20250418091750-e67e3226ca5f/go.mod h1:bQhbl71Lk1ATni0H+u249hjoQ8ShAdVNcNjnw6z+SbE=
8282
github.com/databacker/api/go/api v0.0.0-20250418100420-12e1adda1303 h1:TVLyJzdvDvWIEs1/v6G0rQPpZeUsArQ7skzicjfCV8I=
8383
github.com/databacker/api/go/api v0.0.0-20250418100420-12e1adda1303/go.mod h1:bQhbl71Lk1ATni0H+u249hjoQ8ShAdVNcNjnw6z+SbE=
84+
github.com/databacker/api/go/api v0.0.0-20250423104730-2789787a240e h1:0ITg+YYAjyvM+rXirvZvx/PBLhMhHG+Nj5h+1flzWwY=
85+
github.com/databacker/api/go/api v0.0.0-20250423104730-2789787a240e/go.mod h1:bQhbl71Lk1ATni0H+u249hjoQ8ShAdVNcNjnw6z+SbE=
86+
github.com/databacker/api/go/api v0.0.0-20250423151229-1987d37f6e2f h1:JTyrIdH5lGiObGO7qFmON8ACIaCb5es+gjySkL/YWYc=
87+
github.com/databacker/api/go/api v0.0.0-20250423151229-1987d37f6e2f/go.mod h1:bQhbl71Lk1ATni0H+u249hjoQ8ShAdVNcNjnw6z+SbE=
88+
github.com/databacker/api/go/api v0.0.0-20250423183243-7775066c265e h1:5K7IbijS9p+dezx9m45CjFCR2Sf6BfT/tb540aEw66k=
89+
github.com/databacker/api/go/api v0.0.0-20250423183243-7775066c265e/go.mod h1:bQhbl71Lk1ATni0H+u249hjoQ8ShAdVNcNjnw6z+SbE=
8490
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8591
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
8692
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

pkg/core/dump.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func (e *Executor) Dump(ctx context.Context, opts DumpOptions) (DumpResults, err
3636
compressor := opts.Compressor
3737
encryptor := opts.Encryptor
3838
compact := opts.Compact
39+
triggers := opts.Triggers
40+
routines := opts.Routines
3941
suppressUseDatabase := opts.SuppressUseDatabase
4042
maxAllowedPacket := opts.MaxAllowedPacket
4143
filenamePattern := opts.FilenamePattern
@@ -105,6 +107,8 @@ func (e *Executor) Dump(ctx context.Context, opts DumpOptions) (DumpResults, err
105107
dbDumpCtx, dbDumpSpan := tracer.Start(ctx, "database_dump")
106108
if err := database.Dump(dbDumpCtx, dbconn, database.DumpOpts{
107109
Compact: compact,
110+
Triggers: triggers,
111+
Routines: routines,
108112
SuppressUseDatabase: suppressUseDatabase,
109113
MaxAllowedPacket: maxAllowedPacket,
110114
}, dw); err != nil {

pkg/core/dumpoptions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ type DumpOptions struct {
1919
PreBackupScripts string
2020
PostBackupScripts string
2121
Compact bool
22+
Triggers bool
23+
Routines bool
2224
SuppressUseDatabase bool
2325
MaxAllowedPacket int
2426
Run uuid.UUID

pkg/database/dump.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
type DumpOpts struct {
1212
Compact bool
13+
Triggers bool
14+
Routines bool
1315
SuppressUseDatabase bool
1416
MaxAllowedPacket int
1517
}
@@ -36,6 +38,8 @@ func Dump(ctx context.Context, dbconn Connection, opts DumpOpts, writers []DumpW
3638
Schema: schema,
3739
Host: dbconn.Host,
3840
Compact: opts.Compact,
41+
Triggers: opts.Triggers,
42+
Routines: opts.Routines,
3943
SuppressUseDatabase: opts.SuppressUseDatabase,
4044
MaxAllowedPacket: opts.MaxAllowedPacket,
4145
}

0 commit comments

Comments
 (0)