Home  >  Article  >  Database  >  Ubuntu Server使用automysqlbackup脚本_MySQL

Ubuntu Server使用automysqlbackup脚本_MySQL

WBOY
WBOYOriginal
2016-06-01 13:32:181140browse

UbuntuServerUbuntu

bitsCN.com

Ubuntu Server使用automysqlbackup脚本

 

昨天服务器意外关闭,导致开发环境数据库文件损坏,所以想起了做自动备份的事情,原本是打算自己用Mysqldump写个脚本来做计划任务的,不过出于偷懒的角度考虑,在网上找了找,发现了automysqlbackup,看评论都还不错,而且本身也是shell脚本实现的。本着不重复造轮子的精神,就下载来使用了。

 

地址:http://sourceforge.net/projects/automysqlbackup/

 

选择的版本是最新的automysqlbackup-v3.0_rc6.tar.gz。

 

安装非常简单,比较麻烦的地方,是后面配置邮件客户端,以及脚本中的一个小bug。

 

1、在任意目录下,解压文件包

 

tar –xzf automysqlbackup-v3.0_rc6.tar.gz

 

2、省事起见,切个权限

 

sudo su -

 

3、安装(若对路径没有什么特别的要求,出现提示回车即可)

 

./install.sh

默认配置文件位置:/etc/automysqlbackup

 

默认sh文件位置:/usr/local/bin

 

4、修改配置文件

 

安装完成,默认会有两个配置文件,一个是automysqlbackup.conf,还有一个是myserver.conf。

 

实际上看了安装脚本就知道,这两个文件本来就是拷贝冗余了一个出来,猜测用途是防止二次安装或者误操作的情况下,误操作覆盖已经配置好的配置文件。不过从脚本逻辑上看起来,貌似没太大用,因为他重装,照样两个文件一块会覆盖。不过如果发现automysqlbackup.conf配置有值的话,会给出提示就是了。

 

依旧,为了偷懒期间,就直接修改automysqlbackup.conf了,毕竟这是默认配置,sh执行的时候,不用带任何参数。

 

主要需要注意的配置,有以下几个:(配置文件中,带有默认值,如果不需要更改,直接保持被注释的状态就好)

 

# Username to access the MySQL server e.g. dbuser

(数据库连接用户名)

CONFIG_mysql_dump_username='root'

 

# Password to access the MySQL server e.g. password(数据库连接密码)

 

CONFIG_mysql_dump_password='密码'

 

 

# "Friendly" host name of MySQL server to be used in email log

# if unset or empty (default) will use CONFIG_mysql_dump_host instead(dump的时候,数据库host显示的别名,不配的话,基本就都是localhost之流了)

CONFIG_mysql_dump_host_friendly='DataBaseAlias'

 

 

# Backup directory location e.g /backups(不用多说了,重中之重,备份路径)

CONFIG_backup_dir='/srv/backup/mysql'

 

 

# List of databases for Daily/Weekly Backup e.g. ( 'DB1' 'DB2' 'DB3' ... )

# set to (), i.e. empty, if you want to backup all databases(需要日备份和周备份的数据库名称)

CONFIG_db_names=('TestDB')

 

 

# List of databases for Monthly Backups.

# set to (), i.e. empty, if you want to backup all databases(需要月备份的数据库名称)

CONFIG_db_month_names=('TestDB')

 

# Which day do you want monthly backups? (01 to 31) (月备份的日期,如果配置的日期大于当月最大日期,会在当月最后一天的时候执行)

# If the chosen day is greater than the last day of the month, it will be done

# on the last day of the month.

# Set to 0 to disable monthly backups.

#CONFIG_do_monthly="01"

 

# Which day do you want weekly backups? (1 to 7 where 1 is Monday)(周备份在星期几发生)

# Set to 0 to disable weekly backups.

#CONFIG_do_weekly="5"

 

# Set rotation of daily backups. VALUE*24hours

# If you want to keep only today's backups, you could choose 1, i.e. everything older than 24hours will be removed.(日备份文件保留多少天)

CONFIG_rotation_daily=7

 

# Set rotation for weekly backups. VALUE*24hours(周备份文件保留多少天)

#CONFIG_rotation_weekly=35

 

# Set rotation for monthly backups. VALUE*24hours(月备份文件保留多少天)

#CONFIG_rotation_monthly=150

 

# Use ssl encryption with mysqldump?(是否使用ssl,本地的话,开ssl找抽?远程就另算了)

CONFIG_mysql_dump_usessl='no'

 

 

# What would you like to be mailed to you?(是不是想要备份完的时候,自动给你发个邮件?)

# - log   : send only log file(邮件内容仅包含日志)

# - files : send log file and sql files as attachments (see docs)(邮件内容除了日志意外,把备份的压缩包也发给你)

