Last active
May 22, 2016 16:12
-
-
Save methylene/f2eaebe049c0d06939cb1a9d95ac378b to your computer and use it in GitHub Desktop.
rsync backup script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example
pathnames.txt
: