|
#!/bin/sh
|
|
|
|
# This is a backup script that takes snapshots of your device
|
|
# In a Time Machine like fashion. You can add this to root's
|
|
# crontab for regular backups
|
|
|
|
# Config
|
|
readonly SSH_HOST=192.168.1.34
|
|
readonly SSH_USER=pi
|
|
readonly SSH_KEY="/home/yigit/.ssh/id_skeleton"
|
|
readonly BACKUP_DESTINATION="~/snapshots/backups"
|
|
readonly RSYNC_PROFILE="$SSH_USER@$SSH_HOST:$BACKUP_DESTINATION"
|
|
|
|
[ $(id -u) != "0" ] && printf "This program must be run as root!\n" && exit
|
|
|
|
alias ssh_cmd="ssh -i \"$SSH_KEY\" $SSH_USER@$SSH_HOST"
|
|
[ -f "$XDG_CACHE_HOME/dbus_settings" ] && "$XDG_CACHE_HOME/dbus_settings" # Load the dbus settings for current session
|
|
|
|
|
|
displays="$(ps -u $(id -u) -o pid= \
|
|
| xargs -I PID -r cat /proc/PID/environ 2> /dev/null \
|
|
| tr '\0' '\n' \
|
|
| grep ^DISPLAY=: \
|
|
| sort -u \
|
|
| xargs \
|
|
| cut -d ":" -f2)"
|
|
notify() {
|
|
IFS="\n"
|
|
for x in $displays; do
|
|
export DISPLAY=$x
|
|
notify-send --app-name="$1" "$2"
|
|
done ;
|
|
}
|
|
|
|
notify " Periodic backup" "Backup is starting"
|
|
|
|
DEVICE=tatooine
|
|
OPT="-aPh"
|
|
SNAP="$RSYNC_PROFILE/$DEVICE/"
|
|
date=$(date "+%d.%m.%Y_%I:%M")
|
|
older=$(date --date="4 days ago" "+%d.%m.%Y_%I:%M")
|
|
# You should enter absolute paths
|
|
DIRS="/home/yigit
|
|
/etc
|
|
/var/lib
|
|
/var/spool
|
|
/usr/src/linux"
|
|
|
|
# Check whether backup server is available on network
|
|
timeout 3 ssh -i "$SSH_KEY" $SSH_USER@$SSH_HOST id < /dev/null > /dev/null 2> /dev/null || echo "SSH Failed." || exit
|
|
ssh -i "$SSH_KEY" $SSH_USER@$SSH_HOST rm -rf "$BACKUP_DESTINATION/$DEVICE/$older" > /dev/null 2> /dev/null
|
|
|
|
# Run rsync to create snapshot
|
|
while IFS= read -r DIR
|
|
do
|
|
LINK="--link-dest=/home/$SSH_USER/snapshots/tatooine/last$DIR/"
|
|
ssh -i "$SSH_KEY" $SSH_USER@$SSH_HOST mkdir -p "$BACKUP_DESTINATION/$DEVICE/$date$DIR" < /dev/null
|
|
rsync --exclude '.cache' "$@" -e "ssh -i $SSH_KEY" $OPT "$LINK" "$DIR/" "${SNAP}$date$DIR/"
|
|
done << EOF
|
|
$DIRS
|
|
EOF
|
|
|
|
# Remove symlink to previous snapshot
|
|
ssh_cmd rm -f "$BACKUP_DESTINATION/$DEVICE/last" < /dev/null
|
|
|
|
# Create new symlink to latest snapshot for the next backup to hardlink
|
|
ssh -i "$SSH_KEY" $SSH_USER@$SSH_HOST ln -s "$BACKUP_DESTINATION/$DEVICE/$date" "$BACKUP_DESTINATION/$DEVICE/last" < /dev/null
|
|
|
|
# pidof slock > /dev/null && { # Shutdown if screen inhibition is enabled
|
|
# shutdown -h now
|
|
# }
|