Skip to content

Instantly share code, notes, and snippets.

@kotopes
Last active August 29, 2015 14:04
Show Gist options
  • Save kotopes/68137cdfdc1c52ce5daa to your computer and use it in GitHub Desktop.
Save kotopes/68137cdfdc1c52ce5daa to your computer and use it in GitHub Desktop.
Percona XtraBackup automate script
#!/bin/sh
#########################################
# Percona XtraBackup automate script
# Evgeny Kulev / [email protected]
# Write and test on FreeBSD 9.x
#########################################
PERCONADIRS="/var/mysql"
mkdir -p ${PERCONADIRS}
MAXDIRS=0
PREFIX_FILE="mysl_full"
DDTE="`date -v+9H +%Y%m%d%H%M`"
######################################
# backup server
ftp_client="/usr/bin/ftp"
ftp_login="backup"
ftp_password="***********"
ftp_host="backup.server.int.tf"
ftp_port="21"
ftp_path="/xtradb/slave1"
/bin/echo "`/bin/date "+%Y-%m-%d %H:%M"`: Start Innobackup db Full..."
_MASTERPOS=`echo "show slave status\G" | mysql -u root | grep "Log_"`
/usr/local/bin/innobackupex --defaults-file=/var/db/mysql/my.cnf --ibbackup=/usr/local/bin/xtrabackup --host=localhost --port=3306 --user=root --use-memory=12G --tmpdir=${PERCONADIRS}/tmp ${PERCONADIRS}/
PDIRS=`ls -1 -A ${PERCONADIRS} | grep -v "tar\.gz" | grep -v "tmp"`
LDIRS=`ls -1 -A ${PERCONADIRS} | grep -v "tar\.gz" | grep -v "tmp" | wc -l`
echo "# found ${LDIRS} backups"
echo "#-------------------------------"
for DUMP_DIR in $PDIRS
do
echo "# ${DUMP_DIR}"
done
/bin/echo "`/bin/date "+%Y-%m-%d %H:%M"`: Make prepare db Innobackup Full..."
echo "#-------------------------------"
BDIR=`ls -1 -A ${PERCONADIRS} | grep -v "tmp" | grep -v "tar\.gz" | sort -rn | head -n 1`
/usr/local/bin/innobackupex --defaults-file=/var/db/mysql/my.cnf --apply-log --use-memory=4G ${PERCONADIRS}/${BDIR}/
echo "${_MASTERPOS}" > ${PERCONADIRS}/${BDIR}/slave.pos.status
chown -R mysql:mysql ${PERCONADIRS}/${BDIR}/
echo "`/bin/date "+%Y-%m-%d %H:%M"`: Archiving ${BDIR} into ${PREFIX_FILE}_${DDTE}.tar.gz"
/usr/bin/tar czpf ${PERCONADIRS}/${PREFIX_FILE}_${DDTE}.tar.gz ${PERCONADIRS}/${BDIR}/
echo "`/bin/date "+%Y-%m-%d %H:%M"`: Upload Backup into Cloud Latest ${BDIR}"
${ftp_client} -u ftp://${ftp_login}:${ftp_password}@${ftp_host}:${ftp_port}${ftp_path}/${PREFIX_FILE}_${DDTE}.tar.gz ${PERCONADIRS}/${PREFIX_FILE}_${DDTE}.tar.gz
/bin/rm -f ${PERCONADIRS}/${PREFIX_FILE}_${DDTE}.tar.gz
if [ ${LDIRS} -gt ${MAXDIRS} ]; then
DELS=$(echo ${LDIRS} - ${MAXDIRS} | bc)
echo "#-------------------------------"
echo "# Need delete ${DELS} directory"
LDIRS=`ls -1 -A ${PERCONADIRS} | grep -v "tar\.gz" | grep -v "tmp" | sort | head -n ${DELS}`
if [ ${DELS} -gt 0 ]; then
for DEL_DIR in $LDIRS
do
echo "# delete old ${DEL_DIR}"
cd ${PERCONADIRS} && /bin/rm -rf ${DEL_DIR}
done
fi
fi
/bin/echo "`/bin/date "+%Y-%m-%d %H:%M"`: end."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment