#!/bin/bash DB_USER="root" DB_PASS="password" SCP_HOST="remote.server" SCP_USER="username" SCP_PATH="backups/" STOR="/home/dbbackup/" STOR_USER="dbbackup:root" STOR_PERM="0600" # ------------- DUMP_SCHEMA="/usr/bin/mysqldump --opt --all --no-data" DUMP_DATA="/usr/bin/mysqldump --opt --add-locks=FALSE --no-create-info" umask 0077 NOW=`date +%Y-%m-%d-%H-%M` TMPDIR=`mktemp -d` TMPDIR2="$TMPDIR/$NOW" mkdir -p $TMPDIR2 # generate dumps db_list=`echo "show databases;" | /usr/bin/mysql -u${DB_USER} -p${DB_PASS} -r --column-name=FALSE` for db in $db_list; do $DUMP_SCHEMA -u${DB_USER} -p${DB_PASS} --databases $db > $TMPDIR2/$db-schema.sql $DUMP_DATA -u${DB_USER} -p${DB_PASS} $db > $TMPDIR2/$db-data.sql done # pack dumps together pushd . &> /dev/null cd $TMPDIR tar -czf database-$NOW.tar.gz $NOW popd &> /dev/null # upload /usr/bin/scp -q $TMPDIR/database-$NOW.tar.gz ${SCP_USER}@${SCP_HOST}:${SCP_PATH} # move to storage chmod $STOR_PERM $TMPDIR/database-$NOW.tar.gz chown $STOR_USER $TMPDIR/database-$NOW.tar.gz mv $TMPDIR/database-$NOW.tar.gz $STOR rm -rf $TMPDIR