done with new version
This commit is contained in:
184
backup.go
184
backup.go
@ -1,42 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pelletier/go-toml"
|
||||
)
|
||||
|
||||
func parseEnvironment(key string) string {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
log.Panicf("%s is not set\n", key)
|
||||
}
|
||||
if _, err := os.Stat(value); err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
log.Panicf("%s does not point to a valid path\n", key)
|
||||
}
|
||||
log.Panicf("trying to stat %s produced an unkown error\n", value)
|
||||
}
|
||||
return value
|
||||
}
|
||||
var backup_locations []string
|
||||
|
||||
func parsePacmanConf() string {
|
||||
func parsePacmanConf() (string, error) {
|
||||
var err error
|
||||
var path string
|
||||
|
||||
file, err := os.Open("/etc/pacman.conf")
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
return "", err
|
||||
}
|
||||
defer func() {
|
||||
if err := file.Close(); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
}()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
@ -48,73 +35,130 @@ func parsePacmanConf() string {
|
||||
break
|
||||
}
|
||||
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
log.Panicln("DBPath in /etc/pacman.conf does not point to a valid path")
|
||||
}
|
||||
log.Panicf("trying to stat %s produced an unkown error\n", path)
|
||||
err = file.Close()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s%s", path, "local")
|
||||
return fmt.Sprint(path, "local"), nil
|
||||
}
|
||||
|
||||
func preBackup() {
|
||||
pacmanDb := parsePacmanConf()
|
||||
log.Printf("pacmanDb: %s\n", pacmanDb)
|
||||
tmpPath := "/tmp/update/pre_backup"
|
||||
func zipIt(pathToZip string, pathToFiles string) error {
|
||||
var err error
|
||||
|
||||
output, err := exec.Command("pacman", "--verbose", "--query").Output()
|
||||
zipFile, err := os.Create(pathToZip)
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
return err
|
||||
}
|
||||
|
||||
zipWriter := zip.NewWriter(zipFile)
|
||||
|
||||
walker := func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
f, err := zipWriter.Create(path[len(pathToFiles):])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = io.Copy(f, file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
err = filepath.Walk(pathToFiles, walker)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = zipWriter.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = zipFile.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func backup(when string) error {
|
||||
var err error
|
||||
tmpPath := fmt.Sprint("/tmp/update/", when, "_backup")
|
||||
pacmanDb, err := parsePacmanConf()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.MkdirAll(tmpPath, 0755)
|
||||
|
||||
err = os.WriteFile(fmt.Sprintf("%s/%s", tmpPath, "pre_pacman.txt"), output, 0644)
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err := os.RemoveAll(tmpPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = CopyDir(pacmanDb, fmt.Sprintf("%s/%s", tmpPath, pacmanDb))
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
log.Println("recovered successfully")
|
||||
}
|
||||
os.RemoveAll("/tmp/update")
|
||||
}()
|
||||
|
||||
err = CopyDir(pacmanDb, fmt.Sprintf("%s/%s", tmpPath, pacmanDb))
|
||||
output, err := exec.Command("pacman", "--verbose", "--query").Output()
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func postBackup() {
|
||||
/*
|
||||
* basic logic for post:
|
||||
* create new pkg list and delete all lines which are the same as pre
|
||||
*/
|
||||
panic("unimplemented")
|
||||
err = os.WriteFile(fmt.Sprint(tmpPath, fmt.Sprint(when, "_pacman.txt")), output, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if when != "post" {
|
||||
err = CopyDir(pacmanDb, fmt.Sprint(tmpPath, "/", pacmanDb))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
current_time := time.Now().Format(time.RFC3339)
|
||||
pathToZip := fmt.Sprint(tmpPath, "_", current_time, ".zip")
|
||||
err = zipIt(pathToZip, tmpPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, backup_location := range backup_locations {
|
||||
err = CopyFile(pathToZip, fmt.Sprint(backup_location, "/", when, "_backup_", current_time, ".zip"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
// backup_dir := parseEnvironment("UPDATE_BACKUP_DIR")
|
||||
cmdlineArg := os.Args[1]
|
||||
log.Printf("cmdlineArg: %s\n", cmdlineArg)
|
||||
os.RemoveAll("/tmp/update")
|
||||
|
||||
if cmdlineArg == "pre" {
|
||||
preBackup()
|
||||
} else if cmdlineArg == "post" {
|
||||
postBackup()
|
||||
} else {
|
||||
log.Panicf("invalid cmdline argument. supplied value was: %s\n", cmdlineArg)
|
||||
tree, err := toml.LoadFile("/etc/update.toml")
|
||||
if err != nil {
|
||||
fmt.Println("backup failed for the following reason: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
backup_locations = tree.GetArray("backup.locations").([]string)
|
||||
|
||||
err = backup(os.Args[1])
|
||||
if err != nil {
|
||||
fmt.Println("backup failed for the following reason: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user