]> git.giacomolli.uk - onechat/blobdiff - backup.go
Keep recovery advice out of log events
[onechat] / backup.go
index 9398772ce8e0839fde894b2ce79320c4e3fc3739..517b7c7af5eeeba953aff24e4d90830dd387bf22 100644 (file)
--- a/backup.go
+++ b/backup.go
@@ -93,42 +93,6 @@ func restore(ctx context.Context, path string, out io.Writer) error {
        return nil
 }
 
        return nil
 }
 
-// backupData stages a session snapshot, then copies the data under snapshot's
-// write locks. Compression runs after the locks are released.
-func backupData(ctx context.Context, dir, path string, whatsapp *WhatsApp, snapshot func(context.Context, func() error) error) (size int64, err error) {
-       if !filepath.IsAbs(path) {
-               return 0, errors.New("backup path must be absolute")
-       }
-
-       stage, err := os.MkdirTemp("", "onechat-backup-*")
-       if err != nil {
-               return 0, fmt.Errorf("create backup directory: %w", err)
-       }
-
-       defer func() {
-               err = errors.Join(err, os.RemoveAll(stage))
-       }()
-
-       err = whatsapp.BackupSession(ctx, filepath.Join(stage, whatsappDirName, whatsappSessionFileName))
-       if err != nil {
-               return 0, fmt.Errorf("copy WhatsApp session: %w", err)
-       }
-
-       err = snapshot(ctx, func() error {
-               return backupCopy(ctx, dir, stage, path)
-       })
-       if err != nil {
-               return 0, fmt.Errorf("copy data directory: %w", err)
-       }
-
-       size, err = backupArchive(ctx, stage, path)
-       if err != nil {
-               return 0, fmt.Errorf("archive backup: %w", err)
-       }
-
-       return size, nil
-}
-
 // backupRestore installs a validated archive into an empty data directory while
 // holding the token lock. It keeps the lock file so daemon starts use that same
 // inode, including after a failed restore.
 // backupRestore installs a validated archive into an empty data directory while
 // holding the token lock. It keeps the lock file so daemon starts use that same
 // inode, including after a failed restore.
@@ -205,6 +169,42 @@ func backupRestore(ctx context.Context, path, dir string) (err error) {
        return nil
 }
 
        return nil
 }
 
+// backupData stages a session snapshot, then copies the data under snapshot's
+// write locks. Compression runs after the locks are released.
+func backupData(ctx context.Context, dir, path string, whatsapp *WhatsApp, snapshot func(context.Context, func() error) error) (size int64, err error) {
+       if !filepath.IsAbs(path) {
+               return 0, errors.New("backup path must be absolute")
+       }
+
+       stage, err := os.MkdirTemp("", "onechat-backup-*")
+       if err != nil {
+               return 0, fmt.Errorf("create backup directory: %w", err)
+       }
+
+       defer func() {
+               err = errors.Join(err, os.RemoveAll(stage))
+       }()
+
+       err = whatsapp.BackupSession(ctx, filepath.Join(stage, whatsappDirName, whatsappSessionFileName))
+       if err != nil {
+               return 0, fmt.Errorf("copy WhatsApp session: %w", err)
+       }
+
+       err = snapshot(ctx, func() error {
+               return backupCopy(ctx, dir, stage, path)
+       })
+       if err != nil {
+               return 0, fmt.Errorf("copy data directory: %w", err)
+       }
+
+       size, err = backupArchive(ctx, stage, path)
+       if err != nil {
+               return 0, fmt.Errorf("archive backup: %w", err)
+       }
+
+       return size, nil
+}
+
 // backupCopy stages regular files and directories, excluding runtime files, the
 // output archive, and raw session database files. Each file keeps its mode.
 func backupCopy(ctx context.Context, source, target, output string) error {
 // backupCopy stages regular files and directories, excluding runtime files, the
 // output archive, and raw session database files. Each file keeps its mode.
 func backupCopy(ctx context.Context, source, target, output string) error {