Skip to content

Instantly share code, notes, and snippets.

@bgaillard
Created November 3, 2015 14:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgaillard/cf9898586ce3a7dcd90a to your computer and use it in GitHub Desktop.
Save bgaillard/cf9898586ce3a7dcd90a to your computer and use it in GitHub Desktop.
Backup utilities
#!/bin/sh
########################################################################################################################
# Amazon Glacier Backup Script
#
# Copyright @ 2015 GoMoob
# http://www.gomoob.com
# contact@gomoob.com
#
# Authors :
# - Baptiste GAILLARD (baptiste.gaillard@gomoob.com)
########################################################################################################################
# see http://www.davidpashley.com/articles/writing-robust-shell-scripts/#id2382181
set -u
set -e
# The directory where Web Application and Database Backups are placed
BACKUP_DIRECTORY=/home/ec2-user/backups
##
# This array indicates where are the directories to copy inside the archive to create and push to Amazon Glacier. The
# backup script will scan the provided arrays recursively. The files found inside this directory will never be touched
# or deleted after the backup is performed.
##
FILES_TO_COPY=()
##
# This array has the same purpose as the FILES_TO_COPY array except that the files taken from this array which are older
# than the DELETE_FILES_OLDER_THAN value will be automatically deleted after each successful archive creation and
# sending to Amazon Glacier.
#
# The files present inside the directories provided here can be database backups, this is very useful when you want to
# keep the last database backups of your application and have a quick access to them for example.
##
FILES_TO_COPY_AND_DELETE=("${BACKUP_DIRECTORY}/database")
##
# Number of days which allow to delete files present in the directories specified in the FILES_TO_COPY_AND_DELETE
# variable. This variable indicate to delete files older that X days.
##
DELETE_FILES_OLDER_THAN=5
# The date when this script is executed.
CURRENT_DATE=`date '+%Y-%m-%d - %H-%M-%S'`
##
# Path to the configuration file of the Command Line for Amazon Glacier tool
#
# This tool has to be installed following the instructions present here :
# - https://github.com/uskudnik/amazon-glacier-cmd-interface
##
GLACIER_CMD_CONF=/home/ec2-user/glacier-cmd.conf
# Name of the Amazon Glacier Vault where to save the created archives
GLACIER_VAULT=myvault
# Name of the temporary directory used to create the Amazon Glacier
# archive before uploading it
GLACIER_DIRECTORY=${BACKUP_DIRECTORY}/glacier
# Name of the Amazon Glacier archives to create
GLACIER_ARCHIVE=${GLACIER_DIRECTORY}/${CURRENT_DATE}.tar.gz
# Name of temporary directory created by the script to create the archives
GLACIER_DIRECTORY_TO_ARCHIVE=${GLACIER_DIRECTORY}/${CURRENT_DATE}
# Create a glacier directory were to build the glacier archive
echo Creating a Glacier Directory to create the archive ...
echo - Directory to create is \'${GLACIER_DIRECTORY_TO_ARCHIVE}\' ...
mkdir "${GLACIER_DIRECTORY_TO_ARCHIVE}"
if [[ -z FILES_TO_COPY ]]
then
echo - Copying files into the Glacier Directory
for i in "${FILES_TO_COPY[@]}"
do :
echo - Copying files from \'$i\' to \'${GLACIER_DIRECTORY_TO_ARCHIVE}\' ...
cp -R "$i"/. "${GLACIER_DIRECTORY_TO_ARCHIVE}"
done
fi
for i in "${FILES_TO_COPY_AND_DELETE[@]}"
do :
echo - Copying files from \'$i\' to \'${GLACIER_DIRECTORY_TO_ARCHIVE}\' ...
cp -R "$i"/. "${GLACIER_DIRECTORY_TO_ARCHIVE}"
done
echo - Creating the archive to backup in \'${GLACIER_ARCHIVE}\' ...
tar -zcvf "${GLACIER_ARCHIVE}" "${GLACIER_DIRECTORY_TO_ARCHIVE}"
echo - Uploading \'${GLACIER_ARCHIVE}\' to Amazon Glacier Vault \'${GLACIER_VAULT}\' ...
if hash glacier-cmd 2>/dev/null; then
glacier-cmd -c "${GLACIER_CMD_CONF}" upload ${GLACIER_VAULT} "${GLACIER_ARCHIVE}"
else
glacier -c "${GLACIER_CMD_CONF}" upload ${GLACIER_VAULT} "${GLACIER_ARCHIVE}"
fi
echo Deleting files older than ${DELETE_FILES_OLDER_THAN} days ...
for i in "${FILES_TO_COPY_AND_DELETE[@]}"
do :
find "$i"/* -mtime +${DELETE_FILES_OLDER_THAN} -exec rm -f {} \;
done
echo Removing the backup temporay archive ...
echo ${GLACIER_DIRECTORY_TO_ARCHIVE}
rm -rf "${GLACIER_DIRECTORY_TO_ARCHIVE}"
echo Removing the backup archive ...
echo ${GLACIER_ARCHIVE}
rm -rf "${GLACIER_ARCHIVE}"
echo Cleaning the glacier temporary directory ...
echo ${GLACIER_DIRECTORY}
rm -Rf ${GLACIER_DIRECTORY}/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment