Skip to content

Instantly share code, notes, and snippets.

@methylene
Last active May 22, 2016 16:12
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 methylene/f2eaebe049c0d06939cb1a9d95ac378b to your computer and use it in GitHub Desktop.
Save methylene/f2eaebe049c0d06939cb1a9d95ac378b to your computer and use it in GitHub Desktop.
rsync backup script
#!/bin/bash -e
CONF_PATHNAMES=${HOME}/.config/collect/pathnames.txt
function warn { echo -e " \e[93m[$1]\e[0m"; }
function printDone { echo -e " \e[92m[DONE]\e[0m"; }
# Backup a path
function performBackup {
# Use absolute path
if [[ $1 == /* ]]; then
local SOURCE=$1
else
local SOURCE=${HOME}/${1}
fi
local TARGET=${BACKUP_DIR}${SOURCE}
echo -n Backup $SOURCE ...
if [[ ! -e $SOURCE ]]; then
warn "NOT FOUND"
return
fi
if [[ -d $SOURCE ]]; then
if [[ -f ${SOURCE}/.rsyncexcludes ]]; then
rsync -r --exclude-from ${SOURCE}/.rsyncexcludes ${SOURCE}/ $TARGET
printDone
else
rsync -r ${SOURCE}/ $TARGET
printDone
fi
elif [[ -f $SOURCE ]]; then
mkdir -p `dirname $TARGET`
rsync $SOURCE $TARGET
printDone
else
warn "UNKNOWN FILE TYPE"
fi
}
# Backup dir must be specified
if [[ $# -ne 1 ]]; then
echo "Argument: backup dir"
exit
fi
# Configuration must exist
touch $CONF_PATHNAMES || exit 1
# Make backup dir, fail if exists
mkdir $1 || exit 1
# Absolute path
BACKUP_DIR=`cd $1; pwd`
echo "Using backup dir: $BACKUP_DIR"
# Backup self
performBackup $0
# Backup config file
performBackup $CONF_PATHNAMES
# Backup everything else
for P in `cat $CONF_PATHNAMES`; do
performBackup $P
done
echo "Done"
@methylene
Copy link
Author

Example pathnames.txt:

bin
scripts
Documents
Pictures
.emacs.d/init.el
.irssi/config
.i3/config
.gitconfig
.ssh
.bashrc
.bash_history
workspace
/usr/local/bin/luksOpen
/usr/local/bin/luksClose
/etc/hosts
/etc/udev/rules.d/51-android.rules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment