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.

71 lines
2.2 KiB

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