MySQL 备份
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MySQL 备份,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3003字,纯文字阅读大概需要5分钟。
内容图文
![MySQL 备份](/upload/InfoBanner/zyjiaocheng/1065/9dfee14c1ded49e4934c164bd3934238.jpg)
Automated MySQL Database Backup
Version 1
Want to backup your MySQL databases to another machine on a nightly basis?
Then create a /etc/cron.daily/mysqlbackup.sh job like this:
Run chmod +x /etc/cron.daily/mysqlbackup.sh
. And change
the $ parameters to the appropriate ‘real‘ values.
Or if you are not backing up to a separate host, run:
Version 2
This one is slightly more advanced. It‘s a bash script that will basically do the same as above but also only keep a certain number of old copies.
Note that this script is prepared to backup as the root user, if you are going to be running this as a non root user you should modify paths to reflect that.
# List of databases to be backed up separated by space
dblist="db_name_1 db_name_2 db_name_3 etc."
# Directory for backups
backupdir=/root/mysql_dumps
# Number of versions to keep
numversions=4
# Full path for MySQL hotcopy command
# Please put credentials into /root/.my.cnf
#hotcopycmd=/usr/bin/mysqlhotcopy
hotcopycmd="/usr/bin/mysqldump --lock-tables --databases"
# Create directory if needed
mkdir -p "$backupdir"
if [ ! -d "$backupdir" ]; then
echo "Invalid directory: $backupdir"
exit 1
fi
# Hotcopy begins here
echo "Dumping MySQL Databases..."
RC=0
for database in $dblist; do
echo
echo "Dumping $database ..."
mv "$backupdir/$database.gz" "$backupdir/$database.0.gz" 2> /dev/null
$hotcopycmd $database | gzip > "$backupdir/$database.gz"
RC=$?
if [ $RC -gt 0 ]; then
continue;
fi
# Rollover the backup directories
rm -fr "$backupdir/$database.$numversions.gz" 2> /dev/null
i=$numversions
while [ $i -gt 0 ]; do
mv "$backupdir/$database.`expr $i - 1`.gz" "$backupdir/$database.$i.gz" 2> /dev/null
i=`expr $i - 1`
done
done
if [ $RC -gt 0 ]; then
echo "MySQL Dump failed!"
exit $RC
else
# Hotcopy is complete. List the backup versions!
ls -l "$backupdir"
echo "MySQL Dump is complete!"
fi
exit 0
So this system has three parts. First is the file that holds the credentials, e.g. /root/.my.cnf and it‘s in standard mysql config format like so:
Second is the script itself, as listed above. Enter the names of your dbs, how many copies to keep, and where to put them.
Third is the crontab entry that will run this script for you. For example, once per day:
# ----------------- minute (0 - 59)
# | -------------- hour (0 - 23)
# | | ----------- day of month (1 - 31)
# | | | -------- month (1 - 12)
# | | | | ----- day of week (0 - 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
35 01 * * * /root/mysql_backup.sh
完!
原文:http://www.cnblogs.com/lonelyxmas/p/3565684.html
内容总结
以上是互联网集市为您收集整理的MySQL 备份全部内容,希望文章能够帮你解决MySQL 备份所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。