-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
142 lines (122 loc) · 3.29 KB
/
main.go
File metadata and controls
142 lines (122 loc) · 3.29 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"io/ioutil"
"net"
"os"
"time"
"github.com/pkg/sftp"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh"
)
func init() {
pflag.String("keyFile", "~/.ssh/id_rsa", "Path to KeyFile used")
pflag.String("host", "", "Host to connect to")
pflag.String("port", "22", "SSH Port to use")
pflag.String("remoteBasePath", "", "Remote Base Path to backup folder")
pflag.String("localBasePath", "", "Local Base apth for backup")
pflag.String("user", "", "Username")
pflag.String("dbName", "insitu", "DBName of Backup")
pflag.String("logfile", "", "Logile path")
pflag.String("config", "", "Configfile")
pflag.Bool("debug", false, "show debug log output")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
if viper.GetString("config") != "" {
viper.SetConfigName("go_db_backup")
viper.AddConfigPath("config")
viper.AddConfigPath("config/default")
err := viper.ReadInConfig()
if err != nil {
log.Fatal(err)
}
}
if viper.GetString("logfile") != "" {
f, err := os.OpenFile(viper.GetString("logfile"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
log.SetOutput(f)
}
if viper.GetBool("debug") {
log.SetLevel(log.DebugLevel)
}
}
func main() {
var now = time.Now().Local()
log.WithFields(
log.Fields{
"time": now.Format("2006-01-02_15:04:02"),
},
).Info("Start Backup Retrival")
var backupFileName = viper.GetString("dbName") + "_" + now.Format("2006-01-02") + ".7z"
var backupFilePath = viper.GetString("remoteBasePath") + backupFileName
privateKey, err := ioutil.ReadFile(viper.GetString("keyFile"))
if err != nil {
log.Fatal(err)
}
signer, err := ssh.ParsePrivateKey(privateKey)
if err != nil {
log.Fatal(err)
}
config := &ssh.ClientConfig{
User: viper.GetString("user"),
HostKeyCallback: keyCallBack,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
}
log.WithField(
"host",
viper.GetString("host")+":"+viper.GetString("port"),
).Debug("Try to establish connection")
client, err := ssh.Dial(
"tcp",
viper.GetString("host")+":"+viper.GetString("port"),
config,
)
if err != nil {
log.WithField(
"host",
viper.GetString("host")+":"+viper.GetString("port"),
).Fatal(err)
}
log.WithField(
"time",
time.Now().Local().Format("2006-01-02_15:04:02"),
).Debug("Connection established")
// open an SFTP session over an existing ssh connection.
sftp, err := sftp.NewClient(client)
if err != nil {
log.Fatal(err)
}
defer sftp.Close()
// Open the source file
srcFile, err := sftp.Open(backupFilePath)
if err != nil {
log.Fatal(err)
}
defer srcFile.Close()
log.Debug("Connections / Interfaces created")
// Create the destination file
dstFile, err := os.Create(viper.GetString("localBasePath") + backupFileName)
if err != nil {
log.Fatal(err)
}
defer dstFile.Close()
log.Debug("Local File touched")
// Copy the file
written, err := srcFile.WriteTo(dstFile)
if err != nil {
log.Fatal(err)
}
log.WithFields(log.Fields{
"time": time.Now().Local().Format("2006-01-02_15:04:02"),
"bytes": written,
"localPath": viper.GetString("localBasePath") + backupFileName,
}).Info("Backup successfully written")
}
func keyCallBack(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
}