commit
83cedeac10
7 changed files with 93 additions and 49 deletions
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"scabiosa/Tools"
|
"scabiosa/Tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateNewConfigs() *cli.Command {
|
func GenerateNewConfigsCommand() *cli.Command {
|
||||||
logger := Logging.Logger("generate-configs")
|
logger := Logging.Logger("generate-configs")
|
||||||
|
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func StartBackupProc() *cli.Command {
|
func StartBackupProcCommand() *cli.Command {
|
||||||
logger := Logging.Logger("backup")
|
logger := Logging.Logger("backup")
|
||||||
|
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
|
|
@ -21,6 +21,19 @@ func StartBackupProc() *cli.Command {
|
||||||
Description: "Compresses and uploads/stores the backups",
|
Description: "Compresses and uploads/stores the backups",
|
||||||
HelpName: "backup",
|
HelpName: "backup",
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
|
StartBackupProc()
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
OnUsageError: func(cc *cli.Context, err error, isSubcommand bool) error {
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func StartBackupProc() {
|
||||||
Tools.CheckIfConfigExists()
|
Tools.CheckIfConfigExists()
|
||||||
config := Tools.GetConfig()
|
config := Tools.GetConfig()
|
||||||
|
|
||||||
|
|
@ -57,16 +70,6 @@ func StartBackupProc() *cli.Command {
|
||||||
}
|
}
|
||||||
SQL.NewBackupEntry(SQL.GetSQLInstance(), backupItem.BackupName, time.Now(), backupItem.CreateLocalBackup, backupItem.FolderPath, StorageTypes.CheckRemoteStorageType(backupItem.RemoteStorageType), backupItem.RemoteTargetPath, backupItem.LocalTargetPath)
|
SQL.NewBackupEntry(SQL.GetSQLInstance(), backupItem.BackupName, time.Now(), backupItem.CreateLocalBackup, backupItem.FolderPath, StorageTypes.CheckRemoteStorageType(backupItem.RemoteStorageType), backupItem.RemoteTargetPath, backupItem.LocalTargetPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
OnUsageError: func(cc *cli.Context, err error, isSubcommand bool) error {
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal(err)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTimeSuffix() string {
|
func getTimeSuffix() string {
|
||||||
|
|
@ -75,6 +78,7 @@ func getTimeSuffix() string {
|
||||||
return "_" + currTime.Format("02-01-2006_15-04")
|
return "_" + currTime.Format("02-01-2006_15-04")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// skipcq: RVV-A0005
|
||||||
func checkTmpPath(createLocalBackup bool, targetPath string) string {
|
func checkTmpPath(createLocalBackup bool, targetPath string) string {
|
||||||
logger := Logging.DetailedLogger("mainThread", "checkTmpPath")
|
logger := Logging.DetailedLogger("mainThread", "checkTmpPath")
|
||||||
if !createLocalBackup {
|
if !createLocalBackup {
|
||||||
|
|
|
||||||
|
|
@ -19,21 +19,18 @@ func CreateBakFile(fileName string, folderPath string, destinationPath string, b
|
||||||
|
|
||||||
pathToFile := destinationPath + string(os.PathSeparator) + fileName + ".bak"
|
pathToFile := destinationPath + string(os.PathSeparator) + fileName + ".bak"
|
||||||
|
|
||||||
|
|
||||||
fileToWrite, err := os.OpenFile(pathToFile, os.O_CREATE|os.O_RDWR, os.FileMode(0775))
|
fileToWrite, err := os.OpenFile(pathToFile, os.O_CREATE|os.O_RDWR, os.FileMode(0775))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
compress(fileToWrite, folderPath, backupName)
|
compress(fileToWrite, folderPath, backupName)
|
||||||
|
|
||||||
|
|
||||||
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, SQL.SQLStage_Compress, SQL.REMOTE_NONE, "File successfully written.", time.Now())
|
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, SQL.SQLStage_Compress, SQL.REMOTE_NONE, "File successfully written.", time.Now())
|
||||||
|
|
||||||
|
|
||||||
return pathToFile
|
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")
|
logger := Logging.DetailedLogger("Gzip", "compress")
|
||||||
|
|
||||||
zr, _ := gzip.NewWriterLevel(fileToWrite, flate.BestCompression)
|
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)
|
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())
|
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 {
|
filepath.Walk(folderPath, func(file string, fi os.FileInfo, err error) error {
|
||||||
header, err := tar.FileInfoHeader(fi, file)
|
header, err := tar.FileInfoHeader(fi, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -54,7 +52,7 @@ func compress(fileToWrite *os.File, folderPath string, backupName string){
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !fi.IsDir(){
|
if !fi.IsDir() {
|
||||||
data, err := os.Open(file)
|
data, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
|
|
@ -77,7 +75,6 @@ func compress(fileToWrite *os.File, folderPath string, backupName string){
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
go fmt.Printf("[%s] Compression Done.\n", backupName)
|
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())
|
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 (
|
require (
|
||||||
github.com/Azure/azure-storage-file-go v0.8.0
|
github.com/Azure/azure-storage-file-go v0.8.0
|
||||||
github.com/cheggaaa/pb/v3 v3.0.8
|
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/go-sql-driver/mysql v1.6.0
|
||||||
github.com/google/uuid v1.3.0
|
github.com/google/uuid v1.3.0
|
||||||
|
github.com/judwhite/go-svc v1.2.1
|
||||||
github.com/urfave/cli/v2 v2.3.0
|
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/Azure/azure-pipeline-go v0.2.1 // indirect
|
||||||
github.com/VividCortex/ewma v1.1.1 // indirect
|
github.com/VividCortex/ewma v1.1.1 // indirect
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // 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/fatih/color v1.10.0 // indirect
|
||||||
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
|
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
|
||||||
github.com/mattn/go-colorable v0.1.8 // 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/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 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
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 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
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-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-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-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 h1:F5Gozwx4I1xtr/sr/8CFbb57iKi3297KFs0QDbGN60A=
|
||||||
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
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",
|
Copyright: "(c) 2021-2022 netbenix",
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
Commands.StartBackupProc(),
|
Commands.StartBackupProcCommand(),
|
||||||
Commands.GenerateNewConfigs(),
|
Commands.GenerateNewConfigsCommand(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Reference in a new issue