Skip to content

Instantly share code, notes, and snippets.

@jlyonsmith
Last active September 24, 2016 22:43
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 jlyonsmith/9353413 to your computer and use it in GitHub Desktop.
Save jlyonsmith/9353413 to your computer and use it in GitHub Desktop.
Script and support files for backing up Redmine database to AWS S3 bucket. Replace 'xxx' with correct values.
{
"ToAddresses": ["xxx@xxx.xxx"],
"CcAddresses": [],
"BccAddresses": []
}
{
"Subject": {
"Data": "xxx Redmine Daily Backup",
"Charset": "UTF-8"
},
"Body": {
"Text": {
"Data": "A backup file s3://{{AWS_BUCKET}}/{{TAR_FILE}} was created by a cron job running on {{PUBLIC_HOSTNAME}}.",
"Charset": "UTF-8"
},
"Html": {
"Data": "A backup file <b>s3://{{AWS_BUCKET}}/{{TAR_FILE}}</b> was created by a cron job running on <b>{{PUBLIC_HOSTNAME}}.</b>",
"Charset": "UTF-8"
}
}
}
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::xxx-redmine-backups"
]
},
{
"Effect": "Allow",
"Action": [
"s3:DeleteObject",
"s3:GetObject",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::xxx-redmine-backups/*"
]
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1393797395000",
"Effect": "Allow",
"Action": [
"ses:SendEmail"
],
"Resource": [
"*"
]
}
]
}
#!/bin/bash
function upfind() {
#Find $1 in current or parent directory
while [[ "$PWD" != "/" ]]; do
find "$PWD" -maxdepth 1 -type f -name "$1"
if [[ $? != 0 ]]; then
exit
fi
cd ..
done
}
# Configuration
SCRIPT_DIR=$(cd $(dirname $0); pwd -P)
ROOT_DIR=$(dirname $(upfind Gemfile))
REDMINE_USER=redmine
REDMINE_PASSWORD=xxx
REDMINE_DATABASE=redmine
PUBLIC_HOSTNAME=$(/usr/bin/curl http://169.254.169.254/latest/meta-data/public-hostname)
DATETIME=$(date +%Y%m%d-%H%M%S)
AWS_BUCKET=xxx-redmine-backups
export AWS_ACCESS_KEY_ID=xxx
export AWS_SECRET_ACCESS_KEY=xxx
export AWS_DEFAULT_REGION=xxx
FILES_DIR=files
DB_BACKUP_FILE=xxx-backup.sql
TAR_FILE=$(echo $PUBLIC_HOSTNAME | cut -f1,2 -d'.')-${DATETIME}.tar.gz
NUM_FILES_TO_KEEP=20
EMAIL_FROM=xxx@jamoki.com
EMAIL_DESTINATION_FILE=${SCRIPT_DIR}/backup-destination.json
EMAIL_MESSAGE_FILE=${SCRIPT_DIR}/backup-message.json
if [[ -z "$PUBLIC_HOSTNAME" ]]; then
echo Cannot get instance public hostname!
exit -1
fi
function cleanup {
rm $TAR_FILE
rm $DB_BACKUP_FILE
}
trap cleanup EXIT
cd $ROOT_DIR
# Backup database
PGPASSWORD="$REDMINE_PASSWORD" pg_dump -U $REDMINE_USER $REDMINE_DATABASE > $DB_BACKUP_FILE
# Create tarfile
/bin/tar czvf $TAR_FILE $DB_BACKUP_FILE $FILES_DIR
# Copy to Amazon S3
/usr/local/bin/aws s3 cp $TAR_FILE s3://"$AWS_BUCKET"/
if [[ $? -ne 0 ]]; then
echo error: Copy to S3 failed
exit -1
fi
# Delete old backups on S3
files=($(/usr/local/bin/aws s3 ls s3://"$AWS_BUCKET" | awk '/'"$PUBLIC_HOSTNAME"'/{print $4}' | sort --reverse | awk 'BEGIN{a=1};{if(a++>'"$NUM_FILES_TO_KEEP"'){print}}'))
for t in "${files[@]}"
do
/usr/local/bin/aws s3 rm s3://"$AWS_BUCKET"/"$t"
done
# Send E-mail
export AWS_DEFAULT_REGION=xxx
tmpfile=$BACKUP_DIR/"$EMAIL_MESSAGE_FILE".tmp.json
cat $EMAIL_MESSAGE_FILE | sed -e 's/{{AWS_BUCKET}}/'"$AWS_BUCKET"'/' -e 's/{{TAR_FILE}}/'"$TAR_FILE"'/' -e 's/{{PUBLIC_HOSTNAME}}/'"$PUBLIC_HOSTNAME"'/' > $tmpfile
/usr/local/bin/aws ses send-email --from $EMAIL_FROM --destination file://"$EMAIL_DESTINATION_FILE" --message file://"$tmpfile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment