From 1b744d4c1c25bb9cb6d182eedb743843e5b92164 Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Fri, 10 Dec 2021 10:12:53 +0100 Subject: [PATCH] Allow changing backup ownership --- cmd/backup/main.go | 12 +++++++++--- test/compose/docker-compose.yml | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cmd/backup/main.go b/cmd/backup/main.go index 5326039..b7a893d 100644 --- a/cmd/backup/main.go +++ b/cmd/backup/main.go @@ -105,6 +105,8 @@ type config struct { BackupPruningPrefix string `split_words:"true"` BackupStopContainerLabel string `split_words:"true" default:"true"` BackupFromSnapshot bool `split_words:"true"` + BackupUID int `split_words:"true" default:"-1"` + BackupGID int `split_words:"true" default:"-1"` AwsS3BucketName string `split_words:"true"` AwsEndpoint string `split_words:"true" default:"s3.amazonaws.com"` AwsEndpointProto string `split_words:"true" default:"https"` @@ -442,10 +444,14 @@ func (s *script) copyBackup() error { } if _, err := os.Stat(s.c.BackupArchive); !os.IsNotExist(err) { + if err := os.Chown(s.file, s.c.BackupUID, s.c.BackupGID); err != nil { + return fmt.Errorf("copyBackup: error changing owner on temp file: %w", err) + } if err := copyFile(s.file, path.Join(s.c.BackupArchive, name)); err != nil { return fmt.Errorf("copyBackup: error copying file to local archive: %w", err) } s.logger.Infof("Stored copy of backup `%s` in local archive `%s`.", s.file, s.c.BackupArchive) + if s.c.BackupLatestSymlink != "" { symlink := path.Join(s.c.BackupArchive, s.c.BackupLatestSymlink) if _, err := os.Lstat(symlink); err == nil { @@ -683,19 +689,19 @@ func lock(lockfile string) func() error { func copyFile(src, dst string) error { in, err := os.Open(src) if err != nil { - return err + return fmt.Errorf("copyFile: error opening source file: %w", err) } defer in.Close() out, err := os.Create(dst) if err != nil { - return err + return fmt.Errorf("copyFile: error creating destination: %w", err) } _, err = io.Copy(out, in) if err != nil { out.Close() - return err + return fmt.Errorf("copyFile: error copying: %w", err) } return out.Close() } diff --git a/test/compose/docker-compose.yml b/test/compose/docker-compose.yml index b9ade86..8f4b3b8 100644 --- a/test/compose/docker-compose.yml +++ b/test/compose/docker-compose.yml @@ -30,6 +30,8 @@ services: BACKUP_PRUNING_LEEWAY: 5s BACKUP_PRUNING_PREFIX: test GPG_PASSPHRASE: 1234secret + BACKUP_UID: '1000' + BACKUP_GID: '1000' volumes: - ./local:/archive - app_data:/backup/app_data:ro