|
@@ -2,6 +2,9 @@ package hybridBackup
|
|
|
|
|
|
import (
|
|
|
"log"
|
|
|
+ "path/filepath"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
/*
|
|
@@ -23,19 +26,55 @@ import (
|
|
|
*/
|
|
|
|
|
|
type BackupConfig struct {
|
|
|
- CycleCounter int64 //The number of backup executed in the background
|
|
|
- DiskUID string //The UID of the target fsandlr
|
|
|
- DiskPath string //The mount point for the disk
|
|
|
- ParentUID string //Parent virtal disk UUID
|
|
|
- ParentPath string //Parent disk path
|
|
|
- Mode string //Backup mode
|
|
|
+ CycleCounter int64 //The number of backup executed in the background
|
|
|
+ LastCycleTime int64 //The execution time of the last cycle
|
|
|
+ DiskUID string //The UID of the target fsandlr
|
|
|
+ DiskPath string //The mount point for the disk
|
|
|
+ ParentUID string //Parent virtal disk UUID
|
|
|
+ ParentPath string //Parent disk path
|
|
|
+ Mode string //Backup mode
|
|
|
}
|
|
|
|
|
|
+//Main handler function for hybrid backup
|
|
|
func HandleBackupProcess(backupConfig *BackupConfig) (string, error) {
|
|
|
log.Println(">>>>>> Running backup process: ", backupConfig)
|
|
|
+ rootPath := filepath.ToSlash(filepath.Clean(backupConfig.ParentPath))
|
|
|
+
|
|
|
+ //Check if the target disk is writable and mounted
|
|
|
+ if fileExists(filepath.Join(backupConfig.DiskPath, "aofs.db")) && fileExists(filepath.Join(backupConfig.DiskPath, "aofs.db.lock")) {
|
|
|
+ //This filesystem is mounted
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //File system not mounted. Skipping
|
|
|
+ log.Println("*HybridBackup* Skipping backup cycle for " + backupConfig.ParentUID + ":/")
|
|
|
+ return "Parent drive (" + backupConfig.ParentUID + ":/) not mounted. Skipping", nil
|
|
|
+ }
|
|
|
|
|
|
if backupConfig.Mode == "smart" {
|
|
|
+ //Smart backup mode. Scan new files every minutes and compare creation time scan every hour minutes
|
|
|
+ if backupConfig.CycleCounter%5 == 0 {
|
|
|
+ //Perform deep backup, use walk function
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //Perform shallow backup
|
|
|
+ fastWalk(rootPath, func(filename string) error {
|
|
|
+ rootAbs, _ := filepath.Abs(rootPath)
|
|
|
+ fileAbs, _ := filepath.Abs(filename)
|
|
|
+
|
|
|
+ rootAbs = filepath.ToSlash(filepath.Clean(rootAbs))
|
|
|
+ fileAbs = filepath.ToSlash(filepath.Clean(fileAbs))
|
|
|
+
|
|
|
+ relPath := strings.ReplaceAll(fileAbs, rootAbs, "")
|
|
|
+ assumedTargetPosition := filepath.Join(backupConfig.DiskPath, relPath)
|
|
|
+ if !fileExists(assumedTargetPosition) {
|
|
|
+ //Target file not exists in backup disk. Make a copy
|
|
|
+ //WIP
|
|
|
+ log.Println("Copying ", assumedTargetPosition)
|
|
|
+ }
|
|
|
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ }
|
|
|
} else if backupConfig.Mode == "nightly" {
|
|
|
|
|
|
} else if backupConfig.Mode == "append" {
|
|
@@ -44,6 +83,7 @@ func HandleBackupProcess(backupConfig *BackupConfig) (string, error) {
|
|
|
|
|
|
//Add one to the cycle counter
|
|
|
backupConfig.CycleCounter++
|
|
|
+ backupConfig.LastCycleTime = time.Now().Unix()
|
|
|
|
|
|
//Return the log information
|
|
|
return "", nil
|