Skip to content

Commit 583438b

Browse files
committed
Add BeforeCopy and AfterCopy to Retention
1 parent e5058f9 commit 583438b

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

config/profile.go

Lines changed: 3 additions & 1 deletion
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 backup copy succeeded. Defaults to true in configuration format v2 if any \"keep-*\" flag is set and \"before-copy\" is unset"`
246248
}
247249

248250
func (r *RetentionSection) IsEmpty() bool { return r == nil }
@@ -262,7 +264,7 @@ func (r *RetentionSection) resolve(profile *Profile) {
262264
// Extras, only enabled for Version >= 2 (to remain backward compatible in version 1)
263265
if profile.config != nil && profile.config.version >= Version02 {
264266
// Auto-enable "after-backup" if nothing was specified explicitly and any "keep-" was configured
265-
if r.AfterBackup.IsUndefined() && r.BeforeBackup.IsUndefined() {
267+
if r.AfterBackup.IsUndefined() && r.BeforeBackup.IsUndefined() && r.AfterCopy.IsUndefined() && r.BeforeCopy.IsUndefined() {
266268
for name := range r.OtherFlags {
267269
if strings.HasPrefix(name, "keep-") {
268270
r.AfterBackup = maybe.True()

wrapper.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,35 @@ 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+
// Copy command
159+
err = copyAction()
160+
if err != nil {
161+
return
162+
}
163+
164+
// Retention after
165+
if r.profile.Retention != nil && r.profile.Retention.AfterCopy.IsTrue() {
166+
err = r.runRetention()
167+
if err != nil {
168+
return
169+
}
170+
}
171+
172+
return
151173
}
152174
}
153175

0 commit comments

Comments
 (0)