Skip to content

Instantly share code, notes, and snippets.

@cphyc
Last active April 18, 2020 13:40
Show Gist options
  • Save cphyc/fd533e304883acef9c8c to your computer and use it in GitHub Desktop.
Save cphyc/fd533e304883acef9c8c to your computer and use it in GitHub Desktop.
Synchronize local and remote folders
#!/bin/sh
# License: CC0 1.0 Universal license
# Legal code: https://creativecommons.org/publicdomain/zero/1.0/legalcode
# You can copy, modify, distribute and perform the work, even for commercial purposes,
# all without asking permission.
#
# This script reads an exclusion list (see below for format) in ~/.rsync/exclude
# and transfers ALL files contained in your home to the remote host using rsync as a backend
#
# The script creates on the server a unique exact copy of your HOME each time you call it. To
# save some space, unmodified files are hard-linked on the remote side to the previous snapshot.
#
# If you want to recover your data, just copy back the folder using:
# > rsync -az $REMOTE_HOST:"$REMOTE_HOME/$CURRENT" $HOME
# don't forget to replace these values your customized ones.
#
# Exclude list format: one pattern (accepting wildcards) per line. For example:
# Downloads/
# tmp_*/
persec=$(date "+%Y-%m-%dT%H:%M:%S")
permin=$(date "+%Y-%m-%dT%H:%M")
perhour=$(date "+%Y-%m-%dT%H")
perday=$(date "+%Y-%m-%d")
permonth=$(date "+%Y-%m")
peryear=$(date "+%Y")
#####################################
# Local configuration
#####################################
# Folder to sync
HOME="/home/$USER"
#####################################
# Remote configuration
#####################################
# Fost to sync to. For example '[email protected]'
REMOTE_HOST="[email protected]"
# Folder in which the backup will be stored. For example /home/user/backups
REMOTE_HOME="/home/user/backups"
# Name of the folder containing the last backup. E.g. 'current_bkp' will create
# /home/user/backups/current_bkp for the last backup.
CURRENT="current_bkp"
# Name of the folder containing incomplete backups (e.g. the one being transferred)
INCOMPLETE="bkp/.incomplete"
# Name of the folder containing complete backups (e.g. the one transferred).
# This will determine the frequency of the backups
# Examples of values:
# - one backup per day: 'bkp/back-$perday'
# - one backup per hour: 'bkp/back-$perhour'
# You can also customize it in any way you want, for example:
# bkp/back-$hostname-$perday will create one backup each day for each (local) hostname.
COMPLETE="bkp/back-$perday"
##################################################################################
# Use the following lines if you don't use NetworkManager (e.g. on Max Os X)
# while ! ping -c 1 -W 1 192.168.0.18 ; do
# sleep 1
# count=$((count+1))
# if [ "$count" == "3600" ] && notification erreur
# done
# notification debut synchro
##################################################################################
# Else, use these:
# Wait one hour at most for network
nm-online -q -t 3600
if [ "$?" -eq 0 ]; then
notify-send 'Synchro' 'Beggining synchro'
else
notify-send -u critical 'Synchro' 'Network is not up.'
exit $?
fi
##################################################################################
#####################################
# Synchro. Do not touch if unfamiliar with
#####################################
# 1. Do the synchronisation
# 2. Move the synched folder to its final destination
# and create a link to quickly access it
# The options of rsync are:
# -a: send all the data contained in the $HOME folder (+ metadata)
# -z: use compression
# -P: show progress
# --link-dest: look in this remote folder for files already transferred (preventing
# duplication of files between different synchs)
# --delete: remove files on REMOTE SIDE in the current tranfer that aren't on the local
# side anymore. This doesn't affect previous transfer nor local files.
# --delete-excluded: also remove files that are excluded from transfer
# --exclude-from: tell rsync which pattern to exclude from sync
${RSYNC} -az --stats --human-readable -v \
--delete \
--delete-excluded \
--exclude-from=$HOME/.rsync/exclude \
--link-dest=$REMOTE_HOME/$CURRENT \
--max-size=2.0GiB \
$HOME/ \
$REMOTE_HOST:$REMOTE_HOME/$INCOMPLETE &&\
ssh $REMOTE_HOST \
"mv $REMOTE_HOME/$INCOMPLETE $REMOTE_HOME/$COMPLETE && \
rm -f $REMOTE_HOME/$CURRENT && \
ln -s $REMOTE_HOME/$COMPLETE $REMOTE_HOME/$CURRENT" &&\
notify-send 'Synchro' 'End of synchro' ||\
notify-send 'Synchro' 'An error occured'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment