Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #!/bin/sh
  2. # This is a backup script that takes snapshots of your device
  3. # In a Time Machine like fashion. You can add this to root's
  4. # crontab for regular backups
  5. # Config
  6. readonly SSH_HOST=192.168.1.8
  7. readonly SSH_USER=yigit
  8. readonly SSH_KEY="/home/yigit/.ssh/id_skeleton"
  9. readonly RSYNC_PROFILE="$SSH_USER@$SSH_HOST:~/snapshots"
  10. alias ssh_cmd="ssh -i \"$SSH_KEY\" $SSH_USER@$SSH_HOST"
  11. [ -f "$XDG_CACHE_HOME/dbus_settings" ] && "$XDG_CACHE_HOME/dbus_settings" # Load the dbus settings for current session
  12. displays="$(ps -u $(id -u) -o pid= \
  13. | xargs -I PID -r cat /proc/PID/environ 2> /dev/null \
  14. | tr '\0' '\n' \
  15. | grep ^DISPLAY=: \
  16. | sort -u \
  17. | xargs \
  18. | cut -d ":" -f2)"
  19. notify() {
  20. IFS="\n"
  21. for x in $displays; do
  22. export DISPLAY=$x
  23. notify-send --app-name="$1" "$2"
  24. done ;
  25. }
  26. notify " Periodic backup" "Backup is starting"
  27. DEVICE=tatooine
  28. OPT="-aPh"
  29. SNAP="$RSYNC_PROFILE/$DEVICE/"
  30. date=$(date "+%d.%m.%Y_%I:%M")
  31. # You should enter absolute paths
  32. DIRS="/home/yigit
  33. /etc
  34. /var/lib
  35. /var/spool
  36. /usr/src/linux"
  37. # Check whether backup server is available on network
  38. timeout 3 ssh -i "$SSH_KEY" $SSH_USER@$SSH_HOST id < /dev/null > /dev/null 2> /dev/null || echo "SSH Failed." || exit
  39. # Run rsync to create snapshot
  40. while IFS= read -r DIR
  41. do
  42. LINK="--link-dest=/home/$SSH_USER/snapshots/tatooine/last$DIR/"
  43. ssh -i "$SSH_KEY" $SSH_USER@$SSH_HOST mkdir -p "~/snapshots/$DEVICE/$date$DIR" < /dev/null
  44. rsync --exclude '.cache' "$@" -e "ssh -i $SSH_KEY" $OPT "$LINK" "$DIR/" "${SNAP}$date$DIR/"
  45. done << EOF
  46. $DIRS
  47. EOF
  48. # Remove symlink to previous snapshot
  49. ssh_cmd rm -f "~/snapshots/$DEVICE/last" < /dev/null
  50. # Create new symlink to latest snapshot for the next backup to hardlink
  51. ssh -i "$SSH_KEY" $SSH_USER@$SSH_HOST ln -s "~/snapshots/$DEVICE/$date" "~/snapshots/$DEVICE/last" < /dev/null
  52. pidof slock > /dev/null && {
  53. shutdown -h now
  54. }