Last active
August 5, 2018 23:26
-
-
Save ei-grad/7610406 to your computer and use it in GitHub Desktop.
TRivial INcremental bacKUP script (MOVED TO REPOSITORY)
This file contains 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 | |
# | |
# trinkup - TRivial INcremental bacKUP script | |
# | |
# Уж 200 раз твердили Сене: | |
# Хардлинк спасет от удаленья! | |
# А кто создать его поможет? | |
# Crontab и man, тупая рожа! | |
# | |
# (c) linux.org.ru, no-dashi | |
# | |
# Licensed under DWTFYWWI license, but recommended to be used before drinkup. | |
# | |
set -e | |
if ! which rsync >/dev/null; then | |
echo "rsync not found!" >&2 | |
exit 1 | |
fi | |
USAGE=" | |
usage: | |
$0 <rsync_source> <backups_directory> <number_of_backups> [rsync_args] | |
Keep calm and drink vodka! | |
" | |
SRC="$1" | |
BACKUPS="$2" | |
ROTATE="$3" | |
if [ -z "$SRC" ] || [ -z "$BACKUPS" ] || ! [ -n "$ROTATE" -a "$ROTATE" -gt 1 ] ; then | |
echo -e "$USAGE" >&2 | |
exit 1 | |
fi | |
shift 3 | |
if ! [ -d "$BACKUPS" ] ; then | |
echo "'$BACKUPS' is not a directory!" >&2 | |
exit 1 | |
fi | |
WORKDIR="$(echo "$BACKUPS/`basename \"$SRC\"`.`date +%s`" | sed 's,/\+,/,g')" | |
echo "Using '$WORKDIR' as backup destination." | |
if [ -d `ls -rtd \"$BACKUPS/$(basename "$SRC")\"\.[0-9]* 2>/dev/null | head -n 1` ] ; then | |
OLDEST="`ls -rtd \"$BACKUPS/$(basename "$SRC")\"\.[0-9]* | sed 's,/\+,/,g' | awk NR==$ROTATE`" | |
NEWEST="`ls -rtd \"$BACKUPS/$(basename "$SRC")\"\.[0-9]* | sed 's,/\+,/,g' | head -n 1`" | |
fi | |
if [ -d "$OLDEST" ] ; then | |
echo "Reusing $OLDEST to reduce amount of disk operations." | |
mv "$OLDEST" "$WORKDIR" | |
fi | |
if [ -d "$NEWEST" ] ; then | |
echo "Syncing with $NEWEST..." | |
cp -laf "$NEWEST/." "$WORKDIR" | |
fi | |
echo "Running rsync..." | |
rsync -a --delete $* "$SRC" "$WORKDIR" | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I removed the 't' flag from the ls commands on lines 50-52. Backups are time stamped, so ordering by name is ok. Also if a backup is modified by accident, the order is still maintained. Fixes an issue I had with a backup that was modified by accident.