Skip to content

Instantly share code, notes, and snippets.

@tifletcher
Last active December 16, 2015 07:39
Show Gist options
  • Save tifletcher/5399923 to your computer and use it in GitHub Desktop.
Save tifletcher/5399923 to your computer and use it in GitHub Desktop.
DIY Backup

DIY Backup

Usage

This goes in crontab:

  0  4   *   *   *   /<full path to>/bin/backup.sh

Notes / TODO

  • Stop hardcoding to N backups with N rotation lines. This is dumb.
  • Can we abuse logrotate(8) for this?
  • report-backup.sh should just report the results of the last backup
#!/bin/sh
# for some background, read:
# http://www.mikerubel.org/computers/rsync_snapshots/
# ^^ especially section "4.4 Putting it all together"
# here's where this could go eventually:
# http://blog.interlinked.org/tutorials/rsync_time_machine.html
# DATE=`date "+%Y-%m-%dT%H:%M:%S"`
# ---------- VARIABLES ----------
# ~/snapshots is a symlink to /media/FreeAgent Drive/Archives/snapshots
BACKUP_PATH='/media/w5-backup'
LOGFILE='/home/tif/log/backup.log'
# ---------- LOG START OF BACKUP ----------
echo "" >> $LOGFILE
echo "*********************************************" >> $LOGFILE
echo -n `date` >> $LOGFILE
echo ":" >> $LOGFILE
echo "" >> $LOGFILE
# ---------- ROTATE ----------
rm -rf $BACKUP_PATH/backup.9
mv $BACKUP_PATH/backup.8 $BACKUP_PATH/backup.9
mv $BACKUP_PATH/backup.7 $BACKUP_PATH/backup.8
mv $BACKUP_PATH/backup.6 $BACKUP_PATH/backup.7
mv $BACKUP_PATH/backup.5 $BACKUP_PATH/backup.6
mv $BACKUP_PATH/backup.4 $BACKUP_PATH/backup.5
mv $BACKUP_PATH/backup.3 $BACKUP_PATH/backup.4
mv $BACKUP_PATH/backup.2 $BACKUP_PATH/backup.3
mv $BACKUP_PATH/backup.1 $BACKUP_PATH/backup.2
mv $BACKUP_PATH/backup.0 $BACKUP_PATH/backup.1
# ---------- DO THE BACKUP ----------
# options:
# -a archive (same as -rlptgoD):
# -r: recursive
# l: recreate symlinks
# p: preserve permissions
# t: preserve modification times
# g: set group (only if executor of rsync is root, ie it does nothing for me)
# o: set owner (same caveat as g above)
# D: recreate devices & special files (sockets and named pipes)
# --delete and remove deleted files
# --link-dest make hardlinks, so only duplicate changed files
# --modify-window=1 properly detect unchanged files between ntfs and ext4
# see http://lukemcreynolds.com/content/rsync_from_linux_to_a_windows_partition_ext3_to_ntfs/
rsync -avx --delete-excluded --delete \
--exclude '.VirtualBox/*' \
--exclude 'VirtualBox\ VMs/*' \
--exclude '.cache/*' \
--exclude '.local/share/Trash/*' \
--link-dest=$BACKUP_PATH/backup.1 \
/home/tif/ $BACKUP_PATH/backup.0/ >> $LOGFILE
# what i did for INITIAL BACKUP
# $BACKUP_PATH= ... as above
# mkdir $BACKUP_PATH/backup.3
# mkdir $BACKUP_PATH/backup.2
# mkdir $BACKUP_PATH/backup.1
# rsync -av /home/tif/ $BACKUP_PATH/backup.0/
FILE=/home/tif/log/backup.log
egrep '^\*+$' -A 1 -B 4 $FILE | grep -v '^--$'
tail -n 3 $FILE
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment