124 lines
3.6 KiB
Go
124 lines
3.6 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"fmt"
|
||
|
|
"log"
|
||
|
|
"time"
|
||
|
|
simplyssh "canguidev.fr/pvnet/simply-ssh"
|
||
|
|
"github.com/TwiN/go-choice"
|
||
|
|
"github.com/pkg/sftp"
|
||
|
|
"github.com/fatih/color"
|
||
|
|
)
|
||
|
|
|
||
|
|
func main(){
|
||
|
|
//var path string="/Users/cangui/.ssh/julien_pvnet"
|
||
|
|
color.Blue("Enter path ssh key: ")
|
||
|
|
|
||
|
|
var path string
|
||
|
|
// fonction pour recupére la donnée saisie
|
||
|
|
fmt.Scanln(&path)
|
||
|
|
// Chargement de la liste des hosts du fichier config ssh
|
||
|
|
d:=simplyssh.GetHost()
|
||
|
|
var s []string
|
||
|
|
for _, host := range d {
|
||
|
|
s=append(s,string(host.Id)+" "+host.Host)
|
||
|
|
}
|
||
|
|
// selectionne du host
|
||
|
|
choice, index, err := gochoice.Pick(
|
||
|
|
"What do you want to do?\nPick:",
|
||
|
|
s,
|
||
|
|
)
|
||
|
|
if err != nil {
|
||
|
|
fmt.Println("You didn't select anything!")
|
||
|
|
} else {
|
||
|
|
for _, host := range d {
|
||
|
|
// for sur les hosts et break sur le host selectionner
|
||
|
|
if(host.Id == uint(index)){
|
||
|
|
fmt.Println("debut de connection")
|
||
|
|
// connection en ssh avec la clée et les informations du fichier config
|
||
|
|
client:=simplyssh.Connect(path,host.HostName,host.Port,host.User)
|
||
|
|
command := "mkdir backup"
|
||
|
|
defer client.Close()
|
||
|
|
|
||
|
|
// Ouverture d une session pour excecuté la commande
|
||
|
|
session, err := client.NewSession()
|
||
|
|
if err != nil {
|
||
|
|
log.Fatal("Failed to create session: ", err)
|
||
|
|
}
|
||
|
|
defer session.Close()
|
||
|
|
// initialisation du buffer pour recupere les infos reçu de l' hôte
|
||
|
|
var b bytes.Buffer
|
||
|
|
session.Stdout = &b
|
||
|
|
//exceution de la commande
|
||
|
|
if err := session.Run(command); err != nil {
|
||
|
|
fmt.Println("Dossier existe dejà")
|
||
|
|
// Fermez donc cette session
|
||
|
|
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) {
|
||
|
|
currentTime := time.Now()
|
||
|
|
formattedTime := currentTime.Format("20060102_150405")
|
||
|
|
|
||
|
|
filename := "backup/test_" + formattedTime + ".sql"
|
||
|
|
commandBackup := "mysqldump -u cangui@localhost -p'GHT30k7!' test > " + filename
|
||
|
|
|
||
|
|
// Maintenant on utilise la sessionBackup pour exécuter la commande du backup
|
||
|
|
if err := sessionBackup.Run(commandBackup); err != nil {
|
||
|
|
log.Fatal("Failed to run commandBackup: " + err.Error())
|
||
|
|
}
|
||
|
|
fmt.Println("backup ok")
|
||
|
|
|
||
|
|
sftpClient, err := sftp.NewClient(client)
|
||
|
|
if err != nil {
|
||
|
|
log.Fatal("Erreur connection: ", err)
|
||
|
|
}
|
||
|
|
defer sftpClient.Close()
|
||
|
|
|
||
|
|
var pathDownload string
|
||
|
|
fmt.Scanln(&pathDownload)
|
||
|
|
fmt.Println(filename)
|
||
|
|
currentTime2 := time.Now()
|
||
|
|
formattedTime2:= currentTime2.Format("20060102_150405")
|
||
|
|
|
||
|
|
filename2 := "test_" + formattedTime2 + ".sql"
|
||
|
|
err = simplyssh.DownloadFile(sftpClient, filename, "/Users/cangui/Documents/Dev peso/Golang dev/"+filename2)
|
||
|
|
if err != nil {
|
||
|
|
log.Fatal("Erreur lors du téléchargement: ", err)
|
||
|
|
}
|
||
|
|
fmt.Println("Téléchargement terminé")
|
||
|
|
}
|
||
|
|
|
||
|
|
//log.Fatal("Failed to run: " + err.Error())
|
||
|
|
}
|
||
|
|
fmt.Println(b.String())
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
fmt.Printf("You have selected: '%s', which is the index %d\n", choice, index)
|
||
|
|
}
|
||
|
|
}
|