Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a backup script to send backups over email when changed #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions lib/mail-backup-if-new.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash

# backups/ has special 'new' and 'old' symbolic links to the last
# backup and the one-before-last backup. We use them to check whether
# the new backup is identical to the previous backup. If it isn't, we
# send an email with the backup.

# If a backups/passphrase file exists, its content is used as a
# passphrase to encrypt the backup file before sending it by email.

# we assume the script is in the 'lib/' directory of a HotCRP install,
# move to the root of the project before running the code
cd "$( dirname "${BASH_SOURCE[0]}" )"/..

BACKUP=backup-$(date +%F-%T).txt
PASSPHRASE_FILE=passphrase

if [ -z "$BACKUP_EMAIL" ]
then
echo "You must define a BACKUP_EMAIL environment variable"
echo "with the email address to send backups to."
exit 2
fi

dump () {
# --skip-dump-date is necessary, otherwise a timestamp
# is included in the backup and the files always differ
./lib/backupdb.sh --skip-dump-date > backups/$BACKUP
}

# assumes that 'dump ()' ran and that we are in 'backups'
init () {
ln -s old $BACKUP
ln -s new $BACKUP
}

# assumes that 'dump ()' ran and that we are in 'backups'
age () {
rm $(readlink old)
mv new old
ln -s $BACKUP new
}

email () {
echo "New backup $(readlink new) differs from $(readlink old), so it is attached" \
| mailx -s "[hotcrp-mlocaml2017-postproceedings] new backup" -a $BACKUP_FILE $BACKUP_EMAIL
}

# assumes that 'dump ()' ran and that we are in 'backups'
email_if_changed () {
echo "comparing the new backup $BACKUP and the previous one $(readlink old)"
cmp new old && exit 0

if [[ -f $PASSPHRASE_FILE ]]
then
echo "encrypting the backup according to backups/$PASSPHRASE_FILE"
gpg --batch --passphrase-file $PASSPHRASE_FILE --symmetric $BACKUP
BACKUP_FILE=$BACKUP.gpg
email
rm $BACKUP_FILE
else
BACKUP_FILE=$BACKUP
email
fi
}

mkdir_if_missing () {
if [[ ! -d backups ]]
then
mkdir -p backups
init
fi
}

dump
cd backups
age
email_if_changed