diff --git a/go.mod b/go.mod index 987da5f..e1fea84 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.23.2 require ( github.com/go-mail/mail v2.3.1+incompatible github.com/jkaninda/encryptor v0.0.0-20241013064832-ed4bd6a1b221 - github.com/jkaninda/go-storage v0.1.2 + github.com/jkaninda/go-storage v0.1.3 github.com/robfig/cron/v3 v3.0.1 github.com/spf13/cobra v1.8.1 gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index 3439997..274648a 100644 --- a/go.sum +++ b/go.sum @@ -46,6 +46,8 @@ github.com/jkaninda/encryptor v0.0.0-20241013064832-ed4bd6a1b221 h1:AwkCf7el1kze github.com/jkaninda/encryptor v0.0.0-20241013064832-ed4bd6a1b221/go.mod h1:9F8ZJ+ZXE8DZBo77+aneGj8LMjrYXX6eFUCC/uqZOUo= github.com/jkaninda/go-storage v0.1.2 h1:d7+TRPjmHXdSqO0wne3KAB8zt9ih8lf5D8aL4n7/Dds= github.com/jkaninda/go-storage v0.1.2/go.mod h1:zVRnLprBk/9AUz2+za6Y03MgoNYrqKLy3edVtjqMaps= +github.com/jkaninda/go-storage v0.1.3 h1:lEpHVgFLKSvjsi/6tAek96Y07za3vxmsXF2/+jiCMZU= +github.com/jkaninda/go-storage v0.1.3/go.mod h1:zVRnLprBk/9AUz2+za6Y03MgoNYrqKLy3edVtjqMaps= github.com/jlaffaye/ftp v0.2.0 h1:lXNvW7cBu7R/68bknOX3MrRIIqZ61zELs1P2RAiA3lg= github.com/jlaffaye/ftp v0.2.0/go.mod h1:is2Ds5qkhceAPy2xD6RLI6hmp/qysSoymZ+Z2uTnspI= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= diff --git a/pkg/config.go b/pkg/config.go index 37ac2c4..ba63be4 100644 --- a/pkg/config.go +++ b/pkg/config.go @@ -80,7 +80,7 @@ type FTPConfig struct { host string user string password string - port string + port int remotePath string } type AzureConfig struct { @@ -94,7 +94,7 @@ type SSHConfig struct { user string password string hostName string - port string + port int identifyFile string } type AWSConfig struct { @@ -149,7 +149,7 @@ func loadSSHConfig() (*SSHConfig, error) { user: os.Getenv("SSH_USER"), password: os.Getenv("SSH_PASSWORD"), hostName: os.Getenv("SSH_HOST"), - port: os.Getenv("SSH_PORT"), + port: utils.GetIntEnv("SSH_PORT"), identifyFile: os.Getenv("SSH_IDENTIFY_FILE"), }, nil } @@ -159,7 +159,7 @@ func loadFtpConfig() *FTPConfig { fConfig.host = utils.GetEnvVariable("FTP_HOST", "FTP_HOST_NAME") fConfig.user = os.Getenv("FTP_USER") fConfig.password = os.Getenv("FTP_PASSWORD") - fConfig.port = os.Getenv("FTP_PORT") + fConfig.port = utils.GetIntEnv("FTP_PORT") fConfig.remotePath = os.Getenv("REMOTE_PATH") err := utils.CheckEnvVars(ftpVars) if err != nil { diff --git a/pkg/remote.go b/pkg/remote.go index 79450d0..a70be3e 100644 --- a/pkg/remote.go +++ b/pkg/remote.go @@ -52,12 +52,13 @@ func sshBackup(db *dbConfig, config *BackupConfig) { } sshStorage, err := ssh.NewStorage(ssh.Config{ - Host: sshConfig.hostName, - Port: sshConfig.port, - User: sshConfig.user, - Password: sshConfig.password, - RemotePath: config.remotePath, - LocalPath: tmpPath, + Host: sshConfig.hostName, + Port: sshConfig.port, + User: sshConfig.user, + Password: sshConfig.password, + IdentifyFile: sshConfig.identifyFile, + RemotePath: config.remotePath, + LocalPath: tmpPath, }) if err != nil { utils.Fatal("Error creating SSH storage: %s", err) @@ -103,6 +104,51 @@ func sshBackup(db *dbConfig, config *BackupConfig) { utils.Info("Backup completed successfully") } +func remoteRestore(db *dbConfig, conf *RestoreConfig) { + utils.Info("Restore database from remote server") + sshConfig, err := loadSSHConfig() + if err != nil { + utils.Fatal("Error loading ssh config: %s", err) + } + + sshStorage, err := ssh.NewStorage(ssh.Config{ + Host: sshConfig.hostName, + Port: sshConfig.port, + User: sshConfig.user, + Password: sshConfig.password, + IdentifyFile: sshConfig.identifyFile, + RemotePath: conf.remotePath, + LocalPath: tmpPath, + }) + if err != nil { + utils.Fatal("Error creating SSH storage: %s", err) + } + err = sshStorage.CopyFrom(conf.file) + if err != nil { + utils.Fatal("Error copying backup file: %s", err) + } + RestoreDatabase(db, conf) +} +func ftpRestore(db *dbConfig, conf *RestoreConfig) { + utils.Info("Restore database from FTP server") + ftpConfig := loadFtpConfig() + ftpStorage, err := ftp.NewStorage(ftp.Config{ + Host: ftpConfig.host, + Port: ftpConfig.port, + User: ftpConfig.user, + Password: ftpConfig.password, + RemotePath: conf.remotePath, + LocalPath: tmpPath, + }) + if err != nil { + utils.Fatal("Error creating SSH storage: %s", err) + } + err = ftpStorage.CopyFrom(conf.file) + if err != nil { + utils.Fatal("Error copying backup file: %s", err) + } + RestoreDatabase(db, conf) +} func ftpBackup(db *dbConfig, config *BackupConfig) { utils.Info("Backup database to the remote FTP server") startTime = time.Now().Format(utils.TimeFormat()) @@ -169,48 +215,3 @@ func ftpBackup(db *dbConfig, config *BackupConfig) { deleteTemp() utils.Info("Backup completed successfully") } -func remoteRestore(db *dbConfig, conf *RestoreConfig) { - utils.Info("Restore database from remote server") - sshConfig, err := loadSSHConfig() - if err != nil { - utils.Fatal("Error loading ssh config: %s", err) - } - - sshStorage, err := ssh.NewStorage(ssh.Config{ - Host: sshConfig.hostName, - Port: sshConfig.port, - User: sshConfig.user, - Password: sshConfig.password, - IdentifyFile: sshConfig.identifyFile, - RemotePath: conf.remotePath, - LocalPath: tmpPath, - }) - if err != nil { - utils.Fatal("Error creating SSH storage: %s", err) - } - err = sshStorage.CopyFrom(conf.file) - if err != nil { - utils.Fatal("Error copying backup file: %s", err) - } - RestoreDatabase(db, conf) -} -func ftpRestore(db *dbConfig, conf *RestoreConfig) { - utils.Info("Restore database from FTP server") - ftpConfig := loadFtpConfig() - ftpStorage, err := ftp.NewStorage(ftp.Config{ - Host: ftpConfig.host, - Port: ftpConfig.port, - User: ftpConfig.user, - Password: ftpConfig.password, - RemotePath: conf.remotePath, - LocalPath: tmpPath, - }) - if err != nil { - utils.Fatal("Error creating SSH storage: %s", err) - } - err = ftpStorage.CopyFrom(conf.file) - if err != nil { - utils.Fatal("Error copying backup file: %s", err) - } - RestoreDatabase(db, conf) -}