commit
83cedeac10
7 changed files with 93 additions and 49 deletions
|
|
@ -8,7 +8,7 @@ import (
|
|||
"scabiosa/Tools"
|
||||
)
|
||||
|
||||
func GenerateNewConfigs() *cli.Command {
|
||||
func GenerateNewConfigsCommand() *cli.Command {
|
||||
logger := Logging.Logger("generate-configs")
|
||||
|
||||
return &cli.Command{
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func StartBackupProc() *cli.Command {
|
||||
func StartBackupProcCommand() *cli.Command {
|
||||
logger := Logging.Logger("backup")
|
||||
|
||||
return &cli.Command{
|
||||
|
|
@ -21,43 +21,7 @@ func StartBackupProc() *cli.Command {
|
|||
Description: "Compresses and uploads/stores the backups",
|
||||
HelpName: "backup",
|
||||
Action: func(c *cli.Context) error {
|
||||
Tools.CheckIfConfigExists()
|
||||
config := Tools.GetConfig()
|
||||
|
||||
SQL.CreateDefaultTables(SQL.GetSQLInstance())
|
||||
|
||||
for _, backupItem := range config.FolderToBackup {
|
||||
|
||||
var storage StorageTypes.Storage
|
||||
var destPath string
|
||||
|
||||
if backupItem.RemoteStorageType != "none" {
|
||||
storage = StorageTypes.CheckStorageType(backupItem.RemoteStorageType)
|
||||
destPath = checkTmpPath(backupItem.CreateLocalBackup, backupItem.LocalTargetPath)
|
||||
} else {
|
||||
destPath = backupItem.LocalTargetPath
|
||||
}
|
||||
|
||||
bakFile := Compressor.CreateBakFile(backupItem.BackupName+getTimeSuffix(), backupItem.FolderPath, destPath, backupItem.BackupName)
|
||||
|
||||
if backupItem.RemoteStorageType != "none" {
|
||||
StorageTypes.UploadFile(storage, bakFile, backupItem.BackupName, backupItem.RemoteTargetPath)
|
||||
}
|
||||
|
||||
if !backupItem.CreateLocalBackup && backupItem.RemoteStorageType != "none" {
|
||||
backupItem.LocalTargetPath = "NONE"
|
||||
|
||||
_ = os.Remove(bakFile)
|
||||
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupItem.BackupName, SQL.SQLStage_DeleteTmp, SQL.REMOTE_NONE, "Deleted tmp file", time.Now())
|
||||
}
|
||||
|
||||
if backupItem.RemoteStorageType == "none" {
|
||||
backupItem.CreateLocalBackup = true
|
||||
backupItem.RemoteTargetPath = "NONE"
|
||||
}
|
||||
SQL.NewBackupEntry(SQL.GetSQLInstance(), backupItem.BackupName, time.Now(), backupItem.CreateLocalBackup, backupItem.FolderPath, StorageTypes.CheckRemoteStorageType(backupItem.RemoteStorageType), backupItem.RemoteTargetPath, backupItem.LocalTargetPath)
|
||||
}
|
||||
|
||||
StartBackupProc()
|
||||
return nil
|
||||
},
|
||||
OnUsageError: func(cc *cli.Context, err error, isSubcommand bool) error {
|
||||
|
|
@ -69,12 +33,52 @@ func StartBackupProc() *cli.Command {
|
|||
}
|
||||
}
|
||||
|
||||
func StartBackupProc() {
|
||||
Tools.CheckIfConfigExists()
|
||||
config := Tools.GetConfig()
|
||||
|
||||
SQL.CreateDefaultTables(SQL.GetSQLInstance())
|
||||
|
||||
for _, backupItem := range config.FolderToBackup {
|
||||
|
||||
var storage StorageTypes.Storage
|
||||
var destPath string
|
||||
|
||||
if backupItem.RemoteStorageType != "none" {
|
||||
storage = StorageTypes.CheckStorageType(backupItem.RemoteStorageType)
|
||||
destPath = checkTmpPath(backupItem.CreateLocalBackup, backupItem.LocalTargetPath)
|
||||
} else {
|
||||
destPath = backupItem.LocalTargetPath
|
||||
}
|
||||
|
||||
bakFile := Compressor.CreateBakFile(backupItem.BackupName+getTimeSuffix(), backupItem.FolderPath, destPath, backupItem.BackupName)
|
||||
|
||||
if backupItem.RemoteStorageType != "none" {
|
||||
StorageTypes.UploadFile(storage, bakFile, backupItem.BackupName, backupItem.RemoteTargetPath)
|
||||
}
|
||||
|
||||
if !backupItem.CreateLocalBackup && backupItem.RemoteStorageType != "none" {
|
||||
backupItem.LocalTargetPath = "NONE"
|
||||
|
||||
_ = os.Remove(bakFile)
|
||||
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupItem.BackupName, SQL.SQLStage_DeleteTmp, SQL.REMOTE_NONE, "Deleted tmp file", time.Now())
|
||||
}
|
||||
|
||||
if backupItem.RemoteStorageType == "none" {
|
||||
backupItem.CreateLocalBackup = true
|
||||
backupItem.RemoteTargetPath = "NONE"
|
||||
}
|
||||
SQL.NewBackupEntry(SQL.GetSQLInstance(), backupItem.BackupName, time.Now(), backupItem.CreateLocalBackup, backupItem.FolderPath, StorageTypes.CheckRemoteStorageType(backupItem.RemoteStorageType), backupItem.RemoteTargetPath, backupItem.LocalTargetPath)
|
||||
}
|
||||
}
|
||||
|
||||
func getTimeSuffix() string {
|
||||
currTime := time.Now()
|
||||
|
||||
return "_" + currTime.Format("02-01-2006_15-04")
|
||||
}
|
||||
|
||||
// skipcq: RVV-A0005
|
||||
func checkTmpPath(createLocalBackup bool, targetPath string) string {
|
||||
logger := Logging.DetailedLogger("mainThread", "checkTmpPath")
|
||||
if !createLocalBackup {
|
||||
|
|
|
|||
|
|
@ -19,21 +19,18 @@ func CreateBakFile(fileName string, folderPath string, destinationPath string, b
|
|||
|
||||
pathToFile := destinationPath + string(os.PathSeparator) + fileName + ".bak"
|
||||
|
||||
|
||||
fileToWrite, err := os.OpenFile(pathToFile, os.O_CREATE|os.O_RDWR, os.FileMode(0775))
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
compress(fileToWrite, folderPath, backupName)
|
||||
|
||||
|
||||
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, SQL.SQLStage_Compress, SQL.REMOTE_NONE, "File successfully written.", time.Now())
|
||||
|
||||
|
||||
return pathToFile
|
||||
}
|
||||
|
||||
func compress(fileToWrite *os.File, folderPath string, backupName string){
|
||||
func compress(fileToWrite *os.File, folderPath string, backupName string) {
|
||||
logger := Logging.DetailedLogger("Gzip", "compress")
|
||||
|
||||
zr, _ := gzip.NewWriterLevel(fileToWrite, flate.BestCompression)
|
||||
|
|
@ -41,6 +38,7 @@ func compress(fileToWrite *os.File, folderPath string, backupName string){
|
|||
|
||||
go fmt.Printf("[%s] Start compression...\n", backupName)
|
||||
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, SQL.SQLStage_Compress, SQL.REMOTE_NONE, "Start compression", time.Now())
|
||||
// skippcq: SCC-SA4009
|
||||
filepath.Walk(folderPath, func(file string, fi os.FileInfo, err error) error {
|
||||
header, err := tar.FileInfoHeader(fi, file)
|
||||
if err != nil {
|
||||
|
|
@ -54,7 +52,7 @@ func compress(fileToWrite *os.File, folderPath string, backupName string){
|
|||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
if !fi.IsDir(){
|
||||
if !fi.IsDir() {
|
||||
data, err := os.Open(file)
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
|
|
@ -77,7 +75,6 @@ func compress(fileToWrite *os.File, folderPath string, backupName string){
|
|||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
go fmt.Printf("[%s] Compression Done.\n", backupName)
|
||||
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, SQL.SQLStage_Compress, SQL.REMOTE_NONE, "Compression complete.", time.Now())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
39
Service/WindowsService.go
Normal file
39
Service/WindowsService.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package Service
|
||||
|
||||
import (
|
||||
"github.com/judwhite/go-svc"
|
||||
"log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type program struct {
|
||||
wg sync.WaitGroup
|
||||
quit chan struct{}
|
||||
}
|
||||
|
||||
//TODO: replace all the 'log' crap with an actual logger.
|
||||
|
||||
func (p *program) Init(env svc.Environment) error {
|
||||
log.Printf("is win service? %v\n", env.IsWindowsService())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *program) Start() error {
|
||||
p.quit = make(chan struct{})
|
||||
|
||||
p.wg.Add(1)
|
||||
go func() {
|
||||
//Do stuff
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *program) Stop() error {
|
||||
log.Println("Stopping...")
|
||||
close(p.quit)
|
||||
p.wg.Wait()
|
||||
log.Println("Stopped.")
|
||||
|
||||
return nil
|
||||
}
|
||||
3
go.mod
3
go.mod
|
|
@ -7,8 +7,10 @@ require github.com/sirupsen/logrus v1.8.1
|
|||
require (
|
||||
github.com/Azure/azure-storage-file-go v0.8.0
|
||||
github.com/cheggaaa/pb/v3 v3.0.8
|
||||
github.com/denisenkom/go-mssqldb v0.11.0
|
||||
github.com/go-sql-driver/mysql v1.6.0
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/judwhite/go-svc v1.2.1
|
||||
github.com/urfave/cli/v2 v2.3.0
|
||||
)
|
||||
|
||||
|
|
@ -16,7 +18,6 @@ require (
|
|||
github.com/Azure/azure-pipeline-go v0.2.1 // indirect
|
||||
github.com/VividCortex/ewma v1.1.1 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
|
||||
github.com/denisenkom/go-mssqldb v0.11.0 // indirect
|
||||
github.com/fatih/color v1.10.0 // indirect
|
||||
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
|
||||
github.com/mattn/go-colorable v0.1.8 // indirect
|
||||
|
|
|
|||
3
go.sum
3
go.sum
|
|
@ -21,6 +21,8 @@ github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZ
|
|||
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/judwhite/go-svc v1.2.1 h1:a7fsJzYUa33sfDJRF2N/WXhA+LonCEEY8BJb1tuS5tA=
|
||||
github.com/judwhite/go-svc v1.2.1/go.mod h1:mo/P2JNX8C07ywpP9YtO2gnBgnUiFTHqtsZekJrUuTk=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
|
|
@ -62,6 +64,7 @@ golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 h1:F5Gozwx4I1xtr/sr/8CFbb57iKi3297KFs0QDbGN60A=
|
||||
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
|
|
|
|||
4
main.go
4
main.go
|
|
@ -21,8 +21,8 @@ func main() {
|
|||
},
|
||||
Copyright: "(c) 2021-2022 netbenix",
|
||||
Commands: []*cli.Command{
|
||||
Commands.StartBackupProc(),
|
||||
Commands.GenerateNewConfigs(),
|
||||
Commands.StartBackupProcCommand(),
|
||||
Commands.GenerateNewConfigsCommand(),
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
|||
Reference in a new issue