From c6a2f630a1c1c7181452a7d25505ccec9e9912d0 Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Thu, 18 Jan 2024 19:22:31 +0100 Subject: [PATCH] Refactoring of code --- .gitignore | 3 ++- build.sh | 2 +- docker/Dockerfile | 2 +- main.go | 64 ++++++++++++++++++++++++++++++++++------------- 4 files changed, 51 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 18783d7..0a2bf49 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ compose.yaml .env test.md .DS_Store -pg-bkup \ No newline at end of file +pg-bkup +/.idea \ No newline at end of file diff --git a/build.sh b/build.sh index 3ffb8f1..3e7bf7f 100755 --- a/build.sh +++ b/build.sh @@ -7,7 +7,7 @@ if [ $# -eq 0 ] fi #go build -CGO_ENABLED=0 GOOS=linux go build +#CGO_ENABLED=0 GOOS=linux go build docker build -f docker/Dockerfile -t jkaninda/pg-bkup:$tag . diff --git a/docker/Dockerfile b/docker/Dockerfile index a208abc..f9793e1 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -14,7 +14,7 @@ ENV DB_HOST="" ENV DB_NAME="" ENV DB_USERNAME="" ENV DB_PASSWORD="" -ENV DB_PORT="3306" +ENV DB_PORT="5432" ENV STORAGE=local ENV BUCKETNAME="" ENV ACCESS_KEY="" diff --git a/main.go b/main.go index 3eb9cea..c735787 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ var appVersion string = os.Getenv("VERSION") const s3MountPath string = "/s3mnt" var ( - operation string = "bakup" + operation string = "backup" storage string = "local" file string = "" s3Path string = "/pg-bkup" @@ -42,7 +42,7 @@ var ( disableCompression bool = false startBackup bool = true outputContent string = "" - potimeout int = 30 + timeout int = 30 period string = "0 1 * * *" ) @@ -70,7 +70,7 @@ func init() { dbName = *dbnameFlag executionMode = *modeFlag dbPort = fmt.Sprint(*portFlag) - potimeout = *timeoutFlag + timeout = *timeoutFlag period = *periodFlag disableCompression = *disableCompressionFlag @@ -92,26 +92,44 @@ func init() { os.Exit(0) } if *dbnameFlag != "" { - os.Setenv("DB_NAME", dbName) + err := os.Setenv("DB_NAME", dbName) + if err != nil { + return + } } if *pathFlag != "" { s3Path = *pathFlag - os.Setenv("S3_PATH", fmt.Sprint(*pathFlag)) + err := os.Setenv("S3_PATH", fmt.Sprint(*pathFlag)) + if err != nil { + return + } } if *fileFlag != "" { file = *fileFlag - os.Setenv("FILE_NAME", fmt.Sprint(*fileFlag)) + err := os.Setenv("FILE_NAME", fmt.Sprint(*fileFlag)) + if err != nil { + return + } } - if *portFlag != 3306 { - os.Setenv("DB_PORT", fmt.Sprint(*portFlag)) + if *portFlag != 5432 { + err := os.Setenv("DB_PORT", fmt.Sprint(*portFlag)) + if err != nil { + return + } } if *periodFlag != "" { - os.Setenv("SCHEDULE_PERIOD", fmt.Sprint(*periodFlag)) + err := os.Setenv("SCHEDULE_PERIOD", fmt.Sprint(*periodFlag)) + if err != nil { + return + } } if *storageFlag != "" { - os.Setenv("STORAGE", fmt.Sprint(*storageFlag)) + err := os.Setenv("STORAGE", fmt.Sprint(*storageFlag)) + if err != nil { + return + } } dbHost = os.Getenv("DB_HOST") dbPassword = os.Getenv("DB_PASSWORD") @@ -133,7 +151,10 @@ func version() { fmt.Print() } func main() { - os.Setenv("STORAGE_PATH", storagePath) + err := os.Setenv("STORAGE_PATH", storagePath) + if err != nil { + return + } if startBackup { start() @@ -175,7 +196,10 @@ func backup() { utils.Info("Backing up database...") bkFileName := fmt.Sprintf("%s_%s.sql.gz", dbName, time.Now().Format("20060102_150405")) - os.Setenv("PGPASSWORD", dbPassword) + err := os.Setenv("PGPASSWORD", dbPassword) + if err != nil { + return + } if disableCompression { bkFileName = fmt.Sprintf("%s_%s.sql", dbName, time.Now().Format("20060102_150405")) cmd := exec.Command("pg_dump", @@ -246,7 +270,10 @@ func restore() { if utils.FileExists(fmt.Sprintf("%s/%s", storagePath, file)) { testDatabaseConnection() - os.Setenv("PGPASSWORD", dbPassword) + err := os.Setenv("PGPASSWORD", dbPassword) + if err != nil { + return + } extension := filepath.Ext(fmt.Sprintf("%s/%s", storagePath, file)) // GZ compressed file @@ -314,10 +341,13 @@ func s3Mount() { utils.Fatal("Please make sure all environment variables are set") } else { storagePath = fmt.Sprintf("%s%s", s3MountPath, s3Path) - os.Setenv("STORAGE_PATH", storagePath) + err := os.Setenv("STORAGE_PATH", storagePath) + if err != nil { + return + } //Write file - err := utils.WriteToFile(s3fsPasswdFile, fmt.Sprintf("%s:%s", accessKey, secretKey)) + err = utils.WriteToFile(s3fsPasswdFile, fmt.Sprintf("%s:%s", accessKey, secretKey)) if err != nil { utils.Fatal("Error creating file") } @@ -334,7 +364,7 @@ func s3Mount() { ) if err := cmd.Run(); err != nil { - utils.Fatalf("Error mounting Object storage:", err) + utils.Fatal("Error mounting Object storage:", err) } if err := os.MkdirAll(storagePath, os.ModePerm); err != nil { @@ -363,7 +393,7 @@ func createCrontabScript() { if err := touchCmd.Run(); err != nil { utils.Fatalf("Error creating file %s: %v\n", task, err) } - var disableC string = "" + var disableC = "" if disableCompression { disableC = "--disable-compression" }