Skip to content

Commit 96927cd

Browse files
authored
Merge pull request #106 from jkaninda/refactor
Refactor
2 parents d186071 + ceacfa1 commit 96927cd

File tree

10 files changed

+182
-139
lines changed

10 files changed

+182
-139
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ jobs:
2727
push: true
2828
file: "./docker/Dockerfile"
2929
platforms: linux/amd64,linux/arm64,linux/arm/v7
30+
build-args: |
31+
appVersion=develop-${{ github.sha }}
3032
tags: |
31-
"${{env.BUILDKIT_IMAGE}}:develop-${{ github.sha }}"
33+
"${{vars.BUILDKIT_IMAGE}}:develop-${{ github.sha }}"
3234

.github/workflows/release.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ jobs:
4141
push: true
4242
file: "./docker/Dockerfile"
4343
platforms: linux/amd64,linux/arm64,linux/arm/v7
44+
build-args: |
45+
appVersion=${{ env.TAG_NAME }}
4446
tags: |
45-
"${{env.BUILDKIT_IMAGE}}:${{ env.TAG_NAME }}"
46-
"${{env.BUILDKIT_IMAGE}}:latest"
47-
"ghcr.io/${{env.BUILDKIT_IMAGE}}:${{ env.TAG_NAME }}"
48-
"ghcr.io/${{env.BUILDKIT_IMAGE}}:latest"
47+
"${{vars.BUILDKIT_IMAGE}}:${{ env.TAG_NAME }}"
48+
"${{vars.BUILDKIT_IMAGE}}:latest"
49+
"ghcr.io/${{vars.BUILDKIT_IMAGE}}:${{ env.TAG_NAME }}"
50+
"ghcr.io/${{vars.BUILDKIT_IMAGE}}:latest"
4951

