MySQL 备份

系统 2487 0

http://rimuhosting.com/howto/mysqlbackup.jsp

 

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:

mysqldump  -- compress  - u root  - p $pw   - $currenthost   -- add - drop - table  -- extended - insert  -- quote - names  -- databases db1 db2 |  mysql  - u root  - p $pw   - $remotehost

 

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:

mysqldump  - u root  - p $pw   - $currenthost   -- add - drop - table  -- extended - insert  -- quote - names  -- databases db1 db2  >   / var / log / mysql . backup . $(date  + " %Y%m%d " ) . sql 

 

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.

# !/bin/sh

# 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   - " $backupdir "
if  [  !   - " $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  - " $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:

#  cat /root/.my.cnf  [client] user=root password=whatever 

 

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:

#  (Use to post in the top of your crontab)
# ----------------- 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

 

 

完!

MySQL 备份


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论