备份MySQL数据库的Bash脚本
【如果你管理有运行在在Apache/MySQL/PHP栈上自己的博客或基于Web的应用,你应当有一个备份系统,以保证MySQL数据库中数据的安全。当然有一些好办法,但没一个比得上一个简单的Bash脚本。那是我再一则博客评论上偶然发现的。一下就是美尽在其中的那一脚本:】
#!/bin/bashNOW=`date +"%Y-%m"`;BACKUPDIR="location/of/your/backup/dir/$NOW";### Server Setup ####* MySQL login user name *#MUSER="user";#* MySQL login PASSWORD name *#MPASS="pass";#* MySQL login HOST name *#MHOST="your-mysql-ip";MPORT="your-mysql-port";# DO NOT BACKUP these databasesIGNOREDB="information_schemamysqltest"#* MySQL binaries *#MYSQL=`which mysql`;MYSQLDUMP=`which mysqldump`;GZIP=`which gzip`;# assuming that /nas is mounted via /etc/fstabif [ ! -d $BACKUPDIR ]; then mkdir -p $BACKUPDIRelse :fi# get all database listingDBS="$(mysql -u $MUSER -p$MPASS -h $MHOST -P $MPORT -Bse 'show databases')"# SET DATE AND TIME FOR THE FILENOW=`date +"d%dh%Hm%Ms%S"`; # day-hour-minute-sec format# start to dump database one by onefor db in $DBSdo DUMP="yes"; if [ "$IGNOREDB" != "" ]; then for i in $IGNOREDB # Store all value of $IGNOREDB ON i do if [ "$db" == "$i" ]; then # If result of $DBS(db) is equal to $IGNOREDB(i) then DUMP="NO"; # SET value of DUMP to "no" #echo "$i database is being ignored!"; fi done fi if [ "$DUMP" == "yes" ]; then # If value of DUMP is "yes" then backup database FILE="$BACKUPDIR/$NOW-$db.gz"; echo "BACKING UP $db"; $MYSQLDUMP --add-drop-database --opt --lock-all-tables -u $MUSER -p$MPASS -h $MHOST -P $MPORT $db | gzip > $FILE fidone
The best part is that you only need to specify a handful of parameters to make the work. This includes BACKUPDIR (the destination for storing backups), MUSER (MySQL user), MPASS (MySQL user password), MHOST (the IP address of the MySQL server, e.g. localhost), and MPORT (the port the MySQL database is running on, default is 3306).
【它的最大优点在于,在运行脚本之前,你只需要定义一些参数。这包括BACKUPDIR(存储本分的目录),MUSER(MySQL用户),MPASS(MySQL用户密码),MHOST(MySQL服务器IP地址,如:localhost),和MPORT(MySQL数据库工作的端口,默认是3306)。】
You can run the manually, or you can set up a cron job which will perform backups on a regular basis. To do this, run the crontab -ecommand and add the following line (replace the sample path with the actual path and backup name):
【你可以手动运行这一脚本,或者设置一个计划作业(a cron job)以使备份按计划自动进行。设置计划作业的方法是,运行crontab -e命令,并加入以下代码(请用实际路径和备份脚本名替换示例路径):】
@daily /path/to/mysqlbackup.sh
Don't forget to make the executable using the chmod a+x mysqlbackup.sh command.
【别忘了使用chmod a+x mysqlbackup.sh命令将脚本可执行化。】
关健词:Bash,脚本
新文章:
- CentOS7下图形配置网络的方法
- CentOS 7如何添加删除用户
- 如何解决centos7双系统后丢失windows启动项
- CentOS单网卡如何批量添加不同IP段
- CentOS下iconv命令的介绍
- Centos7 SSH密钥登陆及密码密钥双重验证详解
- CentOS 7.1添加删除用户的方法
- CentOS查找/扫描局域网打印机IP讲解
- CentOS7使用hostapd实现无AP模式的详解
- su命令不能切换root的解决方法
- 解决VMware下CentOS7网络重启出错
- 解决Centos7双系统后丢失windows启动项
- CentOS下如何避免文件覆盖
- CentOS7和CentOS6系统有什么不同呢
- Centos 6.6默认iptable规则详解