Compare commits
	
		
			16 Commits
		
	
	
		
			0cf266dd87
			...
			c_rewrite
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 8024e86962 | |||
| 88742c2ea5 | |||
| d101e186c4 | |||
| 2d881a0d34 | |||
| cb735a01a1 | |||
| e3c9924fbc | |||
| 3645f7f30c | |||
| 66ca74441d | |||
| 7bca34cc89 | |||
| e4ab48fb22 | |||
| 67c7f48cd7 | |||
| e2d4253050 | |||
| 81b07991aa | |||
| 452b0e92e2 | |||
| f89e7112a4 | |||
| b28009f83b | 
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| # Build artifacts | ||||
| update | ||||
							
								
								
									
										29
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								README.md
									
									
									
									
									
								
							| @@ -1,28 +1,33 @@ | ||||
| # 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  | ||||
|  | ||||
| ```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 | ||||
| ```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`. | ||||
|  | ||||
| ``` | ||||
| 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 | ||||
| ``` | ||||
| The function `flatpak-update` contains a variable called `BACKUP_LOCATION`.<br> | ||||
| Please change this path to your desired backup location. | ||||
|  | ||||
| ## Contributing   | ||||
| Contributions are always welcome! | ||||
|   | ||||
							
								
								
									
										120
									
								
								backup.go
									
									
									
									
									
								
							
							
						
						
									
										120
									
								
								backup.go
									
									
									
									
									
								
							| @@ -1,120 +0,0 @@ | ||||
| package main | ||||
|  | ||||
| import ( | ||||
| 	"bufio" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"io/fs" | ||||
| 	"log" | ||||
| 	"os" | ||||
| 	"os/exec" | ||||
| 	"strings" | ||||
| ) | ||||
|  | ||||
| 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 | ||||
| } | ||||
|  | ||||
| func parsePacmanConf() string { | ||||
| 	var path string | ||||
|  | ||||
| 	file, err := os.Open("/etc/pacman.conf") | ||||
| 	if err != nil { | ||||
| 		log.Panicln(err) | ||||
| 	} | ||||
| 	defer func() { | ||||
| 		if err := file.Close(); err != nil { | ||||
| 			log.Panicln(err) | ||||
| 		} | ||||
| 	}() | ||||
|  | ||||
| 	scanner := bufio.NewScanner(file) | ||||
| 	for scanner.Scan() { | ||||
| 		line := scanner.Text() | ||||
| 		if !strings.Contains(line, "DBPath") { | ||||
| 			continue | ||||
| 		} | ||||
| 		path = strings.TrimSpace(strings.Split(line, "=")[1]) | ||||
| 		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) | ||||
| 	} | ||||
|  | ||||
| 	return fmt.Sprintf("%s%s", path, "local") | ||||
| } | ||||
|  | ||||
| func preBackup() { | ||||
| 	pacmanDb := parsePacmanConf() | ||||
| 	log.Printf("pacmanDb: %s\n", pacmanDb) | ||||
| 	tmpPath := "/tmp/update/pre_backup" | ||||
|  | ||||
| 	output, err := exec.Command("pacman",  "--verbose", "--query").Output() | ||||
| 	if err != nil { | ||||
| 		log.Panicln(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) | ||||
| 	} | ||||
|  | ||||
| 	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") | ||||
| 		} | ||||
| 	}() | ||||
|  | ||||
| 	err = CopyDir(pacmanDb, fmt.Sprintf("%s/%s", tmpPath, pacmanDb)) | ||||
| 	if err != nil { | ||||
| 		log.Panicln(err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func postBackup() { | ||||
| 	/* | ||||
| 	 * basic logic for post: | ||||
| 	 * create new pkg list and delete all lines which are the same as pre | ||||
| 	 */ | ||||
| 	panic("unimplemented") | ||||
| } | ||||
|  | ||||
| func main() { | ||||
| 	// backup_dir := parseEnvironment("UPDATE_BACKUP_DIR") | ||||
| 	cmdlineArg := os.Args[1] | ||||
| 	log.Printf("cmdlineArg: %s\n", cmdlineArg) | ||||
|  | ||||
| 	if cmdlineArg == "pre" { | ||||
| 		preBackup() | ||||
| 	} else if cmdlineArg == "post" { | ||||
| 		postBackup() | ||||
| 	} else { | ||||
| 		log.Panicf("invalid cmdline argument. supplied value was: %s\n", cmdlineArg) | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										134
									
								
								copy.go
									
									
									
									
									
								
							
							
						
						
									
										134
									
								
								copy.go
									
									
									
									
									
								
							| @@ -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 | ||||
| } | ||||
							
								
								
									
										10
									
								
								justfile
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								justfile
									
									
									
									
									
								
							| @@ -1,10 +0,0 @@ | ||||
| install: | ||||
| 	@echo "==> Installing update into /usr/local/bin" | ||||
| 	@sudo install -Dm755 update /usr/local/bin/update | ||||
| 	@[[ -f ${HOME}/.config/update.conf ]] || cp update.conf ${HOME}/.config/ | ||||
| 	@echo "==> Finished." | ||||
|  | ||||
| uninstall: | ||||
| 	@echo "==> Uninstalling update from /usr/local/bin" | ||||
| 	@sudo rm /usr/local/bin/update ${HOME}/.config/update.conf | ||||
| 	@echo "==> Finished." | ||||
							
								
								
									
										10
									
								
								makefile
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								makefile
									
									
									
									
									
								
							| @@ -1,10 +0,0 @@ | ||||
| install: | ||||
| 	@echo "==> Installing update into /usr/local/bin" | ||||
| 	@sudo install -Dm755 update /usr/local/bin/update | ||||
| 	@[[ -f ${HOME}/.config/update.conf ]] || cp update.conf ${HOME}/.config/ | ||||
| 	@echo "==> Finished." | ||||
|  | ||||
| uninstall: | ||||
| 	@echo "==> Uninstalling update from /usr/local/bin" | ||||
| 	@sudo rm /usr/local/bin/update ${HOME}/.config/update.conf | ||||
| 	@echo "==> Finished." | ||||
| @@ -1,11 +0,0 @@ | ||||
| [Trigger] | ||||
| Operation = Install | ||||
| Operation = Upgrade | ||||
| Operation = Remove | ||||
| Type = Path | ||||
| Target = /usr/local/bin/backup | ||||
|  | ||||
| [Action] | ||||
| Description = Backing up the pacman db... | ||||
| When = PostTransaction | ||||
| Exec = /usr/local/bin/backup post | ||||
| @@ -1,12 +0,0 @@ | ||||
| [Trigger] | ||||
| Operation = Install | ||||
| Operation = Upgrade | ||||
| Operation = Remove | ||||
| Type = Path | ||||
| Target = /usr/local/bin/backup | ||||
|  | ||||
| [Action] | ||||
| Description = Backing up the pacman db... | ||||
| When = PreTransaction | ||||
| Exec = /usr/local/bin/backup pre | ||||
| AbortOnFail | ||||
							
								
								
									
										141
									
								
								update
									
									
									
									
									
								
							
							
						
						
									
										141
									
								
								update
									
									
									
									
									
								
							| @@ -1,141 +0,0 @@ | ||||
| #!/usr/bin/bash | ||||
|  | ||||
| VERSION="3.2" | ||||
| IGREEN="\033[0;92m"     # Intense Green | ||||
| IYELLOW="\033[0;93m"    # Intense Yellow | ||||
| IRED='\033[0;91m'       # Red | ||||
| NO_COLOR="\033[0m"      # Text Reset | ||||
| TMP="/tmp/update" | ||||
| DATE="$(date +"%Y-%m-%dT%H:%M:%S%:z")"  # RFC 3339 date-time https://datatracker.ietf.org/doc/html/rfc3339#section-5.6 | ||||
|  | ||||
| trap interrupt_function INT | ||||
|  | ||||
| interrupt_function() { | ||||
|     echo -e "${IRED}\nInterrupt has been detected${NO_COLOR}" | ||||
|     if [ -f /var/lib/pacman/db.lck ]; then | ||||
|         echo -e "${IRED}Trying to unlock the pacman db. Please enter your password.${NO_COLOR}" | ||||
|         su --command="rm --force /var/lib/pacman/db.lck" | ||||
|     fi | ||||
|     [[ -d $TMP ]] && rm --recursive --force "$TMP" | ||||
|     [[ -f "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new ]] && rm --force "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new | ||||
|     [[ -f "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new ]] && rm --force "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new | ||||
|     exit 1 | ||||
| } | ||||
|  | ||||
| help() { | ||||
|     echo "Usage: update [OPTION]" | ||||
|     echo "options:" | ||||
|     echo "--help         displays this message" | ||||
|     echo "--preview      shows a preview of pkg's and flatpaks which can be updated" | ||||
|     echo "--version      prints out the version number" | ||||
| } | ||||
|  | ||||
| source "$HOME"/.config/update.conf || echo -e "${IRED}No config found${NO_COLOR}" || exit 1 | ||||
|  | ||||
| if [[ ${1:0:2} == \-\- ]]; then | ||||
|     case "${1:2}" in | ||||
|         help) | ||||
|             help ;; | ||||
|         preview) | ||||
|             "$PACMAN_WRAPPER" -Syy | ||||
|             "$PACMAN_WRAPPER" --query --upgrades | ||||
|             [[ -x /usr/bin/flatpak ]] && flatpak remote-ls --updates ;; | ||||
|         version) | ||||
|             echo "$VERSION" ;; | ||||
|         ?) | ||||
|             help; exit 1;; | ||||
|     esac | ||||
|     exit 0 | ||||
| fi | ||||
|  | ||||
| check_for_valid_backup_location() { | ||||
|     if [ ! -d $1 ]; then | ||||
|         echo -e "${IYELLOW}$1 doesn't exist${NO_COLOR}" | ||||
|         read -p "Do you want to create the path and continue? [y/N]" input | ||||
|         case $input in | ||||
|             [Yy]) mkdir --parents "$1" ;; | ||||
|             [Nn]) exit 0;; | ||||
|             * ) exit 0 ;; | ||||
|         esac | ||||
|     fi | ||||
| } | ||||
|  | ||||
| if [ $BACKUP_LOCATION ]; then | ||||
|     check_for_valid_backup_location "$BACKUP_LOCATION" | ||||
| else | ||||
|     echo -e "${IRED}No BACKUP_LOCATION in $HOME/.config/update.conf specified${NO_COLOR}" | ||||
|     exit 1 | ||||
| fi | ||||
| [[ $SECONDARY_BACKUP_LOCATION ]] && check_for_valid_backup_location "$SECONDARY_BACKUP_LOCATION" | ||||
|  | ||||
| lock_pacman_db() { | ||||
|     if [ -f /var/lib/pacman/db.lck ]; then | ||||
|         echo -e "${IYELLOW}->${NO_COLOR} /var/lib/pacman/db.lck exists" | ||||
|         echo -e "${IYELLOW}->${NO_COLOR} there might be another instance of pacman running. exiting..." | ||||
|         exit 1 | ||||
|     fi | ||||
|  | ||||
|     echo -e "${IYELLOW}Trying to lock the pacman db. Please enter your password.${NO_COLOR}" | ||||
|     su --command="touch /var/lib/pacman/db.lck" | ||||
| } | ||||
|  | ||||
| unlock_pacman_db() { | ||||
|     echo -e "${IYELLOW}Trying to unlock the pacman db. Please enter your password.${NO_COLOR}" | ||||
|     su --command="rm --force /var/lib/pacman/db.lck" | ||||
| } | ||||
|  | ||||
| delete_oldest_backup() { | ||||
|     if [[ $(find $BACKUP_LOCATION -name '*$1*' -exec printf %c {} + | wc -c) -ge $BACKUP_AMOUNT ]]; then | ||||
|         rm --force $(find "$BACKUP_LOCATION" -name '*$1*' | sort -rn | head -1) | ||||
|     fi | ||||
| } | ||||
|  | ||||
| before_backup() { | ||||
|     lock_pacman_db | ||||
|  | ||||
|     [[ -d $TMP ]] && rm --recursive --force "$TMP" | ||||
|     mkdir --parents "$TMP"/before-backup_"$DATE" "$TMP"/after-backup_"$DATE" | ||||
|  | ||||
|     pacman --verbose --query > "$TMP"/before-backup_"$DATE"/pacman-before.txt | ||||
|     [[ -x /usr/bin/flatpak ]] && flatpak list --all --show-details > "$TMP"/before-backup_"$DATE"/flatpak-before.txt | ||||
|  | ||||
|     tar --create --zstd --file "$TMP"/before-backup_"$DATE".tar.zst.new "$TMP"/before-backup_"$DATE" /var/lib/pacman/local | ||||
|     cp "$TMP"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION" | ||||
|     [[ $SECONDARY_BACKUP_LOCATION ]] && cp "$TMP"/before-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION" | ||||
|  | ||||
|     unlock_pacman_db | ||||
| } | ||||
|  | ||||
| after_backup() { | ||||
|     lock_pacman_db | ||||
|  | ||||
|     pacman --verbose --query > "$TMP"/after-backup_"$DATE"/pacman-after.txt | ||||
|     [[ -x /usr/bin/flatpak ]] && flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt | ||||
|     tar --create --zstd --file "$TMP"/after-backup_"$DATE".tar.zst.new "$TMP"/after-backup_"$DATE" | ||||
|  | ||||
|     cp "$TMP"/after-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION" | ||||
|     [[ $SECONDARY_BACKUP_LOCATION ]] && cp "$TMP"/after-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION" | ||||
|  | ||||
|     delete_oldest_backup after-backup | ||||
|     mv "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst | ||||
|     [[ $SECONDARY_BACKUP_LOCATION ]] && mv "$SECONDARY_BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION"/after-backup_"$DATE".tar.zst | ||||
|  | ||||
|     delete_oldest_backup before-backup | ||||
|     mv "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst | ||||
|     [[ $SECONDARY_BACKUP_LOCATION ]] && mv "$SECONDARY_BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION"/before-backup_"$DATE".tar.zst | ||||
|  | ||||
|     unlock_pacman_db | ||||
|     rm --recursive --force "$TMP" | ||||
| } | ||||
|  | ||||
| update() { | ||||
|     "$PACMAN_WRAPPER" | ||||
|     if [[ -x /usr/bin/flatpak ]]; then | ||||
|         flatpak update --assumeyes | ||||
|         flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt | ||||
|     fi | ||||
| } | ||||
|  | ||||
| before_backup && echo -e "${IGREEN}before-backup complete${NO_COLOR}" | ||||
| update | ||||
| after_backup && echo -e "${IGREEN}after-backup complete${NO_COLOR}" | ||||
							
								
								
									
										83
									
								
								update.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								update.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,83 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #include <errno.h> | ||||
|  | ||||
| int command_builder(char *command, char **args) { | ||||
|     char *tmp_command = malloc(strlen(command)); | ||||
|     memcpy(tmp_command, command, strlen(command)); | ||||
|     char *tmp_token; | ||||
|     size_t index = 0; | ||||
|  | ||||
|     if (tmp_command == NULL) { return 1; } | ||||
|  | ||||
|     tmp_token = strtok(tmp_command, " "); | ||||
|     while(tmp_token != NULL) { | ||||
|         args[index] = malloc(strlen(tmp_token)); | ||||
|         memcpy(args[index], tmp_token, strlen(tmp_token)); | ||||
|         tmp_token = strtok(NULL, " "); | ||||
|         index++; | ||||
|     } | ||||
|     free(tmp_command); | ||||
|     args[index++] = NULL; | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| int run_cmd_and_wait(char *command) { | ||||
|     char **args = malloc(strlen(command)); | ||||
|     int ret = command_builder(command, args); | ||||
|     if (ret != 0) { | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     signed int pid = fork(); | ||||
|     int status; | ||||
|  | ||||
|     if (pid == -1) { | ||||
|         fprintf(stderr, "Couldn't fork. Error = %s\n", strerror(errno)); | ||||
|         return 1; | ||||
|     } else if (pid > 0) { | ||||
|         waitpid(pid, &status, 0); | ||||
|     } else { | ||||
|         execvp(args[0], args); | ||||
|     } | ||||
|     free(args); | ||||
|  | ||||
|     return status; | ||||
| } | ||||
|  | ||||
| int main() { | ||||
|     FILE *os_release = fopen("/etc/os-release", "r"); | ||||
|     if (!os_release) { | ||||
|         fprintf(stderr, "Couldn't open os-release. Error = %s\n", strerror(errno)); | ||||
|     } | ||||
|     char buffer[1024]; | ||||
|     while (fgets(buffer, sizeof buffer, os_release)) { | ||||
|         if (strncmp(buffer, "NAME=", 5) == 0) { | ||||
|             memmove(buffer, buffer + 6, (sizeof buffer) - 6); | ||||
|             buffer[strcspn(buffer, "\"")] = '\0'; | ||||
|             break; | ||||
|         } | ||||
|     } | ||||
|     int ret; | ||||
|     char *args[100]; | ||||
|     if (strcmp(buffer, "EndeavourOS") == 0) { | ||||
|         ret = run_cmd_and_wait("yay"); | ||||
|     } else if (strcmp(buffer, "Debian") == 0) { | ||||
|         ret = run_cmd_and_wait("apt update"); | ||||
|         if (ret != 0) { | ||||
|             return ret; | ||||
|         } | ||||
|         ret = run_cmd_and_wait("apt upgrade"); | ||||
|     } else if (strcmp(buffer, "FreeBSD") == 0) { | ||||
|         ret = run_cmd_and_wait("pkg update"); | ||||
|         if (ret != 0) { | ||||
|             return ret; | ||||
|         } | ||||
|         ret = run_cmd_and_wait("pkg upgrade"); | ||||
|     } else { | ||||
|         fprintf(stderr, "OS %s not suported\n", buffer); | ||||
|         return 1; | ||||
|     } | ||||
|     return ret; | ||||
| } | ||||
							
								
								
									
										11
									
								
								update.conf
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								update.conf
									
									
									
									
									
								
							| @@ -1,11 +0,0 @@ | ||||
| # backup to this folder | ||||
| #BACKUP_LOCATION=~ | ||||
|  | ||||
| # optional secondary backup loaction | ||||
| #SECONDARY_BACKUP_LOCATION=~ | ||||
|  | ||||
| # use this pacman-wrapper | ||||
| PACMAN_WRAPPER=yay | ||||
|  | ||||
| # how many different backups to keep | ||||
| BACKUP_AMOUNT=10 | ||||
		Reference in New Issue
	
	Block a user