diff --git a/README.md b/README.md index 7f72ba5..ec15a30 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,10 @@ This repository contains the basic setup for how I like to configure my command editor settings, and other odds and ends. In order to get a new machine up and running, simply clone this repository onto the machine and run the included `install.sh` script. It will check for missing software and install it, and it will install its own versions of various important configuration files (e.g., `.profile`, `.vimrc`, etc.). + +### Backups + +There are a number of commands in `bin` related to backing up your files to a remote drive. If you'd like to use them, +start by running `backup-init.sh`. It will ask you for various bits of information necessary to configure your backups. +When its finished, you'll want to call `backup-run.sh` whenever you'd like to make a backup. This command is written +with running from `cron` in mind, and may be safely run that way. diff --git a/bin/backup-commands.default.sh b/bin/backup-commands.default.sh new file mode 100755 index 0000000..a33ab65 --- /dev/null +++ b/bin/backup-commands.default.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# This script will automatically be invoked by the backup system from the target directory. Use whatever bash commands +# you like to backup any files needed into the current directory. The following will grab everything from your home +# directory which commonly includes data you may care about. If there are other things you want to save, be sure to +# add them in. + +rsync -az --delete /Users/andrew/ ./Users/andrew \ + --exclude .DS_Store \ + --exclude .Trash \ + --exclude .dropbox \ + --exclude Dropbox \ + --exclude Library \ + --include Library/Application\ Support + diff --git a/bin/backup-init.sh b/bin/backup-init.sh new file mode 100755 index 0000000..423deec --- /dev/null +++ b/bin/backup-init.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +BACKUP_COMMANDS="$HOME/.backup-commands.sh" +BIN_DIR=$(dirname $0) + +read -p "Enter your backup drive host: " HOST +read -p "Enter your backup drive share: " SHARE +read -p "Enter your backup drive username: " USERNAME +read -p "Enter your backup drive password: " -s PASSWORD + +MASKED_PASSWORD=$(echo $PASSWORD | sed "s/./*/g") + +echo; read -p "Backup to afp://$USERNAME:$MASKED_PASSWORD@$HOST/$SHARE? (Y/n): " CONFIRM +[[ "$CONFIRM" == "" ]] && CONFIRM="Y" +CONFIRM=$(echo $CONFIRM | tr "a-z" "A-Z") +if [[ "$CONFIRM" != "Y" ]]; then + echo "Aborting backup init." + exit 1 +fi + +security add-generic-password -a $USER -s backup-host -w "$HOST" +security add-generic-password -a $USER -s backup-share -w "$SHARE" +security add-generic-password -a $USER -s backup-username -w "$USERNAME" +security add-generic-password -a $USER -s backup-password -w "$PASSWORD" + +cp $BIN_DIR/backup-commands.default.sh "$BACKUP_COMMANDS" +chmod u+x "$BACKUP_COMMANDS" + +echo; echo ">>>> PLEASE READ THIS! <<<<" +echo "The file '.backup-commands.sh' has been created in your home directory." +echo "Please review it to be sure it covers everything you want to back up." +read -p "(press enter to continue)" + diff --git a/bin/backup-mount.sh b/bin/backup-mount.sh new file mode 100755 index 0000000..47fa8ee --- /dev/null +++ b/bin/backup-mount.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +function die { + echo $*; exit 1 +} +LOCAL_MOUNT="$HOME/.backup-drive" + +HOST=$(security find-generic-password -a $USER -s backup-host -w) +SHARE=$(security find-generic-password -a $USER -s backup-share -w) +USERNAME=$(security find-generic-password -a $USER -s backup-username -w) +PASSWORD=$(security find-generic-password -a $USER -s backup-password -w) +DRIVE="afp://$USERNAME:$PASSWORD@$HOST/$SHARE" + +if ! mount | grep -q "$HOME/$LOCAL_MOUNT"; then + if [[ -e "$LOCAL_MOUNT" ]]; then + rm -rf "$LOCAL_MOUNT" || die "Could not mount to $LOCAL_MOUNT" + fi + mkdir -p "$LOCAL_MOUNT" + mount -t afp "$DRIVE" "$LOCAL_MOUNT" || die "Could not mount from $DRIVE" +fi + diff --git a/bin/backup-run.sh b/bin/backup-run.sh new file mode 100755 index 0000000..117a7c5 --- /dev/null +++ b/bin/backup-run.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +function die { + echo $*; exit 1 +} + +LOCAL_MOUNT="$HOME/.backup-drive" +BACKUP_COMMANDS="$HOME/.backup-commands.sh" +STARTED_DATE=$(date) + +[[ -x "$BACKUP_COMMANDS" ]] || die "Could not find $BACKUP_COMMANDS" + +echo "Starting backup at $STARTED_DATE..." + +./backup-unmount.sh || die +./backup-mount.sh || die +[[ -d "$LOCAL_MOUNT" ]] || die "Could find $LOCAL_MOUNT" + +( + cd "$LOCAL_MOUNT" + echo "Started backing up at $STARTED_DATE" > backup.log + "$BACKUP_COMMANDS" >> backup.log 2>&1 +) + +if [[ "$?" != 0 ]]; then + echo "ERROR: Backup failed!" + head -n10 "$LOCAL_MOUNT/backup.log" + echo "..." + tail -n10 "$LOCAL_MOUNT/backup.log" +fi + +./backup-unmount.sh || echo "WARNING: Could not unmount $LOCAL_MOUNT! Please check into this!" +echo "Finished backing up at $(date)." + diff --git a/bin/backup-unmount.sh b/bin/backup-unmount.sh new file mode 100755 index 0000000..2130e60 --- /dev/null +++ b/bin/backup-unmount.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +function die { + echo $*; exit 1 +} +LOCAL_MOUNT="$HOME/.backup-drive" + +if mount | grep -q "$LOCAL_MOUNT"; then + umount "$LOCAL_MOUNT" || die "Could not unmount $LOCAL_MOUNT" +fi +[[ -e "$LOCAL_MOUNT" ]] && rm -r "$LOCAL_MOUNT" +