# - stdout : will simply output the log to the screen if run manually.(表邮件了,屏幕显示下就好。问题是谁吃饱没事一天到晚盯着server屏幕看?)

# - quiet : Only send logs if an error occurs to the MAILADDR.(出错了的话,给个信儿就好)

CONFIG_mailcontent='files'

 

# Email Address to send mail to? (user@domain.com)(mail地址咯,不用多说了吧)

CONFIG_mail_address='nc@na.com'

 

 

 

5、先改个脚本的小bug吧。。不过如果你的server上mail和mutt齐备,请忽略该步骤。

 

vi /usr/local/bin/automysqlbackup

到1026行,将:

 

 

if [[ "x$CONFIG_mailcontent" = 'xlog' || "x$CONFIG_mailcontent" = 'xquiet' ]]; then

      dependencies=( "${dependencies[@]}" 'mail' )

    elif [[ "x$CONFIG_mailcontent" = 'xfiles' ]]; then

      dependencies=( "${dependencies[@]}" 'mail' )

      if [[ "x$CONFIG_mail_use_uuencoded_attachments" != 'xyes' ]]; then

        dependencies=( "${dependencies[@]}" 'mutt' )

      fi

    fi

 

改为:

 

 

if [[ "x$CONFIG_mail_use_uuencoded_attachments" != 'xyes' ]]; then

      if [[ "x$CONFIG_mailcontent" = 'xlog' || "x$CONFIG_mailcontent" = 'xquiet' ]]; then

        dependencies=( "${dependencies[@]}" 'mutt' )

      elif [[ "x$CONFIG_mailcontent" = 'xfiles' ]]; then

          dependencies=( "${dependencies[@]}" 'mutt' )

        fi

        else

      if [[ "x$CONFIG_mailcontent" = 'xlog' || "x$CONFIG_mailcontent" = 'xquiet' ]]; then

        dependencies=( "${dependencies[@]}" 'mail' )

      elif [[ "x$CONFIG_mailcontent" = 'xfiles' ]]; then

          dependencies=( "${dependencies[@]}" 'mail' )

        fi

    fi

 

 

 

6、安装邮件客户端 mutt+msmtp

 

apt-get install mutt

安装mutt的时候,会自动安装postfix用作mail server,问题是这玩意太大,出于比较有洁癖的心态,我就主动把它干掉了。毕竟小巧玲珑的msmtp在那等着我呢。(看个人需求和喜好而定)

 

apt-get remove postfix –purge

apt-get autoremove –purge

 

安装msmtp

 

apt-get install msmtp

安装完了,来配邮件吧。

 

 

 

7、邮件客户端配置

 

vi /etc/Muttrc

最上面加上如下:

 

 

#My Mail Setting

set sendmail="/usr/bin/msmtp"

set use_from=yes

set realname="邮件地址别名"

set from=username@126.com

set envelope_from=yes

#不保留sent的本地存档

set copy = no

 

然后到~目录下,新建个.msmtprc

 

cd ~

vi .msmtprc

加上如下内容:

 

 

account 邮箱别名

host smtp.126.com

from username@126.com

auth plain

user username

password pwd

logfile ~/.msmtp.log

 

因为这里是明文密码,所以建议修改个600之流,再增加个日志文件

 

chmod 600 .msmtprc

touch ~/.msmtp.log

这全部完成了的话,可以试试邮件发不发的出去了。

 

echo "喂" |mutt -s "发出去没?" testmail@126.com

测试成功的话,就跑一下备份脚本看看吧

 

 

8、装上几个我服务器上木有的命令:

 

apt-get install pax

apt-get install pigz

 

 

9、mysql如果之前没设环境变量,只是加了个快捷方式的话,设个环境变量

 

vi  /etc/profile

在最后的位置加入

 

export  PATH=$PATH:/usr/local/mysql/bin

保存,然后再刷新检查下

 

source  /etc/profile 

echo $PATH

 

 

10、启动下备份脚本试试了,对了,记得要先创建备份路径

 

mkdir /srv/backup

mkdir /srv/backup/mysql

automysqlbackup

如果没什么问题的话,就会看到备份文件路径下面,自动给你创建好了几个目录:

 

daily  fullschema  latest  monthly  status  tmp  weekly

 

而且打开daily,里面应该就会有刚才备份的文件了。

 

再去check一下mail,都ok,那就配个计划任务就完事了。

 

 

 

11、配置计划任务

 

crontab –l

先查查看目前有没有任务,没有的话,就在~建个配置文件

 

cd ~

vi root-crontab

内容添上

 

30 23 * * * /usr/local/bin/automysqlbackup

 

表示每天23点30,自动运行备份脚本

 

然后再添加到计划任务里面去

 

crontab -u root root-crontab

齐活。。。

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn