Refactoring of code

This commit is contained in:
2024-01-18 19:13:04 +01:00
parent 452d77f5ee
commit abd04c0a37
6 changed files with 70 additions and 15 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/msql-bkup.iml" filepath="$PROJECT_DIR$/.idea/msql-bkup.iml" />
</modules>
</component>
</project>

9
.idea/msql-bkup.iml generated Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@@ -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/mysql-bkup:$tag .

52
main.go
View File

@@ -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 = "/mysql-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))
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()
@@ -308,10 +329,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")
}
@@ -328,7 +352,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 {
@@ -357,7 +381,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"
}