Skip to content

Commit ce35acf

Browse files
committed
Implement "before-copy" and "after-copy" in retention section
1 parent 0041174 commit ce35acf

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

config/profile.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ type RetentionSection struct {
243243
OtherFlagsSection `mapstructure:",squash"`
244244
BeforeBackup maybe.Bool `mapstructure:"before-backup" description:"Apply retention before starting the backup command"`
245245
AfterBackup maybe.Bool `mapstructure:"after-backup" description:"Apply retention after the backup command succeeded. Defaults to true in configuration format v2 if any \"keep-*\" flag is set and \"before-backup\" is unset"`
246+
BeforeCopy maybe.Bool `mapstructure:"before-copy" description:"Apply retention before starting the copy command"`
247+
AfterCopy maybe.Bool `mapstructure:"after-copy" description:"Apply retention after the copy command succeeded. Defaults to true in configuration format v2 if any \"keep-*\" flag is set and \"before-backup\" is unset"`
246248
}
247249

248250
func (r *RetentionSection) IsEmpty() bool { return r == nil }
@@ -252,20 +254,27 @@ func (r *RetentionSection) resolve(profile *Profile) {
252254

253255
// Special cases of retention
254256
isSet := func(flags OtherFlags, name string) (found bool) { _, found = flags.GetOtherFlags()[name]; return }
255-
hasBackup := !profile.Backup.IsEmpty()
257+
hasBackup := !profile.Backup.IsEmpty() && (r.BeforeBackup.IsTrue() || r.AfterBackup.IsTrue())
258+
hasCopy := !profile.Copy.IsEmpty() && (r.BeforeCopy.IsTrue() || r.AfterCopy.IsTrue())
256259

257260
// Copy "source" from "backup" as "path" if it hasn't been redefined
258261
if hasBackup && !isSet(r, constants.ParameterPath) {
259262
r.SetOtherFlag(constants.ParameterPath, true)
260263
}
261264

265+
// Copy "source" from "backup" as "path" if it hasn't been redefined
266+
if hasCopy && !isSet(r, constants.ParameterPath) {
267+
r.SetOtherFlag(constants.ParameterPath, true)
268+
}
269+
262270
// Extras, only enabled for Version >= 2 (to remain backward compatible in version 1)
263271
if profile.config != nil && profile.config.version >= Version02 {
264272
// Auto-enable "after-backup" if nothing was specified explicitly and any "keep-" was configured
265-
if r.AfterBackup.IsUndefined() && r.BeforeBackup.IsUndefined() {
273+
if r.AfterBackup.IsUndefined() && r.BeforeBackup.IsUndefined() && r.AfterCopy.IsUndefined() && r.BeforeCopy.IsUndefined() {
266274
for name := range r.OtherFlags {
267275
if strings.HasPrefix(name, "keep-") {
268276
r.AfterBackup = maybe.True()
277+
hasBackup = true
269278
break
270279
}
271280
}
@@ -280,12 +289,23 @@ func (r *RetentionSection) resolve(profile *Profile) {
280289
r.SetOtherFlag(constants.ParameterTag, true)
281290
}
282291

292+
// Copy "tag" from "copy" if it was set and hasn't been redefined here
293+
// Allow setting it at profile level when not defined in "backup" nor "retention"
294+
if hasCopy &&
295+
!isSet(r, constants.ParameterTag) &&
296+
isSet(profile.Copy, constants.ParameterTag) {
297+
298+
r.SetOtherFlag(constants.ParameterTag, true)
299+
}
300+
283301
// Copy "host" from "backup" if it was set and hasn't been redefined here
284302
// Or use os.Hostname() same as restic does for backup when not setting it, see:
285303
// https://github.com/restic/restic/blob/master/cmd/restic/cmd_backup.go#L48
286304
if !isSet(r, constants.ParameterHost) {
287305
if hasBackup && isSet(profile.Backup, constants.ParameterHost) {
288306
r.SetOtherFlag(constants.ParameterHost, profile.Backup.OtherFlags[constants.ParameterHost])
307+
} else if hasCopy && isSet(profile.Copy, constants.ParameterHost) {
308+
r.SetOtherFlag(constants.ParameterHost, profile.Copy.OtherFlags[constants.ParameterHost])
289309
} else if !isSet(profile, constants.ParameterHost) {
290310
r.SetOtherFlag(constants.ParameterHost, true) // resolved with os.Hostname()
291311
}

wrapper.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,34 @@ func (r *resticWrapper) getCommandAction(command string) func() error {
141141
func (r *resticWrapper) getCopyAction() func() error {
142142
copyAction := r.getCommandAction(constants.CommandCopy)
143143

144-
return func() error {
144+
return func() (err error) {
145145
// we might need to initialize the secondary repository (the copy target)
146146
if r.global.Initialize || (r.profile.Copy != nil && r.profile.Copy.Initialize) {
147147
_ = r.runInitializeCopy() // it's ok if the initialization returned an error
148148
}
149149

150-
return copyAction()
150+
// Retention before
151+
if r.profile.Retention != nil && r.profile.Retention.BeforeCopy.IsTrue() {
152+
err = r.runRetention()
153+
if err != nil {
154+
return
155+
}
156+
}
157+
158+
err = copyAction()
159+
if err != nil {
160+
return
161+
}
162+
163+
// Retention after
164+
if r.profile.Retention != nil && r.profile.Retention.AfterCopy.IsTrue() {
165+
err = r.runRetention()
166+
if err != nil {
167+
return
168+
}
169+
}
170+
171+
return
151172
}
152173
}
153174

0 commit comments

Comments
 (0)