<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>船長日誌 &#187; logrotate</title>
	<atom:link href="http://www.cslog.cn/tag/logrotate/feed/zh-hant/" rel="self" type="application/rss+xml" />
	<link>http://www.cslog.cn</link>
	<description>最讓我激動的是不知道下一個星球上能發現什麼...</description>
	<lastBuildDate>Wed, 30 Jul 2025 16:06:05 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>linux下使用logrotate自動備份mysql數據庫</title>
		<link>http://www.cslog.cn/Content/logrotate-mysql-automated-backu/zh-hant/</link>
		<comments>http://www.cslog.cn/Content/logrotate-mysql-automated-backu/zh-hant/#comments</comments>
		<pubDate>Fri, 12 Nov 2010 15:31:19 +0000</pubDate>
		<dc:creator>船長</dc:creator>
				<category><![CDATA[站長文檔]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[logrotate]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[數據備份]]></category>
		<category><![CDATA[數據庫]]></category>

		<guid isPermaLink="false">http://www.cslog.cn/?p=568</guid>
		<description><![CDATA[logrotate是linux下的一個日誌文件管理工具。apache,nignx &#8230; <a href="http://www.cslog.cn/Content/logrotate-mysql-automated-backu/zh-hant/">繼續閱讀 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>logrotate是linux下的一個日誌文件管理工具。apache,nignx或系統的log文件如不處理， 正常情況下會慢慢變的大， 久了， 硬盤就會爆掉。 logrotate（配合cron）就是用來周期性分割、壓縮、刪除日誌文件用的。前段時間在找mysql數據庫的自動備份方法的時候， 在 <a href="http://islandlinux.org/howto/automated-mysql-backups">http://islandlinux.org/howto/automated-mysql-backups</a> 看到了使用logrotate來備份mysql數據庫的偏方， 感覺不錯， 在這裡和大家分享一下。</p>
<p><span id="more-568"></span></p>
<p>首先， 先創建一個腳本文件</p>
<pre>nano /usr/local/sbin/mysql_auto_backup.sh</pre>
<p>貼入如下內容</p>
<pre>#!/bin/sh
#
# 作者 Dallas Vogels 2008-10-01
#
export PATH=/bin:/usr/bin:/sbin:/usr/sbin

OUTPUTDIR="/root/mysql-backups" #將這裡修改成你要放備份文件的目錄
OPTIONS="--all --complete-insert --add-drop-table --extended-insert --quote-names"
CONFIG_FILE="/root/.my.cnf.backup"

# 檢查備份目錄是否存在
if [ ! -d $OUTPUTDIR ]; then
        mkdir $OUTPUTDIR
fi

# 獲取數據庫列表
DATABASES=`echo "SHOW DATABASES" | mysql --defaults-file="$CONFIG_FILE" mysql`

for DATABASE in $DATABASES; do
# 不備份 Database 和 information_schema 這兩個數據庫
  if [ "$DATABASE" != "Database" -a "$DATABASE" != "information_schema" ]; then
    # 數據庫備份開始
    mysqldump --defaults-file="$CONFIG_FILE" $OPTIONS $DATABASE &gt; $OUTPUTDIR/$DATABASE.sql
  fi

done

exit 0</pre>
<p>將這個備份腳本添加可執行屬性：</p>
<pre>sudo chmod og-rwx /usr/local/sbin/mysql_auto_backup.sh</pre>
<p>運行mysql</p>
<pre>mysql -u root -p</pre>
<p>給mysql添加專門用來備份數據庫的用戶：</p>
<pre>GRANT SELECT, LOCK TABLES ON *.* TO mysqlbackup@localhost IDENTIFIED BY 'password';
FLUSH PRIVILEGES;</pre>
<p>注意將password換成你想要設定的密碼。</p>
<p>新創建一個mysql配置文件：</p>
<pre>nano /root/.my.cnf.backup</pre>
<p>將這個mysql備份用戶信息加到裡面：</p>
<pre>[client]
user="mysqlbackup"
password="password"</pre>
<p>注意再次換密碼。</p>
<p>設置好這個文件的屬性：</p>
<pre>chmod og-rwx /root/.my.cnf.backup</pre>
<p>現在可以測試一下數據備份腳本了，運行：</p>
<pre>/usr/local/sbin/mysql_auto_backup.sh</pre>
<p>如果可以在設定的目錄（/root/mysql-backups）找到mysql數據庫dump出來的sql文件，那到目前為止一切正常，繼續。</p>
<p>創建用來自動備份mysql數據庫的logrotate配置文件：</p>
<pre>nano /etc/logrotate.d/mysql-backups</pre>
<p>貼入如下內容:</p>
<pre>/root/mysql-backups/*.sql {
  weekly
  copy
  missingok
  rotate 30
  compress
  notifempty
  create 640 root adm
  sharedscripts
  prerotate
    /usr/local/sbin/mysql_auto_backup.sh
  endscript
}</pre>
<p>上面的設置會每周一次地將mysql中所有除Database和information_schema之外的數據庫自動dump到/root/mysql-backups/目錄中， 並自動將dump文件壓縮成gz格式(如果不希望壓縮，去掉上面的compress行）。<br />
如果希望將數據備份周期改到每天一次， 直接將上面logrotate配置文件weekly改成daily就可以了（每月一次是monthly)<br />
上面的腳本會自動保留最近30個備份（注意rotate 30參數， 修改30可設置保留的備份數）</p>
<p>最後測試一下logrotate配置文件有沒有問題，強制運行logrotate：</p>
<pre>logrotate -f /etc/logrotate.d/mysql-backups</pre>
<p>這時/root/mysql-backups/目錄應該出來gz格式的mysql數據庫備份文件。</p>
<p>這時用logrotate的自動mysql數據庫的備份部署完成了。這樣每星期到/root/mysql-backups/目錄下載mysql備份文件就可了。
<div style="margin-top: 15px; font-style: italic">
<p>轉載請註明: 轉自<a href="http://www.cslog.cn/">船長日誌</a>, 本文鏈接地址: <a href="http://www.cslog.cn/Content/logrotate-mysql-automated-backu/zh-hant/">http://www.cslog.cn/Content/logrotate-mysql-automated-backu/zh-hant/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/logrotate-mysql-automated-backu/feed/zh-hant/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- WP Chinese Conversion Full Page Converted -->