diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2009-12-11 00:18:29 +0100 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2009-12-11 00:18:29 +0100 |
commit | cd110f2944e9fb37cd1f21bb869d24949f6941f0 (patch) | |
tree | 5642397f290b760373f24fc934b8e2171c431705 | |
parent | 27e0588ee8cb335fe6749f2f076fffbd8c051b84 (diff) | |
download | backup-cd110f2944e9fb37cd1f21bb869d24949f6941f0.tar.gz backup-cd110f2944e9fb37cd1f21bb869d24949f6941f0.tar.xz backup-cd110f2944e9fb37cd1f21bb869d24949f6941f0.zip |
new version, mysql binlog reset
-rw-r--r-- | common/check_tools.sh | 11 | ||||
-rw-r--r-- | common/init.sh | 21 | ||||
-rw-r--r-- | common/log.sh | 15 | ||||
-rw-r--r-- | common/update.sh | 18 | ||||
-rwxr-xr-x | mysql/mysql_backup.sh | 87 |
5 files changed, 85 insertions, 67 deletions
diff --git a/common/check_tools.sh b/common/check_tools.sh new file mode 100644 index 0000000..ba3640d --- /dev/null +++ b/common/check_tools.sh @@ -0,0 +1,11 @@ +# check for necessary tools + +_check_tools() { + for prog in $@ + do + if ! hash $prog 2>/dev/null ; then + echo "Error: This script needs '$prog'. Exiting..." >&2 + exit 1 + fi + done +} diff --git a/common/init.sh b/common/init.sh new file mode 100644 index 0000000..a955f18 --- /dev/null +++ b/common/init.sh @@ -0,0 +1,21 @@ +# init common function for backup + +# get path to common script dir +if [ -z "${BACKUP_ROOT}" -o ! -d "${BACKUP_ROOT}/common/" ]; then + echo "\$BACKUP_ROOT not set or invalid! (common dir not there)" >&2 + echo "Please set it to the path to the root directory of the" >&2 + echo "backup scripts before sourcing this init.sh file." >&2 + exit 1 +fi + +# load all common scripts +for file in ${BACKUP_ROOT}/common/* +do + if [ "$(basename ${file})" != "init.sh" ]; then + source "${file}" + fi +done + +# check if git is available for update +_check_tools git +_self_update diff --git a/common/log.sh b/common/log.sh new file mode 100644 index 0000000..622a9bb --- /dev/null +++ b/common/log.sh @@ -0,0 +1,15 @@ +# log function for displaying output if loging enabled + +_log() { + # only output something, + # if logging is enabled + if [ $LOGGING -eq 0 ]; then + if [ -n "$1" ]; then + # echo arguments + echo "$@" + else + # echo stdin + cat + fi + fi +} diff --git a/common/update.sh b/common/update.sh new file mode 100644 index 0000000..c4f0b5f --- /dev/null +++ b/common/update.sh @@ -0,0 +1,18 @@ +# update backup scripts + +_self_update() { + pushd $(dirname $0) &>/dev/null + + git fetch origin 2>/dev/null + if [[ -n "$(git whatchanged HEAD..origin/master)" ]]; then + echo "Selfupdating backupscript..." | log + git merge origin/master | log + echo "Selfupdating done." | log + echo | log + + popd &>/dev/null + exec $0 + fi + + popd &>/dev/null +} diff --git a/mysql/mysql_backup.sh b/mysql/mysql_backup.sh index 70a93dd..632c010 100755 --- a/mysql/mysql_backup.sh +++ b/mysql/mysql_backup.sh @@ -7,10 +7,6 @@ MYSQL_SERVER="localhost" MYSQL_USER="backup" MYSQL_PASSWORD="password" -# databases to exclude, seperated by space -# TODO -#MYSQL_EXCLUDE_DB="" - # ftp server data FTP_SERVER="backup" FTP_USER="ftp" @@ -31,78 +27,35 @@ LOGGING="0" ## end of configuration -# only output something if logging is enabled -log() { - if [ $LOGGING -eq 0 ]; then - if [ -n "$1" ]; then - echo "$@" - else - cat - fi - fi -} - -# selfupdate magic -_self_update() { - pushd $(dirname $0) &>/dev/null - - git fetch origin 2>/dev/null - if [[ -n "$(git whatchanged HEAD..origin/master)" ]]; then - echo "Selfupdating backupscript..." | log - git merge origin/master | log - echo "Selfupdating done." | log - echo | log - - popd &>/dev/null - exec $0 - fi - - popd &>/dev/null -} +# load common stuff +BACKUP_ROOT=".." +source ${BACKUP_ROOT}/common/init.sh # check for tools -for prog in git mysqlshow mysqldump ncftpput -do - if ! hash $prog 2>/dev/null ; then - echo "Error: This script needs '$prog'. Exiting..." >&2 - exit 1 - fi -done - -_self_update +_check_tools mysqlshow mysqldump ncftpput -echo "Started backup of mysql databases ($(date +%c)):" | log -echo | log +echo "Started backup of mysql databases ($(date +%c)):" | _log +echo | _log # create temporary dir mkdir -p ${TMP_DIRECTORY} TMP_DIR=$(mktemp -d --tmpdir=${TMP_DIRECTORY}) pushd ${TMP_DIR} >/dev/null 2>&1 -# enumerate all mysql databases -MYSQL_DATABASES=$(mysqlshow -h${MYSQL_SERVER} -u${MYSQL_USER} \ - -p${MYSQL_PASSWORD} | awk '{ print $2 }' | grep -v "^$" | sed \ - '1d') - -# backup all databases to file -for db in ${MYSQL_DATABASES} -do - mysqldump -h${MYSQL_SERVER} -u${MYSQL_USER} -p${MYSQL_PASSWORD} \ - ${db} | bzip2 -zc > "${BACKUP_PREFIX}_${db}.sql.bz2" -done - -# echo created log -du -sch * | log -echo | log - -# move backups to ftp server -for file in * -do - ncftpput -u${FTP_USER} -p${FTP_PASSWORD} -V -DD ${FTP_SERVER} \ - ${FTP_TARGET} ${file} >/dev/null -done - -echo "Finished backup of mysql databases ($(date +%c))" | log +# backup all databases to file and reset binlog +mysqldump -h${MYSQL_SERVER} -u${MYSQL_USER} -p${MYSQL_PASSWORD} \ + --flush-logs --master-data=2 --all-databases --delete-master-logs \ + --lock-all-tables | bzip2 -zc > "${BACKUP_PREFIX}.sql.bz2" + +# echo created log of file sizes +du -sch "${BACKUP_PREFIX}.sql.bz2" | _log +echo | _log + +# move backup to ftp server +ncftpput -u"${FTP_USER}" -p"${FTP_PASSWORD}" -V -DD "${FTP_SERVER}" \ + "${FTP_TARGET}" "${BACKUP_PREFIX}.sql.bz2" >/dev/null + +echo "Finished backup of mysql databases ($(date +%c))" | _log # restore old cwd popd >/dev/null 2>&1 |