Skip to content

Commit

Permalink
Change variable for filename
Browse files Browse the repository at this point in the history
This saves the file extension in the variable for the filename, so that doesn't
have to be specified every single time any more.
  • Loading branch information
tomwassenberg committed Aug 11, 2017
1 parent 111c269 commit 18e07df
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions ansible/templates/usr/local/bin/backup-to-s3.sh.j2
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ if [[ -z ${1+x} ]]; then
fi

SOURCE="${1}"
FILE="${SOURCE}-$(date +'%Y%m%d-%H%M%S')"
HASH="sha256" # Choose from md5/sha1/sha224/sha256/sha384/sha512
FILE_TITLE="${SOURCE}-$(date +'%Y%m%d-%H%M%S')"

S3BUCKET="{% if staging == 'true' %}staging-{% endif %}sticky-automatic-backups"

cleanup() {
# Ensure files are deleted, in case script crashes

# Check because file extension is only set when a valid backup source is
# Check because complete file name is only set when a valid backup source is
# passed
if [[ -n ${FILE_EXT+x} ]]; then
if [[ -n ${FILE_NAME+x} ]]; then
{
rm -rf "{{ tmp_dir }}/${FILE}.${FILE_EXT}"
rm -rf "{{ tmp_dir }}/${FILE}.${FILE_EXT}.${HASH}"
rm -rf "{{ tmp_dir }}/${FILE_NAME}"
rm -rf "{{ tmp_dir }}/${FILE_NAME}.${HASH}"
} 1> /dev/null
fi
}
Expand All @@ -55,59 +55,56 @@ BACKUP_DATE=$(TZ='Europe/Amsterdam' date +'%F %R %:z')
case "${SOURCE}" in
admins)
S3PATH="${SOURCE}"
FILE_EXT="tar.gz"
FILE_NAME="${FILE_TITLE}.tar.gz"

# Some directories excluded to save space, since they only contain
# binaries/cache anyway.
tar --exclude='home/koala/.rbenv' --exclude='home/koala/.bundle' \
--exclude='home/koala/.cache' -c -f - -C / home | gzip -9 > \
"{{ tmp_dir }}/${FILE}.${FILE_EXT}"
--exclude='home/koala/.cache' -c -f - -C / home | \
gzip -9 > "{{ tmp_dir }}/${FILE_NAME}"
;;
websites)
S3PATH="${SOURCE}"
FILE_EXT="tar.gz"
FILE_NAME="${FILE_TITLE}.tar.gz"

# phpMyAdmin and SODI directories excluded because no other
# committee can write to these folders and they are deployed from \
# git anyway.
tar --exclude='var/www/phpmyadmin.{{ canonical_hostname }}' \
--exclude='var/www/sodi.{{ canonical_hostname }}' -c -f - -C / \
var/www | gzip -9 > "{{ tmp_dir }}/${FILE}.${FILE_EXT}"
var/www | gzip -9 > "{{ tmp_dir }}/${FILE_NAME}"
;;
databases)
S3PATH="websites/${SOURCE}"
FILE_EXT="sql.gz"
FILE_NAME="${FILE_TITLE}.sql.gz"

# Uses root's unix socket for authentication
mysqldump --all-databases | gzip -9 > \
"{{ tmp_dir }}/${FILE}.${FILE_EXT}"
mysqldump --all-databases | gzip -9 > "{{ tmp_dir }}/${FILE_NAME}"
;;
*)
print_to_stderr "${USAGE}"
exit 1
esac

BACKUP_SIZE=$(stat --printf="%s" "{{ tmp_dir }}/${FILE}.${FILE_EXT}" | \
BACKUP_SIZE=$(stat --printf="%s" "{{ tmp_dir }}/${FILE_NAME}" | \
numfmt --to=iec --suffix=B --format="%.2f")

SUCCESS_MESSAGE="*{% if staging == 'true' %}_FROM STAGING:_ {% endif %}Backup of \
${SOURCE} completed* _(${BACKUP_DATE})_\n_(Backup size: ${BACKUP_SIZE})_"

${HASH}sum "{{ tmp_dir }}/${FILE}.${FILE_EXT}" > \
"{{ tmp_dir }}/${FILE}.${FILE_EXT}.${HASH}"
${HASH}sum "{{ tmp_dir }}/${FILE_NAME}" > "{{ tmp_dir }}/${FILE_NAME}.${HASH}"

{
aws s3 cp "{{ tmp_dir }}/${FILE}.${FILE_EXT}" "s3://${S3BUCKET}/${S3PATH}/"
aws s3 cp "{{ tmp_dir }}/${FILE_NAME}" "s3://${S3BUCKET}/${S3PATH}/"

aws s3 cp "{{ tmp_dir }}/${FILE}.${FILE_EXT}.${HASH}" \
"s3://${S3BUCKET}/${S3PATH}/"
aws s3 cp "{{ tmp_dir }}/${FILE_NAME}.${HASH}" "s3://${S3BUCKET}/${S3PATH}/"

rm "{{ tmp_dir }}/${FILE}.${FILE_EXT}"
rm "{{ tmp_dir }}/${FILE_NAME}"

aws s3 cp "s3://${S3BUCKET}/${S3PATH}/${FILE}.${FILE_EXT}" \
"{{ tmp_dir }}/${FILE}.${FILE_EXT}"
aws s3 cp "s3://${S3BUCKET}/${S3PATH}/${FILE_NAME}" \
"{{ tmp_dir }}/${FILE_NAME}"

${HASH}sum --check "{{ tmp_dir }}/${FILE}.${FILE_EXT}.${HASH}"
${HASH}sum --check "{{ tmp_dir }}/${FILE_NAME}.${HASH}"

echo -e "${SUCCESS_MESSAGE}" | /usr/local/bin/slacktee --plain-text\
--username 'Backup service' --icon ':floppy_disk:' --attachment 'good'
Expand Down

0 comments on commit 18e07df

Please sign in to comment.