Finalisation du backup sql et telechargement
This commit is contained in:
parent
22cffe21b4
commit
4d6e744c7c
156
main.go
156
main.go
@ -5,6 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"canguidev.fr/pvnet/function"
|
"canguidev.fr/pvnet/function"
|
||||||
@ -14,43 +16,44 @@ import (
|
|||||||
"github.com/pkg/sftp"
|
"github.com/pkg/sftp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main(){
|
func main() {
|
||||||
//var path string="/Users/cangui/.ssh/julien_pvnet"
|
//var path string="/Users/cangui/.ssh/julien_pvnet"
|
||||||
///Users/cangui/Documents/Dev peso/Golang dev/test.xlsx
|
///Users/cangui/Documents/Dev peso/Golang dev/test.xlsx
|
||||||
err := godotenv.Load()
|
err := godotenv.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error loading .env file")
|
log.Fatal("Error loading .env file")
|
||||||
}
|
}
|
||||||
|
|
||||||
var pathSsh string = os.Getenv("PATH_SSH")
|
var pathSsh string = os.Getenv("PATH_SSH")
|
||||||
var pathConfigSsh = os.Getenv("PATH_SSH_CONF")
|
var pathConfigSsh = os.Getenv("PATH_SSH_CONF")
|
||||||
var pathExcel string= os.Getenv("PATH_FOLDER")
|
var pathExcel string = os.Getenv("PATH_FOLDER")
|
||||||
var mysqlUser string
|
var mysqlUser string
|
||||||
var mysqlPswd string
|
var mysqlPswd string
|
||||||
var pathSaveSql string
|
var pathSaveSql string
|
||||||
|
var nameBase string
|
||||||
|
|
||||||
// Chargement de la liste des hosts du fichier config ssh
|
// Chargement de la liste des hosts du fichier config ssh
|
||||||
d:=simplyssh.GetHost(pathConfigSsh)
|
d := simplyssh.GetHost(pathConfigSsh)
|
||||||
var s []string
|
var s []string
|
||||||
for _, host := range d {
|
for _, host := range d {
|
||||||
fmt.Println(host)
|
fmt.Println(host)
|
||||||
s=append(s,string(host.Id)+" "+host.Host)
|
s = append(s, string(host.Id)+" "+host.Host)
|
||||||
}
|
}
|
||||||
// selectionne du host
|
// selectionne du host
|
||||||
choice, index, err := gochoice.Pick(
|
choice, index, err := gochoice.Pick(
|
||||||
"What do you want to do?\nPick:",
|
"What do you want to do?\nPick:",
|
||||||
s,
|
s,
|
||||||
)
|
)
|
||||||
selectHost:=choice
|
selectHost := choice
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("You didn't select anything!",err)
|
fmt.Println("You didn't select anything!", err)
|
||||||
} else {
|
} else {
|
||||||
for _, host := range d {
|
for _, host := range d {
|
||||||
// for sur les hosts et break sur le host selectionner
|
// for sur les hosts et break sur le host selectionner
|
||||||
if(host.Id == uint(index)){
|
if host.Id == uint(index) {
|
||||||
fmt.Println("debut de connection")
|
fmt.Println("debut de connection")
|
||||||
// connection en ssh avec la clée et les informations du fichier config
|
// connection en ssh avec la clée et les informations du fichier config
|
||||||
client:=simplyssh.Connect(pathSsh,host.HostName,host.Port,host.User)
|
client := simplyssh.Connect(pathSsh, host.HostName, host.Port, host.User)
|
||||||
command := "mkdir backup"
|
command := "mkdir backup"
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
@ -66,101 +69,88 @@ func main(){
|
|||||||
//exceution de la commande
|
//exceution de la commande
|
||||||
if err := session.Run(command); err != nil {
|
if err := session.Run(command); err != nil {
|
||||||
fmt.Println("Dossier existe dejà")
|
fmt.Println("Dossier existe dejà")
|
||||||
// Fermez donc cette session
|
// Fermez donc cette session
|
||||||
session.Close()
|
session.Close()
|
||||||
|
|
||||||
// Créez une nouvelle session pour exécuter le backup
|
|
||||||
sessionBackup, err := client.NewSession()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("Failed to create session for backup: ", err)
|
|
||||||
}
|
|
||||||
defer sessionBackup.Close()
|
|
||||||
|
|
||||||
choice, index, err := gochoice.Pick(
|
|
||||||
"What do you want to do?\nPick:",
|
|
||||||
[]string{
|
|
||||||
"Backup sql and download",
|
|
||||||
"Backup site",
|
|
||||||
"Check serveur apache",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("You didn't select anything!")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("Enter path download key: ")
|
|
||||||
fmt.Printf("You have selected: '%s', which is the index %d\n", choice, index)
|
|
||||||
|
|
||||||
if (index == 0) {
|
|
||||||
|
|
||||||
//var pathSaveSite string
|
|
||||||
orderedKeys := []string{"host","mysql_login", "mysql_pasw", "path_save_sql", "path_save_site"}
|
|
||||||
|
|
||||||
excel:=function.ReadExcel(pathExcel)
|
// Créez une nouvelle session pour exécuter le backup
|
||||||
key:="host"
|
|
||||||
// Parcourir les résultats d'Excel
|
_, index, err := gochoice.Pick(
|
||||||
for _, value := range excel {
|
"What do you want to do?\nPick:",
|
||||||
// Lire les clés dans l'ordre spécifié
|
[]string{
|
||||||
for _, k := range orderedKeys {
|
"Backup sql and download",
|
||||||
if v, exists := value[k]; exists && k == key {
|
"Backup site",
|
||||||
fmt.Println("Key:", k, "Value:", v)
|
"Check serveur apache",
|
||||||
if(k==selectHost){
|
},
|
||||||
switch k {
|
)
|
||||||
case "path_save_sql":
|
if err != nil {
|
||||||
pathSaveSql = v
|
fmt.Println("You didn't select anything!")
|
||||||
case "path_save_site":
|
return
|
||||||
// pathSaveSite = v
|
}
|
||||||
case "mysql_login":
|
if index == 0 {
|
||||||
mysqlUser = v
|
excel := function.ReadExcel(pathExcel)
|
||||||
case "mysql_pasw":
|
// Parcourir les résultats d'Excel
|
||||||
mysqlPswd = v
|
for _, value := range excel {
|
||||||
}
|
sessionBackup, err := client.NewSession()
|
||||||
}
|
if err != nil {
|
||||||
// Switch pour affecter les variables
|
log.Fatal("Failed to create session for backup: ", err)
|
||||||
|
}
|
||||||
}
|
defer sessionBackup.Close()
|
||||||
|
fmt.Println("--------------------------")
|
||||||
|
for key, val := range value {
|
||||||
|
if strings.Compare(val, selectHost) == 1 {
|
||||||
|
switch key {
|
||||||
|
case "path_save_sql":
|
||||||
|
pathSaveSql = val
|
||||||
|
case "path_save_site":
|
||||||
|
// pathSaveSite = v
|
||||||
|
case "mysql_login":
|
||||||
|
mysqlUser = val
|
||||||
|
case "mysql_pasw":
|
||||||
|
mysqlPswd = val
|
||||||
|
case "name_base":
|
||||||
|
nameBase = val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("--------------------------")
|
||||||
fmt.Println("MySQL User:", mysqlUser)
|
fmt.Println("MySQL User:", mysqlUser)
|
||||||
fmt.Println("MySQL Password:", mysqlPswd)
|
fmt.Println("MySQL Password:", mysqlPswd)
|
||||||
fmt.Println("Path Save SQL:", pathSaveSql)
|
fmt.Println("Path Save SQL:", pathSaveSql)
|
||||||
log.Fatal("stop julien")
|
fmt.Println("Name Base:", nameBase)
|
||||||
|
fmt.Println("--------------------------")
|
||||||
|
//log.Fatal("stop julien")
|
||||||
currentTime := time.Now()
|
currentTime := time.Now()
|
||||||
formattedTime := currentTime.Format("20060102_150405")
|
formattedTime := currentTime.Format("20060102_150405")
|
||||||
|
|
||||||
filename := "backup/test_" + formattedTime + ".sql"
|
filename := "backup/" + nameBase + "_" + formattedTime + ".sql"
|
||||||
commandBackup := "mysqldump -u "+mysqlUser+" -p'"+mysqlPswd+"' test > " + filename
|
commandBackup := "mysqldump -u " + mysqlUser + " -p'" + mysqlPswd + "' " + nameBase + " > " + filename
|
||||||
|
|
||||||
// Maintenant on utilise la sessionBackup pour exécuter la commande du backup
|
// Maintenant on utilise la sessionBackup pour exécuter la commande du backup
|
||||||
if err := sessionBackup.Run(commandBackup); err != nil {
|
if err := sessionBackup.Run(commandBackup); err != nil {
|
||||||
log.Fatal("Failed to run commandBackup: " + err.Error())
|
log.Fatal("Failed to run commandBackup: " + err.Error())
|
||||||
}
|
}
|
||||||
fmt.Println("backup ok")
|
fmt.Println("backup ok")
|
||||||
|
if client == nil {
|
||||||
|
log.Fatal("Client SSH non initialisé")
|
||||||
|
}
|
||||||
sftpClient, err := sftp.NewClient(client)
|
sftpClient, err := sftp.NewClient(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Erreur connection: ", err)
|
log.Fatal("Erreur connection: ", err)
|
||||||
}
|
}
|
||||||
defer sftpClient.Close()
|
defer sftpClient.Close()
|
||||||
|
|
||||||
var pathDownload string
|
|
||||||
fmt.Scanln(&pathDownload)
|
|
||||||
fmt.Println(filename)
|
|
||||||
currentTime2 := time.Now()
|
currentTime2 := time.Now()
|
||||||
formattedTime2:= currentTime2.Format("20060102_150405")
|
formattedTime2 := currentTime2.Format("20060102_150405")
|
||||||
|
filename2 := nameBase+"_" + formattedTime2 + ".sql"
|
||||||
filename2 := "test_" + formattedTime2 + ".sql"
|
|
||||||
err = simplyssh.DownloadFile(sftpClient, filename, pathSaveSql+filename2)
|
err = simplyssh.DownloadFile(sftpClient, filename, pathSaveSql+filename2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Erreur lors du téléchargement: ", err)
|
log.Fatal("Erreur lors du téléchargement: ", err)
|
||||||
}
|
}
|
||||||
fmt.Println("Téléchargement terminé")
|
fmt.Println("Téléchargement terminé")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//log.Fatal("Failed to run: " + err.Error())
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
fmt.Println(b.String())
|
fmt.Println(b.String())
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user