Last active
September 24, 2016 11:11
-
-
Save kbhaines/ac69dd5f5fcf15cf9bd0720f7bc64bd6 to your computer and use it in GitHub Desktop.
Simple backup script, using hard-links and rsync to efficiently backup entire directories
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/sh | |
set -e | |
usage() { | |
echo "$0 <backup-media> <source-dir> [<source-dir>...]" | |
echo "where backup-media = directory to backup to" | |
echo "and source-dir = one or more directories to backup" | |
} | |
get_last_id() { | |
last_id=`ls -d [0-9]*|tail -1` | |
if [ -z "$last_id" ];then | |
last_id=1000 | |
mkdir 1000 | |
fi | |
echo $last_id | |
} | |
if [ $# -lt 2 ];then | |
usage | |
exit | |
fi | |
BACKUP_MEDIA=$1 | |
shift 1 | |
BACKUP_SRCS=$* | |
if [ ! -d "$BACKUP_MEDIA" ];then | |
echo "$BACKUP_MEDIA is not a directory" | |
exit 1 | |
fi | |
cd "$BACKUP_MEDIA" | |
# Last backup id | |
last_id=`get_last_id` | |
next_id=$(($last_id + 1)) | |
echo "Cloning last backup $last_id to $next_id" | |
cp -al $last_id $next_id | |
touch $next_id | |
for src in $BACKUP_SRCS; do | |
target="$BACKUP_MEDIA/$next_id$src" | |
echo "Backup $src to $target" | |
[ -d "$target" ] || mkdir -p "$target" | |
rsync --delete -F -a "$src"/ "$target"/ | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment