Skip to content

Instantly share code, notes, and snippets.

@lucien144
Last active November 6, 2016 23:16
Show Gist options
  • Select an option

  • Save lucien144/246041d833b7c22db5cd485c6ec47f33 to your computer and use it in GitHub Desktop.

Select an option

Save lucien144/246041d833b7c22db5cd485c6ec47f33 to your computer and use it in GitHub Desktop.
MySQL backup of database w/ separated tables and S3 sync
#!/bin/bash
START=$SECONDS
DATE=`date +%Y%m%d%H%M`
MYSQL=mysql
MYSQLDUMP=mysqldump
DBNAME="dbname"
USER="mysqldump"
PASSWORD="***"
mkdir $DATE
echo "Exporting database $DBNAME"
$MYSQL -u$USER -p$PASSWORD -e "SHOW tables IN $DBNAME;" | while read table; do
if [ $table == "Tables_in_$DBNAME" ]; then
continue
fi
echo " -> exporting table '$table': started"
$MYSQLDUMP -u$USER -p$PASSWORD --single-transaction --routines --triggers --add-drop-table --add-locks --create-options --disable-keys --extended-insert --quick --set-charset $DBNAME $table | gzip > ./$DATE/$DATE_$table.sql.gz
echo " -> exporting table '$table': ended"
done
s3cmd sync --recursive ./$DATE s3://mysql/
find . -name *.sql.gz -mtime +2 -exec rm {} \;
DURATION=$(( SECONDS - start ))
echo "Backup finished after $DURATION seconds"
#!/bin/bash
START=$SECONDS
MYSQL=mysql
DBNAME="dbname"
USER="user"
FILES=/data/restore/*.sql.gz
read -p "Enter the password for user $USER:" PASSWORD;
for f in $FILES
do
echo "Importing file $f"
zcat $f | $MYSQL -u$USER -p$PASSWORD --database=$DBNAME
done
DURATION=$(( SECONDS - start ))
echo "Restoration finished after $DURATION seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment