Browse Source

Backup script

main
Yigit Colakoglu 4 years ago
parent
commit
7065340a52
2 changed files with 42 additions and 1 deletions
  1. +41
    -0
      .local/bin/backup
  2. +1
    -1
      .profile

+ 41
- 0
.local/bin/backup View File

@ -0,0 +1,41 @@
#!/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.8
readonly SSH_USER=yigit
readonly SSH_KEY="/home/yigit/.ssh/id_skeleton"
readonly RSYNC_PROFILE="$SSH_USER@$SSH_HOST:~/snapshots"
DEVICE=tatooine
OPT="-aPh"
LINK="--link-dest=$RSYNC_PROFILE/$DEVICE/last/"
SNAP="$RSYNC_PROFILE/$DEVICE/"
LAST="$RSYNC_PROFILE/$DEVICE/last"
date=$(date "+%Y-%b-%d:_%T")
# You should enter absolute paths
DIRS="/home/yigit
/etc
/var/lib
/var/spool"
# 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
# Run rsync to create snapshot
while IFS= read -r DIR
do
DEST=$(echo "$DIR" | sed "s/\/[^\/]*$//g")
ssh -i "$SSH_KEY" $SSH_USER@$SSH_HOST mkdir -p "~/snapshots/$DEVICE/$date$DEST" < /dev/null
rsync "$@" -e "ssh -i $SSH_KEY" $OPT "$LINK" "$DIR" "${SNAP}$date$DEST"
done << EOF
$DIRS
EOF
# Remove symlink to previous snapshot
rm -f "$LAST"
# Create new symlink to latest snapshot for the next backup to hardlink
ln -s "${SNAP}$date" "$LAST"

+ 1
- 1
.profile View File

@ -90,7 +90,7 @@ fi
# Start xinit if logged in from tty1
if [ "$DISPLAY" = "" ] && [ "$(tty)" = /dev/tty1 ]; then
if [ "$DBUS_SESSION_BUS_ADDRESS" = "" ] && [ ! $(command -v dbus-launch) = "" ]; then
if [ "$DBUS_SESSION_BUS_ADDRESS" = "" ] && [ ! $(command -v dbus-run-session) = "" ]; then
exec dbus-run-session xinit 2> $XDG_RUNTIME_DIR/xinit.err > $XDG_RUNTIME_DIR/xinit
else
exec xinit 2> $XDG_RUNTIME_DIR/xinit.err > $XDG_RUNTIME_DIR/xinit


Loading…
Cancel
Save