-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.go
80 lines (69 loc) · 1.84 KB
/
backup.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
package main
import (
"fmt"
"net"
"os"
"strconv"
"bytes"
"github.com/robfig/cron"
"os/exec"
)
const (
CONN_PORT = "3333"
CONN_TYPE = "tcp"
)
func main() {
// Listen for incoming connections.
l, err := net.Listen(CONN_TYPE, ":"+CONN_PORT)
if err != nil {
fmt.Println("Cannot bind! ", err.Error())
os.Exit(1)
}
// Close the listener when the application closes.
defer l.Close()
fmt.Println("Useless listen " + CONN_PORT)
c := cron.New()
c.AddFunc(os.Getenv("SCHEDULE"), func() { fmt.Println("Cron schedule: "+os.Getenv("SCHEDULE")) })
c.AddFunc(os.Getenv("SCHEDULE"), func() { exe_cmd("./cron_script.sh") })
c.Start()
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
//logs an incoming message
fmt.Printf("Received message %s -> %s \n", conn.RemoteAddr(), conn.LocalAddr())
// Handle connections in a new goroutine.
go handleRequest(conn)
}
}
// Handles incoming requests.
func handleRequest(conn net.Conn) {
// Make a buffer to hold incoming data.
buf := make([]byte, 1024)
// Read the incoming connection into the buffer.
reqLen, err := conn.Read(buf)
if err != nil {
fmt.Println("Error reading:", err.Error())
}
// Builds the message.
message := "Hi, I received your message! It was "
message += strconv.Itoa(reqLen)
message += " bytes long and that's what it said: \""
n := bytes.Index(buf, []byte{0})
message += string(buf[:n-1])
message += "\" ! Honestly I have no clue about what to do with your messages, so Bye Bye!\n"
// Write the message in the connection channel.
conn.Write([]byte(message));
// Close the connection when you're done with it.
conn.Close()
}
func exe_cmd(cmd string) {
out, err := exec.Command("sh","-c",cmd).Output()
if err != nil {
fmt.Printf("%s", err)
}
fmt.Printf("%s", out)
}