#!/bin/bash
if [ "$(whoami)" != "root" ]; then
echo "You should be root to run backup."
fi
backupSource="/home/elayn/"
backupTarget="/run/media/elayn/Jet Black/Backup/Rsync-LinuxHome/" #the dir where the backup is saved
now=$(date +"%Y-%m-%d-%H%M") # name of this backup
snapshot="$backupTarget/$now"
cd "$backupTarget"
lastBackup=$(ls | tail -1) # ls prints dirs in alphabetical order, so the last entry should be the most current backup, if it exists
mkdir "$snapshot"
if [ "$lastBackup" == "" ]; then
echo "No previous backups found, creating master..."
rsync --archive --acls --xattrs --hard-links --verbose --compress --exclude={"/home/*/.thumbnails/*","/home/*/.cache/mozilla/*","/home/*/.cache/chromium/*","/home/*/.local/share/Trash/*","/home/*/.gvfs"} "$backupSource" "$snapshot" &> "$snapshot/rsync.log"
elif [ "$lastBackup" != "" ]; then
echo "Using $lastBackup as template..."
rsync --archive --acls --xattrs --hard-links --verbose --compress --exclude={"/home/*/.thumbnails/*","/home/*/.cache/mozilla/*","/home/*/.cache/chromium/*","/home/*/.local/share/Trash/*","/home/*/.gvfs"} --link-dest "$backupTarget/$lastBackup" "$backupSource" "$snapshot" &> "$snapshot/rsync.log"
fi
tail "$snapshot/rsync.log"