Skip to content

Instantly share code, notes, and snippets.

@KevinWang15
Created April 23, 2018 16:42
Show Gist options
  • Save KevinWang15/b7f176dd30019fb8384e97d3a6ab8fa7 to your computer and use it in GitHub Desktop.
Save KevinWang15/b7f176dd30019fb8384e97d3a6ab8fa7 to your computer and use it in GitHub Desktop.
backup mysql
#!/bin/bash
# crontab:
# 30 */8 * * * /root/backup/mysql.sh
databases=(db1 db2 db3)
basepath='/root/backup/mysql/'
if [ ! -d "$basepath" ]; then
mkdir -p "$basepath"
fi
for db in ${databases[*]}
do
/usr/bin/nice -n 19 /usr/bin/mysqldump -u root --password=[password here] --databases $db > $basepath$db-$(date +%Y-%m-%d_%H).sql
/usr/bin/nice -n 19 gzip -f $basepath$db-$(date +%Y-%m-%d_%H).sql
find $basepath -mtime +30 -name "*.sql.gz" -exec rm -rf {} \;
done
rm -rf $basepath/*.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment