自動將資料庫等資料備份到Gmail

首先必須安裝 mutt,放在 /usr/ports/mail/mutt
然後就可以複製這份 script 來修改成你的版本

#!/bin/sh

##################################################
# Backup Mysql Datebase To Gmail ( for FreeBSD )
#
# Fixed by ajer001
##################################################
#where you want to save the backup files.
BACKUP_PATH=備份檔案的儲存路徑

#what mail you want to mail to.
RECEIVE_MAIL=填入e-mail account

#what your MySQL database server root's password.
MYSQL_ROOT_PASSWORD=MySQL資料庫伺服器的root密碼

#what database in MySQL server you want to backup.
MYSQL_BACKUP_DATABASE_1=欲備份MySQL內的第一個資料庫名稱
MYSQL_BACKUP_DATABASE_2=欲備份MySQL內的第二個資料庫名稱

#what backup filename you want to save.
MYSQL_BACKUP_DATABASE_FILENAME_1=欲儲存備份的第一個檔案名稱
MYSQL_BACKUP_DATABASE_FILENAME_2=欲儲存備份的第二個檔案名稱

#Script will add the system time to filename automatic
#1.Dump mysql database from mysql server
/usr/local/bin/mysqladmin -uroot -p$MYSQL_ROOT_PASSWORD flush-logs
/usr/local/bin/mysqldump $MYSQL_BACKUP_DATABASE_1 -uroot -p$MYSQL_ROOT_PASSWORD --opt > $BACKUP_PATH/$MYSQL_BACKUP_DATABASE_FILENAME_1.sql
/usr/local/bin/mysqldump $MYSQL_BACKUP_DATABASE_2 -uroot -p$MYSQL_ROOT_PASSWORD --opt > $BACKUP_PATH/$MYSQL_BACKUP_DATABASE_FILENAME_2.sql

#2.Compress the sql file
/usr/bin/tar -zcvpf $BACKUP_PATH/Mysql_Backup-`date +m-%d`.tar.gz $BACKUP_PATH/$MYSQL_BACKUP_DATABASE_FILENAME_1.sql $BACKUP_PATH/$MYSQL_BACKUP_DATABASE_FILENAME_2.sql
/bin/rm $BACKUP_PATH/$MYSQL_BACKUP_DATABASE_FILENAME_1.sql $BACKUP_PATH/$MYSQL_BACKUP_DATABASE_FILENAME_2.sql

#3.Use metasend to send the compressed file to a mail account
/usr/local/bin/mutt -s "Mysql_Backup-`date +m-%d`" -a $BACKUP_PATH/Mysql_Backup-`date +m-%d`.tar.gz $RECEIVE_MAIL < /dev/null
/bin/rm $BACKUP_PATH/Mysql_Backup-`date +m-%d`.tar.gz

因為我有兩個資料庫想備份,所以增加了一個備份的項目,如果不需要可以把相關的移除,只備份一個資料庫。並且改成 freebsd 的語法,還有改成 mutt 的來送信。

接著,只要 chmod 成可執行檔案,因為裡面有 mysql 密碼,所以要注意設定好相關權限,並且丟到 crontab 去跑即可。

如此就完成了,可以到你的 gmail 或其他信箱收收看是否正常,也可以加上規則。讓他自動幫你分類,方便管理。

此外,也可改變一下這份 script,讓他幫你備份其他重要檔案,例如 passwd、master.passwd等等重要的系統檔案。

#!/bin/sh

##################################################
# Backup File To Gmail ( for FreeBSD )
#
# Fixed by ajer001
##################################################
#where you want to save the backup files.
BACKUP_PATH=備份檔案的儲存路徑

#what mail you want to mail to.
RECEIVE_MAIL=填入e-mail account

#what file you want to backup
BACKUP_FILE_1=要備份的檔案路徑

#Script will add the system time to filename automatic
#1.Backup Files
/usr//bin/tar -zcvpf $BACKUP_PATH/File_Backup-`date +m-%d`.tar.gz $BACKUP_FILE_1

#3.Use metasend to send the compressed file to a mail account
/usr/local/bin/mutt -s "File_Backup-`date +m-%d`" -a $BACKUP_PATH/File_Backup-`date +m-%d`.tar.gz $RECEIVE_MAIL < /dev/null
/bin/rm $BACKUP_PATH/File_Backup-`date +m-%d`.tar.gz

From: http://ajer001.blog.twntwn.info/post/1/212

Trackback URL for this post:

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><blockquote>
  • Lines and paragraphs break automatically.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.
Web Hosting by Arvixe