forked from provenance-io/cosmovisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.go
55 lines (50 loc) · 1.47 KB
/
backup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cosmovisor
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/otiai10/copy"
)
// BackupData backs up the data directory located at $DAEMON_BACKUP_DATA_DIR to
// $DAEMON_HOME/backups/$plan/data and create keep at $DAEMON_HOME/backups/$plan/.keep
func BackupData(cfg *Config, upgradeInfo *UpgradeInfo) error {
backupDir := cfg.BackupDir(upgradeInfo.Name)
// Stamp file for completion tracking.
backupStamp := fmt.Sprintf("%s/.keep", backupDir)
// If stamp exists, this plan has executed the backup already.
if _, err := os.Stat(backupStamp); err == nil {
return nil
}
// Make backup dir if it doesn't exist.
if _, err := os.Stat(backupDir); os.IsNotExist(err) {
if err := os.MkdirAll(backupDir, 0700); err != nil {
return err
}
}
// Perform the copy from data src -> backup dst.
if err := copy.Copy(cfg.DataDir, filepath.Join(backupDir, "data")); err != nil {
return err
}
// Touch the stamp file if everything completed.
if _, err := TouchFile(backupStamp); err != nil {
return err
}
// Success!
return nil
}
// TouchFile creates a file at the location similar to the POSIX `touch` command.
func TouchFile(file string) (time.Time, error) {
if _, err := os.Stat(file); os.IsNotExist(err) {
file, ee := os.Create(file)
if ee != nil {
return time.Time{}, ee
}
defer file.Close()
}
currentTime := time.Now().Local()
if err := os.Chtimes(file, currentTime, currentTime); err != nil {
return time.Time{}, err
}
return currentTime, nil
}