cmd/backup.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ func init() {
2929
//Backup
3030
BackupCmd.PersistentFlags().StringP("storage", "s", "local", "Storage. local or s3")
3131
BackupCmd.PersistentFlags().StringP("path", "P", "", "AWS S3 path without file name. eg: /custom_path or ssh remote path `/home/foo/backup`")
32-
BackupCmd.PersistentFlags().StringP("mode", "m", "default", "Execution mode. | Deprecated")
33-
BackupCmd.PersistentFlags().StringP("period", "", "", "Schedule period time | Deprecated")
3432
BackupCmd.PersistentFlags().StringP("cron-expression", "", "", "Backup cron expression")
3533
BackupCmd.PersistentFlags().BoolP("prune", "", false, "Delete old backup, default disabled")
3634
BackupCmd.PersistentFlags().IntP("keep-last", "", 7, "Delete files created more than specified days ago, default 7 days")

docker/Dockerfile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ ENV AWS_SECRET_KEY=""
2323
ENV AWS_S3_PATH=""
2424
ENV AWS_REGION="us-west-2"
2525
ENV AWS_DISABLE_SSL="false"
26+
ENV AWS_FORCE_PATH_STYLE="true"
2627
ENV GPG_PASSPHRASE=""
2728
ENV SSH_USER=""
2829
ENV SSH_PASSWORD=""
29-
ENV SSH_HOST_NAME=""
30+
ENV SSH_HOST=""
3031
ENV SSH_IDENTIFY_FILE=""
3132
ENV SSH_PORT=22
3233
ENV REMOTE_PATH=""
33-
ENV FTP_HOST_NAME=""
34+
ENV FTP_HOST=""
3435
ENV FTP_PORT=21
3536
ENV FTP_USER=""
3637
ENV FTP_PASSWORD=""
@@ -39,15 +40,17 @@ ENV TARGET_DB_PORT=3306
3940
ENV TARGET_DB_NAME=""
4041
ENV TARGET_DB_USERNAME=""
4142
ENV TARGET_DB_PASSWORD=""
42-
ENV VERSION="v1.2.12"
4343
ENV BACKUP_CRON_EXPRESSION=""
4444
ENV TG_TOKEN=""
4545
ENV TG_CHAT_ID=""
4646
ENV TZ=UTC
4747
ARG WORKDIR="/config"
4848
ARG BACKUPDIR="/backup"
4949
ARG BACKUP_TMP_DIR="/tmp/backup"
50+
ARG appVersion="v1.2.12"
51+
ENV VERSION=${appVersion}
5052
LABEL author="Jonas Kaninda"
53+
LABEL version=${appVersion}
5154

5255
RUN apk --update add --no-cache mysql-client mariadb-connector-c gnupg tzdata
5356
RUN mkdir $WORKDIR

docs/how-tos/backup-to-ftp.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ As described for SSH backup section, to change the storage of your backup and us
1111
You need to add the full remote path by adding `--path /home/jkaninda/backups` flag or using `REMOTE_PATH` environment variable.
1212

1313
{: .note }
14-
These environment variables are required for SSH backup `FTP_HOST_NAME`, `FTP_USER`, `REMOTE_PATH`, `FTP_PORT` or `FTP_PASSWORD`.
14+
These environment variables are required for SSH backup `FTP_HOST`, `FTP_USER`, `REMOTE_PATH`, `FTP_PORT` or `FTP_PASSWORD`.
1515

1616
```yml
1717
services:
@@ -30,7 +30,7 @@ services:
3030
- DB_USERNAME=username
3131
- DB_PASSWORD=password
3232
## FTP config
33-
- FTP_HOST_NAME="hostname"
33+
- FTP_HOST="hostname"
3434
- FTP_PORT=21
3535
- FTP_USER=user
3636
- FTP_PASSWORD=password

docs/how-tos/backup-to-ssh.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ As described for s3 backup section, to change the storage of your backup and use
1111
You need to add the full remote path by adding `--path /home/jkaninda/backups` flag or using `REMOTE_PATH` environment variable.
1212

1313
{: .note }
14-
These environment variables are required for SSH backup `SSH_HOST_NAME`, `SSH_USER`, `SSH_REMOTE_PATH`, `SSH_IDENTIFY_FILE`, `SSH_PORT` or `SSH_PASSWORD` if you dont use a private key to access to your server.
14+
These environment variables are required for SSH backup `SSH_HOST`, `SSH_USER`, `SSH_REMOTE_PATH`, `SSH_IDENTIFY_FILE`, `SSH_PORT` or `SSH_PASSWORD` if you dont use a private key to access to your server.
1515
Accessing the remote server using password is not recommended, use private key instead.
1616

1717
```yml
@@ -33,7 +33,7 @@ services:
3333
- DB_USERNAME=username
3434
- DB_PASSWORD=password
3535
## SSH config
36-
- SSH_HOST_NAME="hostname"
36+
- SSH_HOST="hostname"
3737
- SSH_PORT=22
3838
- SSH_USER=user
3939
- REMOTE_PATH=/home/jkaninda/backups
@@ -73,7 +73,7 @@ services:
7373
- DB_USERNAME=username
7474
- DB_PASSWORD=password
7575
## SSH config
76-
- SSH_HOST_NAME="hostname"
76+
- SSH_HOST="hostname"
7777
- SSH_PORT=22
7878
- SSH_USER=user
7979
- REMOTE_PATH=/home/jkaninda/backups
@@ -125,7 +125,7 @@ spec:
125125
# Please use secret!
126126
- name: DB_PASSWORD
127127
value: ""
128-
- name: SSH_HOST_NAME
128+
- name: SSH_HOST
129129
value: ""
130130
- name: SSH_PORT
131131
value: "22"

pkg/config.go

Lines changed: 95 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
package pkg
88

99
import (
10+
"fmt"
1011
"github.com/jkaninda/mysql-bkup/utils"
1112
"github.com/spf13/cobra"
1213
"os"
14+
"strconv"
1315
)
1416

1517
type Config struct {
@@ -29,7 +31,10 @@ type targetDbConfig struct {
2931
targetDbPassword string
3032
targetDbName string
3133
}
32-
34+
type TgConfig struct {
35+
Token string
36+
ChatId string
37+
}
3338
type BackupConfig struct {
3439
backupFileName string
3540
backupRetention int
@@ -41,14 +46,6 @@ type BackupConfig struct {
4146
storage string
4247
cronExpression string
4348
}
44-
type RestoreConfig struct {
45-
s3Path string
46-
remotePath string
47-
storage string
48-
file string
49-
bucket string
50-
gpqPassphrase string
51-
}
5249
type FTPConfig struct {
5350
host string
5451
user string
@@ -57,6 +54,24 @@ type FTPConfig struct {
5754
remotePath string
5855
}
5956

57+
// SSHConfig holds the SSH connection details
58+
type SSHConfig struct {
59+
user string
60+
password string
61+
hostName string
62+
port string
63+
identifyFile string
64+
}
65+
type AWSConfig struct {
66+
endpoint string
67+
bucket string
68+
accessKey string
69+
secretKey string
70+
region string
71+
disableSsl bool
72+
forcePathStyle bool
73+
}
74+
6075
func initDbConfig(cmd *cobra.Command) *dbConfig {
6176
//Set env
6277
utils.GetEnv(cmd, "dbname", "DB_NAME")
@@ -74,14 +89,71 @@ func initDbConfig(cmd *cobra.Command) *dbConfig {
7489
}
7590
return &dConf
7691
}
92+
93+
// loadSSHConfig loads the SSH configuration from environment variables
94+
func loadSSHConfig() (*SSHConfig, error) {
95+
utils.GetEnvVariable("SSH_HOST", "SSH_HOST_NAME")
96+
sshVars := []string{"SSH_USER", "SSH_HOST", "SSH_PORT", "REMOTE_PATH"}
97+
err := utils.CheckEnvVars(sshVars)
98+
if err != nil {
99+
return nil, fmt.Errorf("error missing environment variables: %w", err)
100+
}
101+
102+
return &SSHConfig{
103+
user: os.Getenv("SSH_USER"),
104+
password: os.Getenv("SSH_PASSWORD"),
105+
hostName: os.Getenv("SSH_HOST"),
106+
port: os.Getenv("SSH_PORT"),
107+
identifyFile: os.Getenv("SSH_IDENTIFY_FILE"),
108+
}, nil
109+
}
110+
func initFtpConfig() *FTPConfig {
111+
//Initialize data configs
112+
fConfig := FTPConfig{}
113+
fConfig.host = utils.GetEnvVariable("FTP_HOST", "FTP_HOST_NAME")
114+
fConfig.user = os.Getenv("FTP_USER")
115+
fConfig.password = os.Getenv("FTP_PASSWORD")
116+
fConfig.port = os.Getenv("FTP_PORT")
117+
fConfig.remotePath = os.Getenv("REMOTE_PATH")
118+
err := utils.CheckEnvVars(ftpVars)
119+
if err != nil {
120+
utils.Error("Please make sure all required environment variables for FTP are set")
121+
utils.Fatal("Error missing environment variables: %s", err)
122+
}
123+
return &fConfig
124+
}
125+
func initAWSConfig() *AWSConfig {
126+
//Initialize AWS configs
127+
aConfig := AWSConfig{}
128+
aConfig.endpoint = utils.GetEnvVariable("AWS_S3_ENDPOINT", "S3_ENDPOINT")
129+
aConfig.accessKey = utils.GetEnvVariable("AWS_ACCESS_KEY", "ACCESS_KEY")
130+
aConfig.secretKey = utils.GetEnvVariable("AWS_SECRET_KEY", "SECRET_KEY")
131+
aConfig.bucket = utils.GetEnvVariable("AWS_S3_BUCKET_NAME", "BUCKET_NAME")
132+
aConfig.region = os.Getenv("AWS_REGION")
133+
disableSsl, err := strconv.ParseBool(os.Getenv("AWS_DISABLE_SSL"))
134+
if err != nil {
135+
utils.Fatal("Unable to parse AWS_DISABLE_SSL env var: %s", err)
136+
}
137+
forcePathStyle, err := strconv.ParseBool(os.Getenv("AWS_FORCE_PATH_STYLE"))
138+
if err != nil {
139+
utils.Fatal("Unable to parse AWS_FORCE_PATH_STYLE env var: %s", err)
140+
}
141+
aConfig.disableSsl = disableSsl
142+
aConfig.forcePathStyle = forcePathStyle
143+
err = utils.CheckEnvVars(awsVars)
144+
if err != nil {
145+
utils.Error("Please make sure all required environment variables for AWS S3 are set")
146+
utils.Fatal("Error checking environment variables: %s", err)
147+
}
148+
return &aConfig
149+
}
77150
func initBackupConfig(cmd *cobra.Command) *BackupConfig {
78151
utils.SetEnv("STORAGE_PATH", storagePath)
79152
utils.GetEnv(cmd, "cron-expression", "BACKUP_CRON_EXPRESSION")
80153
utils.GetEnv(cmd, "period", "BACKUP_CRON_EXPRESSION")
81154
utils.GetEnv(cmd, "path", "REMOTE_PATH")
82-
remotePath := utils.GetEnvVariable("REMOTE_PATH", "SSH_REMOTE_PATH")
83-
84155
//Get flag value and set env
156+
remotePath := utils.GetEnvVariable("REMOTE_PATH", "SSH_REMOTE_PATH")
85157
storage = utils.GetEnv(cmd, "storage", "STORAGE")
86158
backupRetention, _ := cmd.Flags().GetInt("keep-last")
87159
prune, _ := cmd.Flags().GetBool("prune")
@@ -94,6 +166,7 @@ func initBackupConfig(cmd *cobra.Command) *BackupConfig {
94166
if passphrase != "" {
95167
encryption = true
96168
}
169+
97170
//Initialize backup configs
98171
config := BackupConfig{}
99172
config.backupRetention = backupRetention
@@ -106,16 +179,25 @@ func initBackupConfig(cmd *cobra.Command) *BackupConfig {
106179
config.cronExpression = cronExpression
107180
return &config
108181
}
182+
183+
type RestoreConfig struct {
184+
s3Path string
185+
remotePath string
186+
storage string
187+
file string
188+
bucket string
189+
gpqPassphrase string
190+
}
191+
109192
func initRestoreConfig(cmd *cobra.Command) *RestoreConfig {
110193
utils.SetEnv("STORAGE_PATH", storagePath)
111194
utils.GetEnv(cmd, "path", "REMOTE_PATH")
112-
remotePath := utils.GetEnvVariable("REMOTE_PATH", "SSH_REMOTE_PATH")
113195

114196
//Get flag value and set env
115197
s3Path := utils.GetEnv(cmd, "path", "AWS_S3_PATH")
198+
remotePath := utils.GetEnvVariable("REMOTE_PATH", "SSH_REMOTE_PATH")
116199
storage = utils.GetEnv(cmd, "storage", "STORAGE")
117200
file = utils.GetEnv(cmd, "file", "FILE_NAME")
118-
_, _ = cmd.Flags().GetString("mode")
119201
bucket := utils.GetEnvVariable("AWS_S3_BUCKET_NAME", "BUCKET_NAME")
120202
gpqPassphrase := os.Getenv("GPG_PASSPHRASE")
121203
//Initialize restore configs
@@ -144,18 +226,3 @@ func initTargetDbConfig() *targetDbConfig {
144226
}
145227
return &tdbConfig
146228
}
147-
func initFtpConfig() *FTPConfig {
148-
//Initialize backup configs
149-
fConfig := FTPConfig{}
150-
fConfig.host = os.Getenv("FTP_HOST_NAME")
151-
fConfig.user = os.Getenv("FTP_USER")
152-
fConfig.password = os.Getenv("FTP_PASSWORD")
153-
fConfig.port = os.Getenv("FTP_PORT")
154-
fConfig.remotePath = os.Getenv("REMOTE_PATH")
155-
err := utils.CheckEnvVars(ftpVars)
156-
if err != nil {
157-
utils.Error("Please make sure all required environment variables for FTP are set")
158-
utils.Fatal("Error checking environment variables: %s", err)
159-
}
160-
return &fConfig
161-
}

0 commit comments

Comments
 (0)