#!/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" STOR_DAYS="7" # ------------- 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` mkdir -p $TMPDIR/$NOW # 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 > $TMPDIR/$NOW/$db-schema.sql $DUMP_DATA -u${DB_USER} -p${DB_PASS} $db > $TMPDIR/$NOW/$db-data.sql done # pack dumps together tar -czf database-$NOW.tar.gz -C $TMPDIR $NOW # 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 # delete old files in storage find $STOR -mtime $STOR_DAYS -exec rm {} \; rm -rf $TMPDIR