Skip to content

Commit

Permalink
Merge pull request #27 from offen/latest-symlink
Browse files Browse the repository at this point in the history
Automatically create symlink to latest local backup if configured
  • Loading branch information
m90 authored Oct 1, 2021
2 parents cff418e + c59b40f commit 789fc65
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ You can populate below template according to your requirements and use it as you

# BACKUP_FILENAME="backup-%Y-%m-%dT%H-%M-%S.tar.gz"

# When storing local backups, a symlink to the latest backup can be created
# in case a value is given for this key. This has no effect on remote backups.

# BACKUP_LATEST_SYMLINK="backup.latest.tar.gz"

########### BACKUP STORAGE

# The name of the remote bucket that should be used for storing backups. If
Expand Down Expand Up @@ -437,6 +442,9 @@ services:
# ... define other services using the `data` volume here
backup:
image: offen/docker-volume-backup:latest
environment:
BACKUP_FILENAME: backup-%Y-%m-%dT%H-%M-%S.tar.gz
BACKUP_LATEST_SYMLINK: backup-latest.tar.gz
volumes:
- data:/backup/my-app-backup:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
Expand Down
13 changes: 12 additions & 1 deletion cmd/backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type script struct {
type config struct {
BackupSources string `split_words:"true" default:"/backup"`
BackupFilename string `split_words:"true" default:"backup-%Y-%m-%dT%H-%M-%S.tar.gz"`
BackupLatestSymlink string `split_words:"true"`
BackupArchive string `split_words:"true" default:"/archive"`
BackupRetentionDays int32 `split_words:"true" default:"-1"`
BackupPruningLeeway time.Duration `split_words:"true" default:"1m"`
Expand Down Expand Up @@ -380,6 +381,16 @@ func (s *script) copyBackup() error {
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 {
os.Remove(symlink)
}
if err := os.Symlink(name, symlink); err != nil {
return fmt.Errorf("copyBackup: error creating latest symlink: %w", err)
}
s.logger.Infof("Created/Updated symlink `%s` for latest backup.", s.c.BackupLatestSymlink)
}
}
return nil
}
Expand Down Expand Up @@ -497,7 +508,7 @@ func (s *script) pruneOldBackups() error {
)
}

if fi.ModTime().Before(deadline) {
if fi.Mode() != os.ModeSymlink && fi.ModTime().Before(deadline) {
matches = append(matches, candidate)
}
}
Expand Down
1 change: 1 addition & 0 deletions test/compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ services:
AWS_ENDPOINT_PROTO: http
AWS_S3_BUCKET_NAME: backup
BACKUP_FILENAME: test.tar.gz
BACKUP_LATEST_SYMLINK: test.latest.tar.gz.gpg
BACKUP_CRON_EXPRESSION: 0 0 5 31 2 ?
BACKUP_RETENTION_DAYS: ${BACKUP_RETENTION_DAYS:-7}
BACKUP_PRUNING_LEEWAY: 5s
Expand Down
1 change: 1 addition & 0 deletions test/compose/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ docker run --rm -it \

echo "[TEST:PASS] Found relevant files in untared remote backup."

test -L ./local/test.latest.tar.gz.gpg
echo 1234secret | gpg -d --yes --passphrase-fd 0 ./local/test.tar.gz.gpg > ./local/decrypted.tar.gz
tar -xf ./local/decrypted.tar.gz -C /tmp && test -f /tmp/backup/app_data/offen.db
rm ./local/decrypted.tar.gz
Expand Down

0 comments on commit 789fc65

Please sign in to comment.