Skip to content

Instantly share code, notes, and snippets.

@ErikThorsell
Created October 16, 2018 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ErikThorsell/09a5ee8b5544bb9fbdd97e6a1ca22973 to your computer and use it in GitHub Desktop.
Save ErikThorsell/09a5ee8b5544bb9fbdd97e6a1ca22973 to your computer and use it in GitHub Desktop.
#!/bin/sh
############################### Backup Script #################################
# Backup script invoked by systemd (timer) to ensure hourly backups. #
# DATE is not appended to the file. This is because the server used as TRG #
# snapshots the data set and saves: hourly snapshots for a day #
# daily snapshots for a weekly #
# monthly snapshots for a year #
# #
# At the moment the script starts of by checking that the laptop is charging; # #
# The script also checks that the computer have internet access. If both are #
# satisfied, we sync away. #
###############################################################################
## Variables
PING="8.8.8.8"
DATE=`date "+%Y-%m-%d"`
TIME=`date "+%H:%M"`
SRC="/"
TRG="backup:/mnt/volume1/backup/laptop"
EXC="/home/erik/scripts/exclude_laptop_rsync.txt"
OPT="-aXz --partial --delete --delete-excluded --exclude-from=$EXC --chmod=o-t --bwlimit=5000"
LOG="/var/log/laptop_backup"
#DOCKED=$(cat /sys/devices/platform/dock.0/docked) # can be used to see if laptop is in docking station
CHARGING=$(acpi -a)
NLINES=$(sed -n '$=' $LOG)
if [ "$CHARGING" == "Adapter 0: on-line" ]; then
ping -c 1 $PING > /dev/null
if [ "$?" == 0 ]; then # verify that ping worked
rsync $OPT $SRC $TRG
if [ "$?" == 0 ]; then # verify that rsync worked
if [ "$NLINES" == 1000 ]; then
echo "Old log file got too long. Restarting log file." > $LOG
fi
echo "Incremental backup of laptop taken: $DATE at $TIME" >> $LOG
else
echo "Rsync result != 0, $DATE at $TIME" >> $LOG
notify-send "Somenthing went wrong with the laptop backup script!" # send msg to dunst
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment