Created
January 8, 2017 21:50
-
-
Save rodkranz/b97eb9c397b6c2e7e6474e1e49747430 to your computer and use it in GitHub Desktop.
create a backup of files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2017 Kranz. All rights reserved. | |
// Use of this source code is governed by a MIT-style | |
// license that can be found in the LICENSE file. | |
package main | |
import ( | |
"os/exec" | |
"log" | |
"os" | |
"time" | |
"fmt" | |
) | |
var ( | |
fileNameBackup string = time.Now().Format("20060102150405") | |
fileNameError string = "error" | |
dirName string = "log" | |
) | |
func main() { | |
// Backup file | |
fileBackup, err := os.Create(fmt.Sprintf("%v/%v.log", dirName, fileNameBackup)) | |
if err != nil { | |
log.Fatalf("Error to create file: %v", err.Error()) | |
os.Exit(1) | |
} | |
defer fileBackup.Close() | |
// Error File | |
fileError, err := os.Create(fmt.Sprintf("%v/%v.log", dirName, fileNameError)) | |
if err != nil { | |
log.Fatalf("Error to create file: %v", err.Error()) | |
os.Exit(1) | |
} | |
defer fileError.Close() | |
// Execute command | |
cmd := exec.Command("_mysql", "-h localhost") | |
cmd.Stdout = fileBackup // return only out of file | |
cmd.Stderr = fileError // return error | |
// execute command | |
if err := cmd.Run(); err != nil { | |
log.Fatalf("Error to exec: %v", err.Error()) | |
os.Exit(1) | |
} | |
// everything is ok. | |
log.Print("command finished!") | |
os.Exit(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment