ssh/example/getdata/main.go

148 lines
3.3 KiB
Go
Raw Normal View History

2019-03-12 17:12:42 +08:00
package main
2018-12-19 14:33:31 +08:00
import (
"fmt"
2019-03-12 17:12:42 +08:00
"io"
2018-12-19 14:33:31 +08:00
"log"
"os"
"path"
2019-03-12 17:12:42 +08:00
"strconv"
2018-12-19 14:33:31 +08:00
"time"
2019-03-12 17:12:42 +08:00
"github.com/yeka/zip"
2018-12-19 14:33:31 +08:00
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
func connect(user, password, host string, port int) (*sftp.Client, error) {
var (
auth []ssh.AuthMethod
addr string
clientConfig *ssh.ClientConfig
sshClient *ssh.Client
sftpClient *sftp.Client
err error
)
// get auth method
auth = make([]ssh.AuthMethod, 0)
auth = append(auth, ssh.Password(password))
clientConfig = &ssh.ClientConfig{
User: user,
Auth: auth,
Timeout: 30 * time.Second,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
// connet to ssh
addr = fmt.Sprintf("%s:%d", host, port)
if sshClient, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
return nil, err
}
// create sftp client
if sftpClient, err = sftp.NewClient(sshClient); err != nil {
return nil, err
}
return sftpClient, nil
}
var FORMAT = "2006-01-02"
var (
err error
sftpClient *sftp.Client
)
var dbnames = []string{"tower", "mengyin", "pingyi", "shizhi", "tancheng", "yinan", "yishui", "feixian", "gaoxinqu", "hedong", "jingkaiqu", "junan", "luozhuang", "lanling", "lanshan", "lingang", "linshu"}
func main() {
// 这里换成实际的 SSH 连接的 用户名密码主机名或IPSSH端口
// sftpClient, err = connect("root", "sdlylshl871016", "111.235.181.127", 443)
sftpClient, err = connect("root", "HR2018!!", "15.14.12.150", 22)
if err != nil {
log.Println(err)
}
defer sftpClient.Close()
// 用来测试的远程文件路径 和 本地文件夹
// fmt.Println(shizhi)
// var localDir = "."
2019-03-12 17:12:42 +08:00
var dsts []string
tmp := os.TempDir()
date_dir := path.Join(tmp, "db_"+time.Now().Format(FORMAT))
2018-12-19 14:33:31 +08:00
os.Mkdir(date_dir, 0755)
var lzkpbi = "/docker/backup/" + time.Now().Format(FORMAT) + "_lzkp_bi_inner.zip"
Down(lzkpbi, date_dir)
2019-03-12 17:12:42 +08:00
dsts = append(dsts, path.Join(date_dir, time.Now().Format(FORMAT)+"_lzkp_bi_inner.zip"))
2018-12-19 14:33:31 +08:00
for _, n := range dbnames {
p := "/docker/backup/" + time.Now().Format(FORMAT) + "_" + n + "_inner.zip"
// fmt.Println(p)
Down(p, date_dir)
2019-03-12 17:12:42 +08:00
dsts = append(dsts, path.Join(date_dir, time.Now().Format(FORMAT)+"_"+n+"_inner.zip"))
2018-12-19 14:33:31 +08:00
}
2019-03-12 17:12:42 +08:00
zippass("", dsts...)
2018-12-19 14:33:31 +08:00
// fmt.Scanln()
2019-03-12 17:12:42 +08:00
for _, v := range dsts {
// fmt.Println(v)
// ioutil.WriteFile(v, []byte("aaa"), 755)
os.Remove(v)
}
2018-12-19 14:33:31 +08:00
}
func Down(src, dst string) {
2019-03-12 17:12:42 +08:00
// fmt.Println(src, "数据正在复制中,请耐心等待...")
2018-12-19 14:33:31 +08:00
srcFile, err := sftpClient.Open(src)
if err != nil {
2019-03-12 17:12:42 +08:00
// log.Println(err)
2018-12-19 14:33:31 +08:00
return
}
defer srcFile.Close()
var localFileName = path.Base(src)
dstFile, err := os.Create(path.Join(dst, localFileName))
if err != nil {
2019-03-12 17:12:42 +08:00
// log.Println(err)
2018-12-19 14:33:31 +08:00
return
}
defer dstFile.Close()
if _, err = srcFile.WriteTo(dstFile); err != nil {
2019-03-12 17:12:42 +08:00
// log.Println(err)
2018-12-19 14:33:31 +08:00
return
}
2019-03-12 17:12:42 +08:00
// fmt.Println(src, "数据复制完成!")
}
2018-12-19 14:33:31 +08:00
2019-03-12 17:12:42 +08:00
func zippass(dst string, src ...string) {
fzip, err := os.Create(`D:/待测试数据.zip`)
if err != nil {
log.Fatalln(err)
}
zipw := zip.NewWriter(fzip)
defer zipw.Close()
for i, n := range src {
w, err := zipw.Encrypt(strconv.Itoa(i), `hangruan2017`, zip.AES256Encryption)
if err != nil {
log.Fatal(err)
}
f, err := os.Open(n)
if err != nil {
return
}
_, err = io.Copy(w, f)
if err != nil {
log.Fatal(err)
}
}
zipw.Flush()
2018-12-19 14:33:31 +08:00
}