Skip to content

Instantly share code, notes, and snippets.

@Qalthos
Last active August 29, 2015 14:09
Show Gist options
  • Save Qalthos/9c38ccaf0743b772e7ea to your computer and use it in GitHub Desktop.
Save Qalthos/9c38ccaf0743b772e7ea to your computer and use it in GitHub Desktop.
Backup all the things
#!/bin/bash
BACKUP=/data/backup/$(hostname)
DATE="$(date '+%Y-%m-%d--%H-%M')"
OPTS='-amxHAX --partial --delete --delete-excluded --exclude-from=.exclude --rsh=ssh --link-dest=../current'
IGNORE_ERROR=(0 23 24)
if [ "$1" == "-v" ]; then
OPTS="$OPTS -vh --progress"
fi
pushd ~ > /dev/null
rsync $OPTS /home/nate linkybook.com:$BACKUP/temp-backup
if [[ $? =~ $IGNORE_ERROR ]]; then
ssh linkybook.com "mv $BACKUP/temp-backup $BACKUP/hourly/$DATE && \
rm $BACKUP/current && ln -s $BACKUP/hourly/$DATE $BACKUP/current"
fi
popd > /dev/null
#!/bin/zsh
# Upgrade hourly backups
for file in /data/backup/greenmagic/hourly/*; do
if [ ! -e /data/backup/greenmagic/monthly/${${file##*/}:0: -10} ]; then
short_file=${${file##*/}:0: -10}
cp -al $file /data/backup/greenmagic/monthly/$short_file
elif [ ! -e /data/backup/greenmagic/weekly/${${file##*/}:0: -8} ]; then
short_file=${${file##*/}:0: -8}
cp -al $file /data/backup/greenmagic/weekly/$short_file
elif [ ! -e /data/backup/greenmagic/daily/${${file##*/}:0: -7} ]; then
short_file=${${file##*/}:0: -7}
cp -al $file /data/backup/greenmagic/daily/$short_file
fi
done
ls -d /data/backup/greenmagic/hourly/* | head -n -49 | xargs rm -rf
ls -d /data/backup/greenmagic/daily/* | head -n -21 | xargs rm -rf
ls -d /data/backup/greenmagic/weekly/* | head -n -9 | xargs rm -rf
ls -d /data/backup/greenmagic/monthly/* | head -n -25 | xargs rm -rf
# Keep only 24 backups total from Fourecks
chmod u+rwX -R /data/backup/Fourecks
ls -d /data/backup/Fourecks/* | head -n -25 | xargs rm -rf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment