首頁 > Linux&FreeBSD > Backup MySQL with bash

Backup MySQL with bash

這是一個簡單的範例程式,用來備份mysql用的。

以下假設我的MySQL使用者帳號user密碼123database名稱為db1

backupmysql.sh:

=編輯開始=

#!/bin/sh
#Program:
#      This code backup MySQL database with date.
#      Author: Allen
#History:
#      2009/1/1

#take date
date=`date +%Y%m%d`

#database name
db_name=’db1
db_user=‘user"
db_password=’123

mysqldump $db_name -u $db_user –password="$db_password" > 備份目錄/$db_name$date.sql

=編輯結束 =

然後將這支bash程式權限設定為 700 (chmod 700 backupmysql.sh)。

如果你要讓它定期幫你備份的話,加入crontab就好囉。

例如我要讓它每七天都幫我備份的話,只要加入以下:

#Allen: 每七天的01:00執行一次 backupmysql.sh ,自動備份資料檔案
0 1 */7 * * backupmysql.sh

分類:Linux&FreeBSD 標籤:
  1. 六月 2, 2012 at 2:34 上午

    You really make it seem so easy with your presentation but I find this matter
    to be really something which I think I would never understand.

    It seems too complicated and extremely broad for me. I’m looking forward for your next post, I will try to get the hang of it!

  2. 六月 3, 2012 at 6:47 下午

    Thanks for your reply. I’ve translated it into English version in my new web site and you can visit it on http://allen501pc.blogspot.tw/2012/06/linux-backup-mysql-with-bash.html

  1. No trackbacks yet.

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

%d 位部落客按了讚: