-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-using-sftp.go
126 lines (125 loc) · 3.12 KB
/
backup-using-sftp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package goback
// type RemoteSite interface {
// Open() error
// Close() error
// Send(srcPath, dstPath string) error
// Test() error
// }
//
// type FtpSite struct {
// Protocol int // FTP, SFTP
// Host string
// Port int
// Dir string
// Username string
// Password string
// conn *sftp.Client
// }
//
// func newFtpSite(protocol int, host string, port int, dir, username, password string) *FtpSite {
// return &FtpSite{
// Protocol: protocol,
// Host: host,
// Port: port,
// Dir: dir,
// Username: username,
// Password: password,
// }
// }
//
// func (f *FtpSite) Open() error {
// var auths []ssh.AuthMethod
// if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
// auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))
// }
// auths = append(auths, ssh.Password(f.Password))
// config := ssh.ClientConfig{
// User: f.Username,
// Auth: auths,
// HostKeyCallback: ssh.InsecureIgnoreHostKey(),
// }
// conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", f.Host, f.Port), &config)
// if err != nil {
// return fmt.Errorf("failed to connect to SFTP server: %w", err)
// }
//
// size := 1 << 15
// ftpConn, err := sftp.NewClient(conn, sftp.MaxPacket(size))
// if err != nil {
// return fmt.Errorf("failed to create to SFTP client: %w", err)
// }
//
// f.conn = ftpConn
// return nil
// }
//
// func (f *FtpSite) Test() error {
// f.conn.Walk(f.Dir)
// return nil
// }
//
// func (f FtpSite) Close() error {
// if err := f.conn.Close(); err != nil {
// return fmt.Errorf("failed to close SFTP connection: %w", err)
// }
// return nil
// }
//
// func (f FtpSite) Send(src, dst string) error {
// d := filepath.ToSlash(filepath.Join("/backup", filepath.Base(src)))
// w, err := f.conn.OpenFile(d, os.O_CREATE|os.O_RDWR|os.O_TRUNC)
// if err != nil {
// return err
// }
// defer w.Close()
//
// file, err := os.Open(src)
// if err != nil {
// return err
// }
// defer f.Close()
// fi, err := file.Stat()
// if err != nil {
// return err
// }
// size := fi.Size()
// log.Debugf("writing %v bytes", size)
// t1 := time.Now()
// n, err := io.Copy(w, io.LimitReader(file, size))
// if err != nil {
// return err
// }
// if n != size {
// return fmt.Errorf("copy: expected %v bytes, got %d", size, n)
// }
// log.Debug("wrote %v bytes in %s", size, time.Since(t1))
// return nil
// }
//
// func (b *Backup) sendChangedFiles() error {
// if err := b.ftpSite.Open(); err != nil {
// return fmt.Errorf("failed to open remote ftp: %w", err)
// }
// defer b.ftpSite.Close()
// fileGroup, _, err := b.createBackupFileGroup()
// if err != nil {
// return err
// }
//
// for i := range fileGroup {
// for j := range fileGroup[i] {
// fileWrapper := fileGroup[i][j]
// // spew.Dump(fileGroup[i][j])
// // send := filepath.Join(fileWrapper.dir, fileWrapper)
// dst := filepath.Join(b.dstDir)
// if err := b.ftpSite.Send(fileWrapper.Path, filepath.Base(fileWrapper.Path)); err != nil {
// log.Error(err)
// continue
// }
// }
// }
//
// // spew.Dump(fileGroup)
//
// return nil
// }