2024-01-19 05:31:30 +01:00
package pkg
import (
"fmt"
"github.com/jkaninda/mysql-bkup/utils"
2024-01-20 13:04:39 +01:00
"github.com/spf13/cobra"
2024-01-19 05:31:30 +01:00
"os"
"os/exec"
"path/filepath"
)
2024-01-20 13:04:39 +01:00
func StartRestore ( cmd * cobra . Command ) {
//Set env
utils . SetEnv ( "STORAGE_PATH" , storagePath )
//Get flag value and set env
2024-08-03 16:03:17 +02:00
s3Path := utils . GetEnv ( cmd , "path" , "AWS_S3_PATH" )
remotePath := utils . GetEnv ( cmd , "path" , "SSH_REMOTE_PATH" )
2024-01-20 13:04:39 +01:00
storage = utils . GetEnv ( cmd , "storage" , "STORAGE" )
file = utils . GetEnv ( cmd , "file" , "FILE_NAME" )
executionMode , _ = cmd . Flags ( ) . GetString ( "mode" )
2024-08-03 16:03:17 +02:00
bucket := utils . GetEnvVariable ( "AWS_S3_BUCKET_NAME" , "BUCKET_NAME" )
2024-08-30 19:58:12 +02:00
dbConf = getDbConfig ( cmd )
2024-08-03 16:03:17 +02:00
switch storage {
case "s3" :
2024-08-30 19:58:12 +02:00
restoreFromS3 ( dbConf , file , bucket , s3Path )
2024-08-03 16:03:17 +02:00
case "local" :
utils . Info ( "Restore database from local" )
copyToTmp ( storagePath , file )
2024-08-30 19:58:12 +02:00
RestoreDatabase ( dbConf , file )
2024-08-03 16:03:17 +02:00
case "ssh" :
2024-08-30 19:58:12 +02:00
restoreFromRemote ( dbConf , file , remotePath )
2024-08-03 16:03:17 +02:00
case "ftp" :
utils . Fatal ( "Restore from FTP is not yet supported" )
default :
2024-01-20 13:04:39 +01:00
utils . Info ( "Restore database from local" )
2024-08-30 19:58:12 +02:00
copyToTmp ( storagePath , file )
RestoreDatabase ( dbConf , file )
2024-08-03 16:03:17 +02:00
}
}
2024-01-20 13:04:39 +01:00
2024-08-30 19:58:12 +02:00
func restoreFromS3 ( db * dbConfig , file , bucket , s3Path string ) {
2024-08-03 16:03:17 +02:00
utils . Info ( "Restore database from s3" )
err := utils . DownloadFile ( tmpPath , file , bucket , s3Path )
if err != nil {
2024-08-11 09:38:31 +02:00
utils . Fatal ( "Error download file from s3 %s %v" , file , err )
2024-01-20 13:04:39 +01:00
}
2024-08-30 19:58:12 +02:00
RestoreDatabase ( db , file )
2024-08-03 16:03:17 +02:00
}
2024-08-30 19:58:12 +02:00
func restoreFromRemote ( db * dbConfig , file , remotePath string ) {
2024-08-03 16:03:17 +02:00
utils . Info ( "Restore database from remote server" )
err := CopyFromRemote ( file , remotePath )
if err != nil {
2024-08-11 09:38:31 +02:00
utils . Fatal ( "Error download file from remote server: %s %v " , filepath . Join ( remotePath , file ) , err )
2024-08-03 16:03:17 +02:00
}
2024-08-30 19:58:12 +02:00
RestoreDatabase ( db , file )
2024-01-20 13:04:39 +01:00
}
2024-01-19 06:56:19 +01:00
// RestoreDatabase restore database
2024-08-30 19:58:12 +02:00
func RestoreDatabase ( db * dbConfig , file string ) {
2024-08-03 16:03:17 +02:00
gpgPassphrase := os . Getenv ( "GPG_PASSPHRASE" )
2024-01-20 13:04:39 +01:00
if file == "" {
2024-01-21 15:18:35 +01:00
utils . Fatal ( "Error, file required" )
2024-01-20 13:04:39 +01:00
}
2024-08-04 01:36:22 +02:00
2024-08-03 16:03:17 +02:00
err := utils . CheckEnvVars ( dbHVars )
if err != nil {
utils . Error ( "Please make sure all required environment variables for database are set" )
utils . Fatal ( "Error checking environment variables: %s" , err )
}
2024-01-19 05:31:30 +01:00
2024-08-03 16:03:17 +02:00
extension := filepath . Ext ( fmt . Sprintf ( "%s/%s" , tmpPath , file ) )
if extension == ".gpg" {
if gpgPassphrase == "" {
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." )
2024-01-19 05:31:30 +01:00
2024-08-03 16:03:17 +02:00
} else {
//Decrypt file
err := Decrypt ( filepath . Join ( tmpPath , file ) , gpgPassphrase )
if err != nil {
2024-08-04 01:36:22 +02:00
utils . Fatal ( "Error decrypting file %s %v" , file , err )
2024-08-03 16:03:17 +02:00
}
//Update file name
file = RemoveLastExtension ( file )
}
2024-01-19 05:31:30 +01:00
2024-08-03 16:03:17 +02:00
}
2024-01-19 05:31:30 +01:00
2024-08-03 16:03:17 +02:00
if utils . FileExists ( fmt . Sprintf ( "%s/%s" , tmpPath , file ) ) {
2024-08-30 19:58:12 +02:00
testDatabaseConnection ( db )
2024-08-11 09:38:31 +02:00
utils . Info ( "Restoring database..." )
2024-01-19 05:31:30 +01:00
2024-08-03 16:03:17 +02:00
extension := filepath . Ext ( fmt . Sprintf ( "%s/%s" , tmpPath , file ) )
// Restore from compressed file / .sql.gz
if extension == ".gz" {
2024-09-03 06:49:26 +02:00
str := "zcat " + fmt . Sprintf ( "%s/%s" , tmpPath , file ) + " | mysql -h " + db . dbHost + " -P " + db . dbPort + " -u " + db . dbUserName + " --password=" + db . dbPassword + " " + db . dbName
2024-08-03 16:03:17 +02:00
_ , err := exec . Command ( "bash" , "-c" , str ) . Output ( )
if err != nil {
2024-08-04 01:36:22 +02:00
utils . Fatal ( "Error, in restoring the database %v" , err )
2024-01-19 05:31:30 +01:00
}
2024-08-11 09:38:31 +02:00
utils . Info ( "Restoring database... done" )
2024-08-03 16:03:17 +02:00
utils . Done ( "Database has been restored" )
2024-08-11 09:38:31 +02:00
//Delete temp
deleteTemp ( )
2024-01-19 05:31:30 +01:00
2024-08-03 16:03:17 +02:00
} else if extension == ".sql" {
//Restore from sql file
2024-09-03 06:49:26 +02:00
str := "cat " + fmt . Sprintf ( "%s/%s" , tmpPath , file ) + " | mysql -h " + db . dbHost + " -P " + db . dbPort + " -u " + db . dbUserName + " --password=" + db . dbPassword + " " + db . dbName
2024-08-03 16:03:17 +02:00
_ , err := exec . Command ( "bash" , "-c" , str ) . Output ( )
if err != nil {
utils . Fatal ( fmt . Sprintf ( "Error in restoring the database %s" , err ) )
}
2024-08-11 09:38:31 +02:00
utils . Info ( "Restoring database... done" )
2024-08-03 16:03:17 +02:00
utils . Done ( "Database has been restored" )
2024-08-11 09:38:31 +02:00
//Delete temp
deleteTemp ( )
2024-01-19 05:31:30 +01:00
} else {
2024-08-03 16:03:17 +02:00
utils . Fatal ( fmt . Sprintf ( "Unknown file extension %s" , extension ) )
2024-01-19 05:31:30 +01:00
}
2024-08-03 16:03:17 +02:00
} else {
utils . Fatal ( fmt . Sprintf ( "File not found in %s" , fmt . Sprintf ( "%s/%s" , tmpPath , file ) ) )
2024-01-19 05:31:30 +01:00
}
}