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

59 lines
1.7 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{
2025-02-09 20:07:57 +08:00
"/home/vomitblood/build/cspj-application/docker/chungus/logs/host-fs-accesslog.log",
"/home/vomitblood/build/cspj-application/docker/chungus/logs/host-fs-auditlog.log",
"/home/vomitblood/build/cspj-application/docker/chungus/logs/host-fs-errorlog.log",
2025-02-09 18:51:53 +08:00
}
var remoteFiles = []string{
2025-02-09 20:07:57 +08:00
"host-fs-accesslog.log",
"host-fs-auditlog.log",
"host-fs-errorlog.log",
2025-02-09 18:51:53 +08:00
}
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)
}
}
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)
}
}
return nil
}