cspj-application/server/internal/log_backup/log_backup.go

61 lines
1.6 KiB
Go
Raw Normal View History

2025-02-09 18:51:53 +08:00
package log_backup
import (
"fmt"
"log"
"github.com/Vomitblood/cspj-application/server/internal/webdav"
"github.com/studio-b12/gowebdav"
)
// TODO: use values from config file
var localLogPaths = []string{
"/path/to/file1.log",
"/path/to/file2.log",
}
var remoteFiles = []string{
"/my/remote/folder/file1.log",
"/my/remote/folder/file2.log",
}
func BackupLogs(client *gowebdav.Client) error {
// check if there are equal number of local and remote file paths
if len(localLogPaths) != len(remoteFiles) {
return fmt.Errorf("mismatch between local log paths and remote paths")
}
// loop through each file and upload it
for i := range localLogPaths {
err := webdav.UploadFile(client, localLogPaths[i], remoteFiles[i])
if err != nil {
log.Printf("Error uploading file %s: %v", localLogPaths[i], err)
return fmt.Errorf("error uploading file %s: %v", localLogPaths[i], err)
} else {
log.Printf("Successfully uploaded file: %s", localLogPaths[i])
}
}
return nil
}
func RestoreLogs(client *gowebdav.Client) error {
// check if there are equal number of local and remote file paths
if len(remoteFiles) != len(localLogPaths) {
return fmt.Errorf("mismatch between remote files and local paths")
}
// loop through each remote file and download it
for i := range remoteFiles {
err := webdav.DownloadFile(client, remoteFiles[i], localLogPaths[i])
if err != nil {
log.Printf("Error downloading file %s: %v", remoteFiles[i], err)
return fmt.Errorf("error downloading file %s: %v", remoteFiles[i], err)
} else {
log.Printf("Successfully downloaded file: %s", remoteFiles[i])
}
}
return nil
}