Compare commits

...

14 Commits

Author SHA1 Message Date
d101e186c4 go get -u && go mod tidy 2025-03-23 20:13:23 +01:00
2d881a0d34 Add nfs check (#28)
For users that use a nfs share for their backup, this will check if it is mounted.

Co-authored-by: AustrianToast <austriantoast@hopeless-cloud.xyz>
Reviewed-on: #28
Co-authored-by: ProfessionalUwU <andre.fuhry@hopeless-cloud.xyz>
Co-committed-by: ProfessionalUwU <andre.fuhry@hopeless-cloud.xyz>
2025-01-29 18:13:39 +01:00
cb735a01a1 switch to different copy 2024-09-27 16:36:44 +02:00
e3c9924fbc progress 2024-09-27 16:36:43 +02:00
3645f7f30c critical fixes 2024-09-12 19:01:16 +02:00
66ca74441d change ZSH_CUSTOM default path 2024-09-08 23:06:20 +02:00
7bca34cc89 add newline 2024-09-08 23:05:16 +02:00
e4ab48fb22 fix typo 2024-09-08 23:04:11 +02:00
67c7f48cd7 update README.md 2024-09-08 23:02:34 +02:00
e2d4253050 re-add functionality
This file is needed to restore preview and flatpak update functionality
2024-09-08 22:48:27 +02:00
81b07991aa move stuff around 2024-09-08 16:36:19 +02:00
452b0e92e2 remove justfile 2024-09-08 00:24:39 +02:00
f89e7112a4 remove executable 2024-09-08 00:24:03 +02:00
b28009f83b done with new version 2024-09-08 00:23:20 +02:00
15 changed files with 293 additions and 233 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
backup

View File

@ -1,28 +1,33 @@
# Update # Update
Update script written in Bash for Arch Linux only. Keeps all your official, aur packages and your flatpaks up to date with one simple script. It also backups a list of all your installed packages and flatpaks. Alpm hooks for the pacman package manager.<br>
This project consists of two hooks, one for pre-transaction and the other for
post-transaction.
## Installation ## Installation
```bash ```bash
git clone https://gitea.hopeless-cloud.xyz/AustrianToast/update.git && cd update git clone https://gitea.hopeless-cloud.xyz/AustrianToast/update.git
cd update
``` ```
Before installing, please edit the config and configure it to your liking. Before installing, please edit the config and configure it to your liking.
Then install using Then install using
```bash ```bash
make make install
``` ```
## Usage If your require the previous functionality of being able to update flatpak and
having the ability to preview the updateable packages and flatpaks.<br>
This functionality has been re-packaged into `update.zsh`.<br>
If you use oh-my-zsh, then you just throw this file into your `$ZSH_CUSTOM`.<br>
This path is by default `~/.oh-my-zsh/custom`. If you use only plain zsh or any other
shell, then you can just take the contained functions and put them inside the
according shell config file. For example your `.bashrc`.
``` The function `flatpak-update` contains a variable called `BACKUP_LOCATION`.<br>
Usage: update [OPTION] Please change this path to your desired backup location.
options:
--help displays this message
--preview shows a preview of which pkg's will be updates
--version prints out the version number
```
## Contributing ## Contributing
Contributions are always welcome! Contributions are always welcome!

197
backup.go
View File

@ -1,42 +1,31 @@
package main package main
import ( import (
"archive/zip"
"bufio" "bufio"
"errors"
"fmt" "fmt"
"io/fs" "io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"strings" "strings"
"time"
"github.com/otiai10/copy"
"github.com/pelletier/go-toml"
) )
func parseEnvironment(key string) string { var backup_locations []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
}
func parsePacmanConf() string { func parsePacmanConf() (string, error) {
var err error
var path string var path string
file, err := os.Open("/etc/pacman.conf") file, err := os.Open("/etc/pacman.conf")
if err != nil { if err != nil {
log.Panicln(err) return "", err
} }
defer func() {
if err := file.Close(); err != nil {
log.Panicln(err)
}
}()
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
@ -48,73 +37,153 @@ func parsePacmanConf() string {
break break
} }
if _, err := os.Stat(path); err != nil { err = file.Close()
if errors.Is(err, fs.ErrNotExist) { if err != nil {
log.Panicln("DBPath in /etc/pacman.conf does not point to a valid path") return "", err
}
log.Panicf("trying to stat %s produced an unkown error\n", path)
} }
return fmt.Sprintf("%s%s", path, "local") return fmt.Sprint(path, "local"), nil
} }
func preBackup() { func zipIt(pathToZip string, pathToFiles string) error {
pacmanDb := parsePacmanConf() var err error
log.Printf("pacmanDb: %s\n", pacmanDb)
tmpPath := "/tmp/update/pre_backup" zipFile, err := os.Create(pathToZip)
if err != nil {
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")
log.Printf("tmpPath is: %s\n", tmpPath)
pacmanDb, err := parsePacmanConf()
if err != nil {
return err
}
log.Printf("pacmanDB is at: %s\n", pacmanDb)
err = os.MkdirAll(tmpPath, 0755)
if err != nil {
return err
}
defer func() {
os.RemoveAll("/tmp/update")
}()
output, err := exec.Command("pacman", "--verbose", "--query").Output() output, err := exec.Command("pacman", "--verbose", "--query").Output()
if err != nil { if err != nil {
log.Panicln(err) return err
} }
err = os.MkdirAll(tmpPath, 0755) err = os.WriteFile(fmt.Sprint(tmpPath, "/", when, "_pacman.txt"), output, 0666)
err = os.WriteFile(fmt.Sprintf("%s/%s", tmpPath, "pre_pacman.txt"), output, 0644)
if err != nil { if err != nil {
log.Panicln(err) return err
} }
defer func() { if strings.Compare(when, "post") != 0 {
if r := recover(); r != nil { err = copy.Copy(pacmanDb, fmt.Sprint(tmpPath, "/", pacmanDb))
err := os.RemoveAll(tmpPath)
if err != nil { if err != nil {
log.Fatal(err) return err
}
} }
err = CopyDir(pacmanDb, fmt.Sprintf("%s/%s", tmpPath, pacmanDb)) current_time := time.Now().Format(time.RFC3339)
pathToZip := fmt.Sprint(tmpPath, "_", current_time, ".zip")
log.Printf("pathToZip is: %s\n", pathToZip)
err = zipIt(pathToZip, tmpPath)
if err != nil { if err != nil {
log.Panicln(err) return err
} }
log.Println("recovered successfully") for _, backup_location := range backup_locations {
} backup_path := fmt.Sprintf("%s/%s/%s_backup_%s.zip", backup_location, when, when, current_time)
}() log.Printf("backup_path is: %s\n", backup_path)
err = copy.Copy(pathToZip, backup_path)
err = CopyDir(pacmanDb, fmt.Sprintf("%s/%s", tmpPath, pacmanDb))
if err != nil { if err != nil {
log.Panicln(err) return err
} }
} }
func postBackup() { return nil
/*
* basic logic for post:
* create new pkg list and delete all lines which are the same as pre
*/
panic("unimplemented")
} }
func main() { func main() {
// backup_dir := parseEnvironment("UPDATE_BACKUP_DIR") if strings.Compare(os.Args[1], "debug") != 0 {
cmdlineArg := os.Args[1] log.SetOutput(io.Discard)
log.Printf("cmdlineArg: %s\n", cmdlineArg)
if cmdlineArg == "pre" {
preBackup()
} else if cmdlineArg == "post" {
postBackup()
} else { } else {
log.Panicf("invalid cmdline argument. supplied value was: %s\n", cmdlineArg) logFile, err := os.OpenFile("/tmp/update.txt", os.O_CREATE | os.O_APPEND | os.O_RDWR, 0666)
if err != nil {
panic(err)
}
defer logFile.Close()
mw := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(mw)
fmt.Println("Logfile is /tmp/update.txt")
}
log.Printf("Cmdline argument is: %s\n", os.Args[1])
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)
log.Printf("backup_locations are: %s\n", backup_locations)
err = backup(os.Args[1])
if err != nil {
fmt.Println("backup failed for the following reason: ", err)
os.Exit(1)
} }
} }

