-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
801 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package data | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
"github.com/Parallels/prl-devops-service/basecontext" | ||
"github.com/cjlapao/common-go/helper" | ||
) | ||
|
||
func (j *JsonDatabase) Backup(ctx basecontext.ApiContext) error { | ||
backupFiles, err := findBackupFiles(j.filename) | ||
if err != nil { | ||
ctx.LogErrorf("[Database] Error finding backup files: %v", err) | ||
return err | ||
} | ||
|
||
if len(backupFiles) >= j.Config.NumberOfBackupFiles { | ||
// Delete the oldest backup file | ||
oldestFile := backupFiles[0] | ||
err := os.Remove(oldestFile) | ||
if err != nil { | ||
ctx.LogErrorf("[Database] Error deleting backup file: %v", err) | ||
return err | ||
} | ||
} | ||
|
||
// Create a new backup file with timestamp | ||
timestamp := time.Now().Format("20060102150405") | ||
newBackupFile := fmt.Sprintf("%s.save.bak.%s", j.filename, timestamp) | ||
err = helper.CopyFile(j.filename, newBackupFile) | ||
if err != nil { | ||
ctx.LogErrorf("[Database] Error creating new backup file: %v", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func findBackupFiles(filename string) ([]string, error) { | ||
dir := filepath.Dir(filename) | ||
base := filepath.Base(filename) | ||
pattern := fmt.Sprintf("%s.save.bak.*", base) | ||
matches, err := filepath.Glob(filepath.Join(dir, pattern)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Sort the backup files by timestamp | ||
sort.Slice(matches, func(i, j int) bool { | ||
return extractTimestamp(matches[i]) < extractTimestamp(matches[j]) | ||
}) | ||
|
||
return matches, nil | ||
} | ||
|
||
func extractTimestamp(filename string) string { | ||
parts := strings.Split(filename, ".") | ||
return parts[len(parts)-1] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package data | ||
|
||
import "encoding/json" | ||
|
||
func Diff(a interface{}, b interface{}) bool { | ||
jsonA, err := json.Marshal(a) | ||
if err != nil { | ||
return false | ||
} | ||
jsonB, err := json.Marshal(b) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return string(jsonA) != string(jsonB) | ||
} |
Oops, something went wrong.