Home > bash, Linux, MySQL > How to store MySQL innobackupex backups at Google Cloud Storage

How to store MySQL innobackupex backups at Google Cloud Storage

In general, I chose Google Cloud Storage to store web sites MySQL backups due to its price and speed of upload/download in real time

I used the Google native tool – gsutil , innobackupex and some bash

in short : the /etc and local MySQL  backup

#!/bin/sh
# Barcelona Tue Nov 22 17 16:30:36 CEST 2013

days_to_keep=3
NFS=/home/mysql.backups/
exportDate=`date +%Y-%m-%d.%H.%M.%S`
export_DIR=${NFS}/${HOSTNAME}.${exportDate}
test ! -d "${export_DIR}" && echo "$(date) : creating ${export_DIR}" && mkdir -p "${export_DIR}"
export_MySQL_DIR=${export_DIR}/mysql.bckp
export_ETC_DIR=${export_DIR}/etc.bckp
# backup the /etc directory
rsync -avh /etc ${export_ETC_DIR}
echo "=========================================================================================================" >> ${export_DIR}/README.restore.with.innobackupex
echo "HOW to restore this FULL mysql backup" >> ${export_DIR}/README.restore.with.innobackupex
echo "=========================================================================================================" >> ${export_DIR}/README.restore.with.innobackupex
echo "service stop mysql" >> ${export_DIR}/README.restore.with.innobackupex
echo "ps aux | grep mysql" >> ${export_DIR}/README.restore.with.innobackupex
echo "rsync -avh /var/lib/mysql /var/lib/mysql.BAD" >> ${export_DIR}/README.restore.with.innobackupex
echo "rm -rf /var/lib/mysql" >> ${export_DIR}/README.restore.with.innobackupex
echo "mkdir -p /var/lib/mysql && chown -R mysql:mysql /var/lib/mysql" >> ${export_DIR}/README.restore.with.innobackupex
echo "innobackupex --copy-back ${export_MySQL_DIR}" >> ${export_DIR}/README.restore.with.innobackupex
echo "=========================================================================================================" >> ${export_DIR}/README.restore.with.innobackupex
echo "more info at http://www.percona.com/doc/percona-xtrabackup/2.1/innobackupex/restoring_a_backup_ibk.html:" >> ${export_DIR}/README.restore.with.innobackupex
#cat /root/bin/README.restore.with.innobackupex >> ${export_DIR}/README.restore.with.innobackupex
innobackupex --ibbackup=xtrabackup --no-timestamp ${export_MySQL_DIR}
test $? -gt 0 && echo "$(date) : xtrabackup failed at ${export_MySQL_DIR}" && exit 0
innobackupex --apply-log ${export_MySQL_DIR}
find ${NFS}/ -daystart -maxdepth 1 -ctime +${days_to_keep} -type d -delete
find ${NFS}/ -daystart -maxdepth 1 -ctime +${days_to_keep} -type f -delete
nice tar cvf ${export_DIR}.tar.bz2 ${export_DIR}
test $? -eq 0 && chown seik:seik ${export_DIR}.tar.bz2 && chmod 0700 ${export_DIR}.tar.bz2 && rm -rf ${export_DIR}
chown -R seik:seik "${NFS}"

The script uploading the backups at Google cloud storage, it maintains the latest 5 backups:

#!/bin/bash
backupDir="/home/mysql.backups"
export PATH=${PATH}:$HOME/gsutil
gsUrl="gs://gsutil-test-test_default_cors-bucket-xxxxxx"
remoteBckpDir="tobedone.es"
for backup in `ls ${backupDir}`
do
 echo "$(date) : checking if ${backup} is stored"
 gsutil ls ${gsUrl}/${remoteBckpDir}/${backup} > /dev/null 2>&1
 if [ $? -gt 0 ]
 then
 echo "$(date) : ${backupDir}/${backup} is not stored, initiating upload"
 gsutil cp -R ${backupDir}/${backup} ${gsUrl}/${remoteBckpDir}/
 test $? -eq 0 && echo "$(date) : ${backup} is stored, deleting the local one" && rm ${backupDir}/${backup}
 else
 echo "$(date) : ${backup} is stored"
 test -f ${backupDir}/${backup} && echo "$(date) : ${backup} is stored, deleting the local one" && rm ${backupDir}/${backup}
 fi
done
# do some clanup at google storage
gsutil ls -lrh ${gsUrl}/${remoteBckpDir}/ | sed '2,$!d;$d' | sort -r -k 3.12 | awk '{print $4}' | sed '6,$!d' | xargs -icrap gsutil rm crap
About these ads
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: