Skip to content

Instantly share code, notes, and snippets.

@Seefin
Created March 23, 2015 09:41
Show Gist options
  • Select an option

  • Save Seefin/3fa30a5599250f09456e to your computer and use it in GitHub Desktop.

Select an option

Save Seefin/3fa30a5599250f09456e to your computer and use it in GitHub Desktop.
A backup script using rsync and etckeeper, written in bash.
#!/bin/bash
# Backs stuff up. This script uses rsync to back stuff up to the backup drive. It
# is *not* version control, so it only has a single copy of the tree; this increases
# speed and keeps the file IO time to a minimum.
# !!!
# THIS SCRIPT CAN AND WILL DELETE THINGS FROM THE DESTINATION.
# !!!
# To pass in a non-standard source and destination, run this script like so:
# backup.sh <SRC> <DEST>
#
#This script is based off of answers from help forums on the internet where indicated.
#This is our ignore list. It is quite extensive, and is based off of the wonderfully
# informative answer [here](http://askubuntu.com/questions/40992/what-files-and-directories-can-be-excluded-from-a-backup-of-the-home-directory)
IGNORELIST="$HOME/usr/bin/.backupIgnoreList"
#Our source and destination directories
if [[ $# -ne 2 ]]; then
SRC=/home/
DEST=/backup/home/
else
SRC=$1
DEST=$2
fi
#Check if our Effective User ID is root; if it's not, we will need to use sudo later
# in our script.
if [[ $EUID -ne 0 ]]; then
USERISROOT=1
else
USERISROOT=0
fi
#Checks a return status for errors. This function runs all it's arguments, and "throws"
# an error if the function fails, halting execution of the script.
function runErrorCheck {
#Expand and run all the arguments - this means that we run the command passed to us.
"$@"
ERROR="$?"
if [[ $ERROR -ne 0 ]]; then
if [[ $ERROR -ne 32 ]]; then #32 == Already mounted (likely)
echo -e "$0 failed\nFix the errors, and try again"
exit $ERROR
fi
fi
}
#Mount the backup drive and cd to it. Both actions could produce errors if the drive
# is not present, or if the mount has incorrect permissions, so they are passed to
# runErrorCheck, which will run the commands, checking for errors. As the name suggests.
runErrorCheck mount /backup
runErrorCheck cd /backup
#If we aren't root, use sudo(8) to run our root commands
#Copy everything to the backup; this copy uses a delta-algorithm to minimize file
# transfers. Options: --Archive(all except Hardlinks) --verbose --human-readable
# show progress, delete files in dest not in src, print file algorithm statistics
# ignore all the things in IGNORELIST.
# Based on [this](https://ask.fedoraproject.org/en/question/27563/recommended-way-to-backup-computer-before-upgrade/?answer=27579#post-id-27579).
# After rsync copies everything, we use etckeeper to update the backup copy of our
# /etc directory. etckeeper.conf sets up a post-commit hook to push the repository
# to the repository on the backup drive, so we don't have to do anything with it.
if [[ $USERISROOT ]]; then #WE ARE (EFFECTIVELY) ROOT YAY!
rsync -avh --progress --delete --stats --exclude-from="$IGNORELIST" "$SRC" "$DEST"
cd /etc
etckeeper commit
else #WE'RE NOT ROOT-LIKE :(
sudo rsync -avh --progress --delete --stats --exclude-from="IGNORELIST" "$SRC" "$DEST"
sudo cd /etc
sudo etckeeper commit
fi
@tux-00

tux-00 commented May 27, 2016

Copy link
Copy Markdown

It seems that you missed a '$' at line 68 at the --exclude-from parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment