update/backup.go

165 lines
2.8 KiB
Go
Raw Normal View History

2024-09-03 03:38:43 +02:00
package main
import (
2024-09-08 00:23:20 +02:00
"archive/zip"
2024-09-03 03:38:43 +02:00
"bufio"
"fmt"
2024-09-08 00:23:20 +02:00
"io"
2024-09-03 03:38:43 +02:00
"os"
"os/exec"
2024-09-08 00:23:20 +02:00
"path/filepath"
2024-09-03 03:38:43 +02:00
"strings"
2024-09-08 00:23:20 +02:00
"time"
"github.com/pelletier/go-toml"
2024-09-03 03:38:43 +02:00
)
2024-09-08 00:23:20 +02:00
var backup_locations []string
2024-09-03 03:38:43 +02:00
2024-09-08 00:23:20 +02:00
func parsePacmanConf() (string, error) {
var err error
2024-09-03 03:38:43 +02:00
var path string
file, err := os.Open("/etc/pacman.conf")
if err != nil {
2024-09-08 00:23:20 +02:00
return "", err
2024-09-03 03:38:43 +02:00
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if !strings.Contains(line, "DBPath") {
continue
}
path = strings.TrimSpace(strings.Split(line, "=")[1])
break
}
2024-09-08 00:23:20 +02:00
err = file.Close()
if err != nil {
return "", err
2024-09-03 03:38:43 +02:00
}
2024-09-08 00:23:20 +02:00
return fmt.Sprint(path, "local"), nil
2024-09-03 03:38:43 +02:00
}
2024-09-08 00:23:20 +02:00
func zipIt(pathToZip string, pathToFiles string) error {
var err error
2024-09-03 03:38:43 +02:00
2024-09-08 00:23:20 +02:00
zipFile, err := os.Create(pathToZip)
2024-09-03 03:38:43 +02:00
if err != nil {
2024-09-08 00:23:20 +02:00
return err
2024-09-03 03:38:43 +02:00
}
2024-09-08 00:23:20 +02:00
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
}
2024-09-03 03:38:43 +02:00
2024-09-08 00:23:20 +02:00
err = zipWriter.Close()
2024-09-03 03:38:43 +02:00
if err != nil {
2024-09-08 00:23:20 +02:00
return err
2024-09-03 03:38:43 +02:00
}
2024-09-08 00:23:20 +02:00
err = zipFile.Close()
if err != nil {
return err
}
return nil
}
2024-09-03 03:38:43 +02:00
2024-09-08 00:23:20 +02:00
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)
if err != nil {
return err
}
2024-09-03 03:38:43 +02:00
defer func() {
2024-09-08 00:23:20 +02:00
os.RemoveAll("/tmp/update")
2024-09-03 03:38:43 +02:00
}()
2024-09-08 00:23:20 +02:00
output, err := exec.Command("pacman", "--verbose", "--query").Output()
2024-09-03 03:38:43 +02:00
if err != nil {
2024-09-08 00:23:20 +02:00
return err
2024-09-03 03:38:43 +02:00
}
2024-09-08 00:23:20 +02:00
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
2024-09-03 03:38:43 +02:00
}
func main() {
2024-09-08 00:23:20 +02:00
os.RemoveAll("/tmp/update")
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)
2024-09-03 03:38:43 +02:00
}
}