2006-07-27 11:37:45 +02:00
|
|
|
#!/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"
|
2006-09-01 13:55:40 +02:00
|
|
|
STOR_DAYS="7"
|
2006-07-27 11:37:45 +02:00
|
|
|
|
|
|
|
# -------------
|
|
|
|
|
|
|
|
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`
|
2006-09-01 13:55:40 +02:00
|
|
|
mkdir -p $TMPDIR/$NOW
|
2006-07-27 11:37:45 +02:00
|
|
|
|
|
|
|
# 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
|
2006-09-01 13:55:40 +02:00
|
|
|
$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
|
2006-07-27 11:37:45 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
# pack dumps together
|
2006-09-01 13:55:40 +02:00
|
|
|
tar -czf database-$NOW.tar.gz -C $TMPDIR $NOW
|
2006-07-27 11:37:45 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2006-09-01 13:55:40 +02:00
|
|
|
# delete old files in storage
|
|
|
|
find $STOR -mtime $STOR_DAYS -exec rm {} \;
|
|
|
|
|
2006-07-27 11:37:45 +02:00
|
|
|
rm -rf $TMPDIR
|