2024-09-10 22:59:28 +02:00
|
|
|
// Package pkg /
|
|
|
|
|
/*****
|
|
|
|
|
@author Jonas Kaninda
|
|
|
|
|
@license MIT License <https://opensource.org/licenses/MIT>
|
|
|
|
|
@Copyright © 2024 Jonas Kaninda
|
|
|
|
|
**/
|
2024-08-29 21:49:35 +02:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/jkaninda/pg-bkup/utils"
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func StartMigration(cmd *cobra.Command) {
|
2024-09-11 04:33:41 +02:00
|
|
|
intro()
|
2024-08-29 21:49:35 +02:00
|
|
|
utils.Info("Starting database migration...")
|
|
|
|
|
//Get DB config
|
2024-09-28 09:20:35 +02:00
|
|
|
dbConf = initDbConfig(cmd)
|
|
|
|
|
targetDbConf = initTargetDbConfig()
|
2024-08-29 21:49:35 +02:00
|
|
|
|
2024-09-03 06:09:56 +02:00
|
|
|
//Defining the target database variables
|
2024-08-29 21:49:35 +02:00
|
|
|
newDbConfig := dbConfig{}
|
2024-09-03 06:09:56 +02:00
|
|
|
newDbConfig.dbHost = targetDbConf.targetDbHost
|
|
|
|
|
newDbConfig.dbPort = targetDbConf.targetDbPort
|
|
|
|
|
newDbConfig.dbName = targetDbConf.targetDbName
|
|
|
|
|
newDbConfig.dbUserName = targetDbConf.targetDbUserName
|
|
|
|
|
newDbConfig.dbPassword = targetDbConf.targetDbPassword
|
|
|
|
|
|
|
|
|
|
//Generate file name
|
|
|
|
|
backupFileName := fmt.Sprintf("%s_%s.sql", dbConf.dbName, time.Now().Format("20060102_150405"))
|
2024-10-08 11:04:46 +02:00
|
|
|
conf := &RestoreConfig{}
|
|
|
|
|
conf.file = backupFileName
|
2024-09-03 06:09:56 +02:00
|
|
|
//Backup source Database
|
|
|
|
|
BackupDatabase(dbConf, backupFileName, true)
|
2024-08-29 21:49:35 +02:00
|
|
|
//Restore source database into target database
|
2024-09-03 06:09:56 +02:00
|
|
|
utils.Info("Restoring [%s] database into [%s] database...", dbConf.dbName, targetDbConf.targetDbName)
|
2024-10-08 11:04:46 +02:00
|
|
|
RestoreDatabase(&newDbConfig, conf)
|
2024-09-03 06:09:56 +02:00
|
|
|
utils.Info("[%s] database has been restored into [%s] database", dbConf.dbName, targetDbConf.targetDbName)
|
|
|
|
|
utils.Info("Database migration completed.")
|
2024-08-29 21:49:35 +02:00
|
|
|
}
|