dotfiles

regexghost dotfiles and scripts
git clone https://git.regexghost.com/dotfiles.git
Log | Files | Refs | README

backup.sh (1827B)


      1 #!/bin/sh
      2 
      3 # Backup script
      4 
      5 BACKUP_FILE="$XDG_CONFIG_HOME/regexghost/backup.txt"
      6 BACKUP_LOCATION="$HOME/Downloads/BackupMount/MainBackup"
      7 
      8 if [ "$1" = "add" ]; then
      9 	# Get real filename, and remove "/home/<user>"
     10 	[ -e "$2" ] && echo "$(readlink -f "$2")" | sed "s|$HOME||g" >> "$BACKUP_FILE"
     11 elif [ "$1" = "remove" ] || [ "$1" = "rm" ]; then
     12 	# Get real filename, and remove "/home/<user>"
     13 	file="$(readlink -f "$2" | sed "s|$HOME||g")"
     14 	# Check if it's in backup file, if so, remove and overwrite
     15 	if grep -q "^${file}\$" "$BACKUP_FILE"; then
     16 		sed "\:^$file\$:d" "$BACKUP_FILE" > /tmp/backup-tempfile
     17 		mv /tmp/backup-tempfile "$BACKUP_FILE"
     18 	fi
     19 elif [ "$1" = "ls" ]; then
     20 	# Add "~" to the start of each path
     21 	cat "$BACKUP_FILE" | sed 's/^/~/g'
     22 elif [ "$1" = "make" ]; then
     23 	# Log backup
     24 	date +"%y-%m-%d" >> "$XDG_DATA_HOME/regexghost/script-data/backup-history.txt"
     25 	# Copy last backup, to previous slot
     26 	rsync -ar --links --delete --info=progress2 "${BACKUP_LOCATION}/latest/" "${BACKUP_LOCATION}/previous/"
     27 	# Make backup from live system
     28 	rsync -ar --links --delete --info=progress2 --files-from="${BACKUP_FILE}" "$HOME" "${BACKUP_LOCATION}/latest/"
     29 	# Hash all files (on PC) and save to file
     30 	hashFile="${BACKUP_LOCATION}/hashes/$(date +"%y%m%d-%H%M").txt"
     31 	while read -r thing; do
     32 		echo "Hashing: ${HOME}${thing}"
     33 		[ -f "${HOME}${thing}" ] && sha256sum "${HOME}${thing}" >> "$hashFile"
     34 		[ -d "${HOME}${thing}" ] && hashfolder "${HOME}${thing}" >> "$hashFile"
     35 	done  <<EOF
     36 $(cat "$BACKUP_FILE")
     37 EOF
     38 elif [ "$1" = "diff" ]; then
     39 	# Get last 2 hash files and compare
     40 	last2="$(find "${BACKUP_LOCATION}/hashes" | sort | tail -n 2 | head -n 1)"
     41 	last1="$(find "${BACKUP_LOCATION}/hashes" | sort | tail -n 1)"
     42 	# --color=always so that you can grep -v while maintaining colour
     43 	diff --color=always "$last2" "$last1"
     44 fi