[Linux] Backup MySQL with BASH

I’ve translated my old post into English version with Italic font type.
這是一個簡單的範例程式,用來備份mysql用的。
This is a simple example program for MySQL backup.
以下假設我的MySQL使用者帳號user密碼123database名稱為db1
Assume that my MySQL account is user, password is 123 and the database name is db1.
backupmysql.sh:
=Edit the following content=
#!/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” > your_backup_directory/$db_name$date.sql
=End of editing =
然後將這支bash程式權限設定為 700 (chmod 700 backupmysql.sh)。
Change permission of this back program into 700 via “chmod 700 backupmysql.sh” command.
如果你要讓它定期幫你備份的話,加入crontab就好囉。
If you want this program do backup procedure periodically, you can add it into crontab. 
例如我要讓它每七天都幫我備份的話,只要加入以下:
For example, I want to backup every 7 days, and I just add the following content via “crontab -e” command.
#Allen: Execute backupmysql.sh automatically at 1:00 for every 7 days.
0 1 */7 * * backupmysql.sh
This entry was posted in Linux, 程式設計. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *