/
backup.go
100 lines (88 loc) · 2.76 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package gostuff
import (
"fmt"
"log"
"os"
"os/exec"
"runtime"
)
//exports database(without Grandmaster games) to an .sql file as a hot backup
//@param isTemplate If true then export template database
func ExportDatabase(isTemplate bool) {
problems, _ := os.OpenFile("logs/errors.txt", os.O_APPEND|os.O_WRONLY, 0666)
defer problems.Close()
log.SetOutput(problems)
command := "mysqldump --databases gochess --ignore-table=gochess.grandmaster > ./../backup/gochessNoGrandmaster.sql"
if isTemplate {
command = "cd config && mysqldump --databases gochess --no-data > ./../backup/gochessTemplate.sql"
}
if runtime.GOOS == "windows" {
_, err := exec.Command("cmd.exe", "/C", command).Output()
if err != nil {
log.Println(err)
fmt.Println("Error in exporting database, please check logs")
}
} else {
_, err := exec.Command("/bin/bash", "-c", command).Output()
if err != nil {
log.Println(err)
fmt.Println("Error in exporting database, please check logs")
}
}
}
// zips up exported database
func CompressDatabase() {
result := compress("./backup/gochess.zip", []string{"./backup/gochess.sql"})
if result {
fmt.Println("Exported database file succesfully compressed!")
}
}
//imports the main gochess database, returns true if successful
func importDatabase() bool {
problems, _ := os.OpenFile("logs/errors.txt", os.O_APPEND|os.O_WRONLY, 0666)
defer problems.Close()
log.SetOutput(problems)
result := unzip("./backup/gochess.zip", "./backup")
if result == false {
return false
}
if runtime.GOOS == "windows" {
_, err := exec.Command("cmd.exe", "/C", "mysql < ./backup/gochess.sql").Output()
if err != nil {
log.Println(err)
fmt.Println("Error in importing gochess database, please check logs")
return false
}
} else {
_, err := exec.Command("/bin/bash", "-c", "mysql < ./backup/gochess.sql").Output()
if err != nil {
log.Println(err)
fmt.Println("Error in importing gochess database, please check logs")
return false
}
}
return true
}
//imports template database, returns true if sucessful
func importTemplateDatabase() bool {
problems, _ := os.OpenFile("logs/errors.txt", os.O_APPEND|os.O_WRONLY, 0666)
defer problems.Close()
log.SetOutput(problems)
//determine which operating system to execute appropriate shell command
if runtime.GOOS == "windows" {
_, err := exec.Command("cmd.exe", "/C", "mysql < ./backup/gochessTemplate.sql").Output()
if err != nil {
log.Println(err)
fmt.Println("Error in importing template database, please check logs")
return false
}
} else {
_, err := exec.Command("/bin/bash", "-c", "mysql < ./backup/gochessTemplate.sql").Output()
if err != nil {
log.Println(err)
fmt.Println("Error in importing template database, please check logs")
return false
}
}
return true
}