debug logs
This commit is contained in:
1 file changed
+38
-23
@@ -2,17 +2,19 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"log"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const dtFormat string = "2006-01-02 15:04:05 Monday"
|
const dtFormat string = "2006-01-02 15:04:05 Monday"
|
||||||
|
const logsBreakLine string = "------------------------ mongodb-backup-script ------------------------"
|
||||||
|
|
||||||
func Handle(err error) {
|
func Handle(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -21,9 +23,13 @@ func Handle(err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Basic logging
|
// Basic logging
|
||||||
func LogToFile(message string) {
|
func LogToFile(message string, debug bool) {
|
||||||
path, _ := os.Getwd()
|
path, _ := os.Getwd()
|
||||||
f, err := os.OpenFile(filepath.Join(path, "databaseBackup.log"), os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
|
var logFile string = "databaseBackup.log"
|
||||||
|
if debug {
|
||||||
|
logFile = "databaseBackup.debug.log"
|
||||||
|
}
|
||||||
|
f, err := os.OpenFile(filepath.Join(path, logFile), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
Handle(err)
|
Handle(err)
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
@@ -35,32 +41,36 @@ func UploadToGithub(archiveDir string) {
|
|||||||
// Add archive files to stage
|
// Add archive files to stage
|
||||||
cmd := exec.Command("git", "add", ".")
|
cmd := exec.Command("git", "add", ".")
|
||||||
cmd.Dir = archiveDir
|
cmd.Dir = archiveDir
|
||||||
_, err := cmd.Output()
|
stdout, err := cmd.Output()
|
||||||
|
LogToFile(string(stdout), true)
|
||||||
Handle(err)
|
Handle(err)
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
var author string = os.Getenv("github_author")
|
var author string = os.Getenv("github_author")
|
||||||
var email string = os.Getenv("github_email")
|
var email string = os.Getenv("github_email")
|
||||||
|
|
||||||
if author == "" || email == "" {
|
if author == "" || email == "" {
|
||||||
LogToFile("ERROR - github link provided but no author or email, add to .env: github_author=<username> and github_email=<email>")
|
LogToFile(fmt.Sprintf("ERROR - github link provided but no author or email, add to .env: github_author=<username> and github_email=<email>\n%s", logsBreakLine), true)
|
||||||
|
LogToFile(fmt.Sprintf("ERROR - github link provided but no author or email, add to .env: github_author=<username> and github_email=<email>\n%s", logsBreakLine), false)
|
||||||
log.Panic("Missing required .env: github_author, github_email")
|
log.Panic("Missing required .env: github_author, github_email")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit archive files
|
// Commit archive files
|
||||||
cmd = exec.Command("git", "commit", "-m", fmt.Sprintf("'%s''", now.Format(dtFormat)), fmt.Sprintf("--author=\"%s <%s>\"", author, email))
|
cmd = exec.Command("git", "commit", "-m", fmt.Sprintf("'%s'", now.Format(dtFormat)), fmt.Sprintf("--author=\"%s <%s>\"", author, email))
|
||||||
cmd.Dir = archiveDir
|
cmd.Dir = archiveDir
|
||||||
_, err = cmd.Output()
|
stdout, err = cmd.Output()
|
||||||
|
LogToFile(string(stdout), true)
|
||||||
Handle(err)
|
Handle(err)
|
||||||
|
|
||||||
// Push archive files to repository
|
// Push archive files to repository
|
||||||
cmd = exec.Command("git", "push", "origin", "master", "--force")
|
cmd = exec.Command("git", "push", "origin", "master", "--force")
|
||||||
cmd.Dir = archiveDir
|
cmd.Dir = archiveDir
|
||||||
_, err = cmd.Output()
|
stdout, err = cmd.Output()
|
||||||
|
LogToFile(string(stdout), true)
|
||||||
Handle(err)
|
Handle(err)
|
||||||
|
|
||||||
LogToFile("Successfully uploaded archive to github repository")
|
LogToFile("Successfully uploaded archive to github repository", false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -68,13 +78,14 @@ func main() {
|
|||||||
|
|
||||||
// Load environment variables
|
// Load environment variables
|
||||||
godotenv.Load(filepath.Join(path, ".env"))
|
godotenv.Load(filepath.Join(path, ".env"))
|
||||||
var mongoURI string = os.Getenv("mongoURI")
|
var mongoURI string = os.Getenv("mongoURI")
|
||||||
var database_string string = os.Getenv("databases")
|
var database_string string = os.Getenv("databases")
|
||||||
var github string = os.Getenv("github")
|
var github string = os.Getenv("github")
|
||||||
|
|
||||||
// Ensure required variables
|
// Ensure required variables
|
||||||
if mongoURI == "" || database_string == "" {
|
if mongoURI == "" || database_string == "" {
|
||||||
LogToFile("ERROR - Missing required .env variables: mongoURI, databases")
|
LogToFile(fmt.Sprintf("ERROR - Missing required .env variables: mongoURI, databases\n%s", logsBreakLine), true)
|
||||||
|
LogToFile(fmt.Sprintf("ERROR - Missing required .env variables: mongoURI, databases\n%s", logsBreakLine), false)
|
||||||
log.Panic("Missing required .env variables: mongoURI, databases")
|
log.Panic("Missing required .env variables: mongoURI, databases")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,36 +100,40 @@ func main() {
|
|||||||
if github != "" {
|
if github != "" {
|
||||||
cmd := exec.Command("git", "init")
|
cmd := exec.Command("git", "init")
|
||||||
cmd.Dir = archiveDir
|
cmd.Dir = archiveDir
|
||||||
_, err := cmd.Output()
|
stdout, err := cmd.Output()
|
||||||
|
LogToFile(string(stdout), true)
|
||||||
Handle(err)
|
Handle(err)
|
||||||
|
|
||||||
cmd = exec.Command("git", "remote", "add", "origin", github)
|
cmd = exec.Command("git", "remote", "add", "origin", github)
|
||||||
cmd.Dir = archiveDir
|
cmd.Dir = archiveDir
|
||||||
_, err = cmd.Output()
|
stdout, err = cmd.Output()
|
||||||
|
LogToFile(string(stdout), true)
|
||||||
Handle(err)
|
Handle(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform mongodump to archive databases to .gzip format
|
// Perform mongodump to archive databases to .gzip format
|
||||||
for _, db := range databases {
|
for _, db := range databases {
|
||||||
archivePath := filepath.Join(archiveDir, db + ".gzip")
|
archivePath := filepath.Join(archiveDir, db+".gzip")
|
||||||
cmd := exec.Command(
|
cmd := exec.Command(
|
||||||
"mongodump",
|
"mongodump",
|
||||||
"--uri=" + mongoURI,
|
"--uri="+mongoURI,
|
||||||
"--authenticationDatabase=admin",
|
"--authenticationDatabase=admin",
|
||||||
"--db=" + db,
|
"--db="+db,
|
||||||
"--archive=" + archivePath,
|
"--archive="+archivePath,
|
||||||
"--gzip",
|
"--gzip",
|
||||||
)
|
)
|
||||||
_, err := cmd.Output()
|
stdout, err := cmd.Output()
|
||||||
|
LogToFile(string(stdout), true)
|
||||||
Handle(err)
|
Handle(err)
|
||||||
|
|
||||||
LogToFile(fmt.Sprintf("Successfully archived %s", db))
|
LogToFile(fmt.Sprintf("Successfully archived %s", db), false)
|
||||||
}
|
}
|
||||||
|
|
||||||
if github != "" {
|
if github != "" {
|
||||||
UploadToGithub(archiveDir)
|
UploadToGithub(archiveDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
LogToFile("------------------------ mongodb-backup-script ------------------------") // Log break for easier viewing
|
LogToFile(logsBreakLine, false) // Log break for easier viewing
|
||||||
|
LogToFile(logsBreakLine, true) // Log break for easier viewing
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user