Fix restore from ssh, refactoring of code

This commit is contained in:
2024-08-01 07:14:40 +02:00
parent 5b0d450740
commit 4208a16223
5 changed files with 15 additions and 14 deletions

View File

@@ -52,7 +52,7 @@ func StartBackup(cmd *cobra.Command) {
s3Backup(backupFileName, s3Path, disableCompression, prune, backupRetention, encryption)
case "local":
localBackup(backupFileName, disableCompression, prune, backupRetention, encryption)
case "ssh":
case "ssh", "remote":
sshBackup(backupFileName, remotePath, disableCompression, prune, backupRetention, encryption)
case "ftp":
utils.Fatalf("Not supported storage type: %s", storage)
@@ -220,7 +220,7 @@ func s3Backup(backupFileName string, s3Path string, disableCompression bool, pru
finalFileName = fmt.Sprintf("%s.%s", backupFileName, "gpg")
}
utils.Info("Uploading backup file to S3 storage...")
utils.Info("Backup name is ", backupFileName)
utils.Info("Backup name is ", finalFileName)
err := utils.UploadFileToS3(tmpPath, finalFileName, bucket, s3Path)
if err != nil {
utils.Fatalf("Error uploading file to S3: %s ", err)
@@ -255,9 +255,10 @@ func sshBackup(backupFileName, remotePath string, disableCompression bool, prune
utils.Info("Backup name is ", backupFileName)
err := CopyToRemote(finalFileName, remotePath)
if err != nil {
utils.Fatalf("Error uploading file to S3: %s ", err)
utils.Fatalf("Error uploading file to the remote server: %s ", err)
}
//Delete backup file from tmp folder
err = utils.DeleteFile(filepath.Join(tmpPath, finalFileName))
if err != nil {
@@ -275,7 +276,6 @@ func sshBackup(backupFileName, remotePath string, disableCompression bool, prune
func encryptBackup(backupFileName string) {
gpgPassphrase := os.Getenv("GPG_PASSPHRASE")
err := Encrypt(filepath.Join(tmpPath, backupFileName), gpgPassphrase)
if err != nil {
utils.Fatalf("Error during encrypting backup %s", err)

View File

@@ -71,7 +71,8 @@ func RestoreDatabase(file string) {
extension := filepath.Ext(fmt.Sprintf("%s/%s", tmpPath, file))
if extension == ".gpg" {
if gpgPassphrase == "" {
utils.Fatal("Error, GPG_PASSPHRASE environment variable required, you need to set the GPG_PASSPHRASE")
utils.Fatal("Error: GPG passphrase is required, your file seems to be a GPG file.\nYou need to provide GPG keys. GPG_PASSPHRASE environment variable is required.")
} else {
//Decrypt file
err := Decrypt(filepath.Join(tmpPath, file), gpgPassphrase)

View File

@@ -27,6 +27,7 @@ func CopyToRemote(fileName, remotePath string) error {
if sshPassword == "" {
return errors.New("SSH_PASSWORD environment variable is required if SSH_IDENTIFY_FILE is empty\n")
}
utils.Info("Accessing the remote server using password, private key is recommended\n")
clientConfig, _ = auth.PasswordKey(sshUser, sshPassword, ssh.InsecureIgnoreHostKey())
}
@@ -70,6 +71,7 @@ func CopyFromRemote(fileName, remotePath string) error {
if sshPassword == "" {
return errors.New("SSH_PASSWORD environment variable is required if SSH_IDENTIFY_FILE is empty\n")
}
utils.Info("Accessing the remote server using password, private key is recommended\n")
clientConfig, _ = auth.PasswordKey(sshUser, sshPassword, ssh.InsecureIgnoreHostKey())
}
@@ -83,14 +85,14 @@ func CopyFromRemote(fileName, remotePath string) error {
}
// Close client connection after the file has been copied
defer client.Close()
file, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0777)
file, err := os.OpenFile(filepath.Join(tmpPath, fileName), os.O_RDWR|os.O_CREATE, 0777)
if err != nil {
fmt.Println("Couldn't open the output file")
}
defer file.Close()
// the context can be adjusted to provide time-outs or inherit from other contexts if this is embedded in a larger application.
err = client.CopyFromRemote(context.Background(), file, remotePath)
err = client.CopyFromRemote(context.Background(), file, filepath.Join(remotePath, fileName))
if err != nil {
fmt.Println("Error while copying file ", err)