134
copy.go
View File

@ -1,134 +0,0 @@
/* MIT License
*
* Copyright (c) 2017 Roland Singer [roland.singer@desertbit.com]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
// CopyFile copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file. The file mode will be copied from the source and
// the copied data is synced/flushed to stable storage.
func CopyFile(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
if e := out.Close(); e != nil {
err = e
}
}()
_, err = io.Copy(out, in)
if err != nil {
return
}
err = out.Sync()
if err != nil {
return
}
si, err := os.Stat(src)
if err != nil {
return
}
err = os.Chmod(dst, si.Mode())
if err != nil {
return
}
return
}
// CopyDir recursively copies a directory tree, attempting to preserve permissions.
// Source directory must exist, destination directory must *not* exist.
// Symlinks are ignored and skipped.
func CopyDir(src string, dst string) (err error) {
src = filepath.Clean(src)
dst = filepath.Clean(dst)
si, err := os.Stat(src)
if err != nil {
return err
}
if !si.IsDir() {
return fmt.Errorf("source is not a directory")
}
_, err = os.Stat(dst)
if err != nil && !os.IsNotExist(err) {
return
}
if err == nil {
return fmt.Errorf("destination already exists")
}
err = os.MkdirAll(dst, si.Mode())
if err != nil {
return
}
entries, err := ioutil.ReadDir(src)
if err != nil {
return
}
for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
dstPath := filepath.Join(dst, entry.Name())
if entry.IsDir() {
err = CopyDir(srcPath, dstPath)
if err != nil {
return
}
} else {
// Skip symlinks.
if entry.Mode()&os.ModeSymlink != 0 {
continue
}
err = CopyFile(srcPath, dstPath)
if err != nil {
return
}
}
}
return
}

13
go.mod
View File

@ -1,3 +1,14 @@
module update module backup
go 1.23.0 go 1.23.0
require (
github.com/otiai10/copy v1.14.1
github.com/pelletier/go-toml v1.9.5
)
require (
github.com/otiai10/mint v1.6.3 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.31.0 // indirect
)

10
go.sum Normal file
View File

@ -0,0 +1,10 @@
github.com/otiai10/copy v1.14.1 h1:5/7E6qsUMBaH5AnQ0sSLzzTg1oTECmcCmT6lvF45Na8=
github.com/otiai10/copy v1.14.1/go.mod h1:oQwrEDDOci3IM8dJF0d8+jnbfPDllW6vUjNc3DoZm9I=
github.com/otiai10/mint v1.6.3 h1:87qsV/aw1F5as1eH1zS/yqHY85ANKVMgkDrf9rcxbQs=
github.com/otiai10/mint v1.6.3/go.mod h1:MJm72SBthJjz8qhefc4z1PYEieWmy8Bku7CjcAqyUSM=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=

28
legacy/README.md Normal file
View File

@ -0,0 +1,28 @@
# Update
Update script written in Bash for Arch Linux only. Keeps all your official, aur packages and your flatpaks up to date with one simple script. It also backups a list of all your installed packages and flatpaks.
## Installation
```bash
git clone https://gitea.hopeless-cloud.xyz/AustrianToast/update.git && cd update
```
Before installing, please edit the config and configure it to your liking.
Then install using
```bash
make
```
## Usage
```
Usage: update [OPTION]
options:
--help displays this message
--preview shows a preview of which pkg's will be updates
--version prints out the version number
```
## Contributing
Contributions are always welcome!

View File

@ -49,7 +49,7 @@ if [[ ${1:0:2} == \-\- ]]; then
fi fi
check_for_valid_backup_location() { check_for_valid_backup_location() {
if [ ! -d $1 ]; then if [ ! -d "$1" ]; then
echo -e "${IYELLOW}$1 doesn't exist${NO_COLOR}" echo -e "${IYELLOW}$1 doesn't exist${NO_COLOR}"
read -p "Do you want to create the path and continue? [y/N]" input read -p "Do you want to create the path and continue? [y/N]" input
case $input in case $input in
@ -60,8 +60,20 @@ check_for_valid_backup_location() {
fi fi
} }
if [ $BACKUP_LOCATION ]; then check_nfs_mount() {
if ! findmnt -rno SOURCE,TARGET,FSTYPE "$1" | grep -q nfs; then
echo -e "${IYELLOW}$1 is not an active NFS mount.${NO_COLOR}"
read -p "Do you want to continue without an active NFS mount? [y/N] " input
case $input in
[Yy]) return 0 ;;
[Nn]|*) exit 1 ;;
esac
fi
}
if [ "$BACKUP_LOCATION" ]; then
check_for_valid_backup_location "$BACKUP_LOCATION" check_for_valid_backup_location "$BACKUP_LOCATION"
check_nfs_mount "$BACKUP_LOCATION"
else else
echo -e "${IRED}No BACKUP_LOCATION in $HOME/.config/update.conf specified${NO_COLOR}" echo -e "${IRED}No BACKUP_LOCATION in $HOME/.config/update.conf specified${NO_COLOR}"
exit 1 exit 1

View File

@ -1,10 +1,32 @@
install: IGREEN := \033[0;92m
@echo "==> Installing update into /usr/local/bin" NO_COLOR := \033[0m
@sudo install -Dm755 update /usr/local/bin/update
@[[ -f ${HOME}/.config/update.conf ]] || cp update.conf ${HOME}/.config/ help:
@echo "==> Finished." @echo "Usage: make [OPTION]"
@echo "Available options are:"
@echo "help"
@echo "compile"
@echo "install This will also compile"
@echo "uninstall"
compile:
@echo -e "$(IGREEN)==> Compiling backup$(NO_COLOR)"
go build .
install: compile
@echo -e "$(IGREEN)==> Copying the hooks into /etc/pacman.d/hooks$(NO_COLOR)"
[[ -d /etc/pacman.d/hooks ]] || sudo mkdir /etc/pacman.d/hooks
sudo cp pre_backup.hook post_backup.hook /etc/pacman.d/hooks
@echo -e "$(IGREEN)==> Copying backup into /usr/local/bin$(NO_COLOR)"
sudo install -Dm755 backup /usr/local/bin/backup
@echo -e "$(IGREEN)==> Copying the config into /etc$(NO_COLOR)"
[[ -f /etc/update.toml ]] || sudo cp update.toml /etc
@echo -e "$(IGREEN)==> Finished.$(NO_COLOR)"
uninstall: uninstall:
@echo "==> Uninstalling update from /usr/local/bin" @echo -e "$(IGREEN)==> Removing the hooks from /etc/pacman.d/hooks$(NO_COLOR)"
@sudo rm /usr/local/bin/update ${HOME}/.config/update.conf sudo rm /etc/pacman.d/hooks/pre_backup.hook /etc/pacman.d/hooks/post_backup.hook
@echo "==> Finished." @echo -e "$(IGREEN)==> Removing backup into /usr/local/bin$(NO_COLOR)"
sudo rm /usr/local/bin/backup
@echo -e "$(IGREEN)==> /etc/update.toml will remain$(NO_COLOR)"
@echo -e "$(IGREEN)==> Finished.$(NO_COLOR)"

View File

@ -2,10 +2,10 @@
Operation = Install Operation = Install
Operation = Upgrade Operation = Upgrade
Operation = Remove Operation = Remove
Type = Path Type = Package
Target = /usr/local/bin/backup Target = *
[Action] [Action]
Description = Backing up the pacman db... Description = Backing up a list of all packages...
When = PostTransaction When = PostTransaction
Exec = /usr/local/bin/backup post Exec = /usr/local/bin/backup post

View File

@ -2,8 +2,8 @@
Operation = Install Operation = Install
Operation = Upgrade Operation = Upgrade
Operation = Remove Operation = Remove
Type = Path Type = Package
Target = /usr/local/bin/backup Target = *
[Action] [Action]
Description = Backing up the pacman db... Description = Backing up the pacman db...

6
update.toml Normal file
View File

@ -0,0 +1,6 @@
# Config for update
[backup]
# All locations need to be an absolute path
# Don't specify a path multiple times, else the zip will likely be broken
locations = ['/opt']

30
update.zsh Normal file
View File

@ -0,0 +1,30 @@
preview() {
yay -Syy && yay -Qu
[[ -x /usr/bin/flatpak ]] && flatpak remote-ls --updates
}
flatpak-update() {
TMP="/tmp/update"
DATE="$(date +"%Y-%m-%dT%H:%M:%S%:z")"
BACKUP_LOCATION="/opt"
[[ -d $TMP ]] && rm --recursive --force "$TMP"
mkdir --parents "$TMP"/before-backup_"$DATE" "$TMP"/after-backup_"$DATE"
flatpak list --all --show-details > "$TMP"/before-backup_"$DATE"/flatpak-before.txt
tar --create --zstd --file "$TMP"/before-backup_"$DATE".tar.zst "$TMP"/before-backup_"$DATE"
flatpak update --assumeyes
flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
tar --create --zstd --file "$TMP"/after-backup_"$DATE".tar.zst "$TMP"/after-backup_"$DATE"
rsync -a "$TMP"/before-backup_"$DATE".tar.zst "$TMP"/after-backup_"$DATE".tar.zst "$BACKUP_LOCATION"
rm --recursive --force "$TMP"
}
update () {
yay
flatpak-update
}