Linux|系统管理|WEB开发

关注Linux,系统管理,WEB开发以及开源世界

Amanda集中备份实例

| Comments

"Amanda Network Backup"
Amanda 高级马里兰自动网络磁盘归档 (Advanced Maryland Automatic Network Disk Archiver网络备份软件是我第一次接触Linux就听到过,只是从来都没有使用过,有需要备份得时候,就是自己写写简单的脚本,使用cron来做定时任务,也能基本满足需求。虽然曾经起心想用Amanda,但限于没有适合的环境,只能作罢。

现在的情况有所不同了,有4,5台服务器需要备份,设计到不同的平台,其备份特性应该达到或者至少朝企业级标准靠拢,因此,这几天看了看Amanda的文档,做了一些测试,目前在我的环境下配置成功,特记录下来和大家分享。

这次打算分两篇文档介绍,本篇是介绍文件备份,下一篇将介绍针对MySQL数据库的备份方法。 这次配置的大致流程如下:

  1. 安装和配置Amanda备份服务器
  2. 安装Amanda客户端
  3. 配置服务器和客户端参数
  4. 检验配置
  5. 校验备份
  6. 测试恢复

下面的列表给出这次测试环境的网络和机器配置

机器名

要备份的文件

OS

是否压缩

说明

cp1.domain.com

-

Ubuntu 8.10

Yes

Amanda服务端

cp2.domain.com

/web/websites

Asianux 3.0

Yes

客户端,运行大量网站

cp3.domain.com

/opt/www

CentOS 5.3

Yes

IDC服务器,大量客户站点

一、安装和配置Amanda服务

首先从官方站点现在对应OS的最新Amanda版本,我写这篇帖子的时候,针对Ubuntu 8.10的最新版本是2.6.1p1,可以从 http://www.zmanda.com/download-amanda.php 找到适合里的版本,包括服务端和客户端。

以下操作cp1机器上执行,如果没有特别说明,使用的帐号为root。

因为ubuntu仓库里自带的版本比较低,而且与官方版本在配置方面有些不同,所以建议先卸载自带的版本,然后安装下载的版本。 Amanda服务的启动依赖xinetd超级服务,编辑或者创建/etc/xinetd.d/amandaserver文件,内容应该像下面这个样子:

{% include_code amanda/amandaserver lang:conf %}

重启xinetd服务 /etc/init.d/xinetd restart

二、安装客户端

用root登录cp2机器,下载对应的客户端并安装 File /Users/wgzhao/Sites/blog.wgzhao.com/source/downloads/code/amanda/amanda_client_install could not be found

[编辑或者创建/etc/xinetd.d/amandaclient,内容如下:

File /Users/wgzhao/Sites/blog.wgzhao.com/source/downloads/code/amanda/amandaclient could not be found

重启xinetd服务 /etc/init.d/xinetd reload

切换到amandabackup用户,编辑/var/lib/amanda/.amandahosts文件,增加下面一行
cp1.domain.com amandabackup amdump
这表示允许cp1服务器能够连接到在cp2机器上的amanda客户端,指令如下:

-su-3.2$ echo cp1.domain.com amandabackup amdump >> /var/lib/amanda/.amandahosts
-su-3.2$ chmod 700 /var/lib/amanda/.amandahosts

amandabackup用户的主目录创建一个隐藏的密码文件,并设置合理的权限

-su-3.2$ chown amandabackup:disk ~amandabackup/.am_passphrase
-su-3.2$ chmod 700 ~amandabackup/.am_passphrase

到此,在cp2上的amanda客户端配置完成。
上述步骤在cp3服务器上同样执行一遍。

三、配置备份参数

amandabackup帐号登录cp1机器,创建Amanda配置目录

root@cp etc:# su - amandabackup -c mkdir /etc/amanda/FileBackup

拷贝/var/lib/amanda/example/amanda.conf文件到/etc/amanda/FileBackup/目录,这个amanda.conf文件就是Amanda服务最重要的配置文件了。

当前这个配置文件有700多行,不过大部分是注释,对每一行配置参数给出比较详尽的解释,下面我指列出需要修改的一些配置参数,没有提到的,可以不用管他。

打开amanda.conf文件,首先编辑下面这些内容:

org "Xplore Backup center"                      # your organization name for reports
mailto "root@localhost"                        # space separated list of operators at your site
tpchanger "chg-disk"                           # the tape-changer glue script
tapedev "file://yourpath/FileBackup/slots"  # the no-rewind tape device to be used
tapetype HARDDISK                              # use hard disk intead of tapes (vtape config)
labelstr "^FileBackup-[0-9][0-9]*$" # label constraint regex: all tapes must match

我们这里用的是虚拟磁带,下面我们要定义虚拟磁带的大小,接着编辑文件,增加下面几行内容:

define tapetype HARDDISK {
 comment "hard disk as virtual tape"
 length 20480 mbytes
}

接下来,我们需要定义认证模式,找到define dumptype global区段,在}之前,增加 auth "bsdtcp" 一行
切换到root帐号,创建备份缓存目录,并设置正确的拥有关系和权限.

root@cp1:~#mkdir -p /dumps/amanda
root@cp1:~#chown amandabackup:disk /dumps/amanda 
root@cp1:~#chmod 750 /dumps/amanda 

接下来,我们需要创建虚拟磁带目录,在amanda.conf文件里,默认定义了缺省的磁带机槽位数为25个。

root@cp1:~#mkdir -p /yourpath
root@cp1:~#chown amandabackup:disk /yourpath
root@cp1:~#chmod 750 /yourpath
root@cp1:~# su - amandabackup
-su-3.2$ touch /etc/amanda/FileBackup/tapelist
-su-3.2$ mkdir -p /yourpath/FileBackup/slots
-su-3.2$ cd /yourpath/FileBackup/slots
-su-3.2$ mkdir slot{1..25}
-su-3.2$ ln -s slot1 data

当前的磁带均没有打标签,在使用之前一定要打好标签,,我们设定了定义磁带标签的规则(labelstr "\^FileBackup-[0-9][0-9]\*$") 可以使用下面的指令一次性打好标签

{% include_code amanda/makelabel lang:bash %}

接下来,我们做一个测试,看看这些槽位状态如何:

File /Users/wgzhao/Sites/blog.wgzhao.com/source/downloads/code/amanda/slots_status could not be found

看上去一切正常,接下来,需要重置槽位

-su-3.2$ amtape FileBackup reset changer: got exit: 0
        str:1 amtape: changer is reset,slot 1 is loaded

接下来我们创建/etc/amanda/FileBackup/disklist文件,这个文件包括了所有客户端需要备份的数据定义,内容如下:

cp3.domain.com /opt/www/sites  comp-user-tar
cp2.domain.com  /web/websites   comp-user-tar

最后,我们用amandabackup帐号编辑/var/lib/amanda/.amandahosts文件,增加下面的几行,表示当做恢复的时候,客户端可以连接到备份服务端。

cp2.domain.com root amindexd amidxtaped
cp3.domain.com root amindexd amidxtaped
cp1.domain.com root amindexd amidxtaped
cp1.domain.com amandabackup amdump

四、测试

现在是测试的时候了,用amandabackup帐号登录cp1机器,运行amcheck程序来校验当前的配置是否已经可以运行备份了。

-su-3.2$ amcheck FileBackup
Amanda Tape Server Host Check
-----------------------------
Holding disk /dumps/amanda: 16777216 KB disk space available, using 16674816 KB
slot 7:read label `FileBackup-07', date `X'.
NOTE: skipping tape-writable test
Tape FileBackup-07 label ok
NOTE: host info dir /etc/amanda/FileBackup/curinfo/cp1.domain.com does not exist
NOTE: it will be created on the next run.
NOTE: index dir /etc/amanda/FileBackup/index/cp1.domain.com does not exist
NOTE: it will be created on the next run.
Server check took 1.633 seconds

Amanda Backup Client Hosts Check
--------------------------------
Client check: 3 hosts checked in 2.086 seconds.  0 problems found.

(brought to you by Amanda 2.6.1p1)

上面的测试结果表示一切正常,可以执行真正的备份了,还是在cp1机器上,用amandabackup帐号执行amdump命令amdump FileBackup
这个命令没有任何输出,除非执行过程中报错,执行完后,将会发一封有关备份状态的细节信息邮件给amanda.conf里定义的收件人,内容大致如下:

File /Users/wgzhao/Sites/blog.wgzhao.com/source/downloads/code/amanda/email could not be found

我们也可以运行amadmin命令来查询刚才备份的摘要信息

File /Users/wgzhao/Sites/blog.wgzhao.com/source/downloads/code/amanda/amadmin could not be found

看上去一切都不错,那就把备份加入到定时任务里吧。
amandabackup帐号创建一个你自己需要的备份策略,类似下面这样:

-su-3.2$ crontab  -l
# m h  dom mon dow   command
0 3 * * * /usr/sbin/amdump FileBackup

到此,关于配置和校验我们都已经完成了,接下来我们完整最后一个最重要的步骤,那就是我们需要校验这些备份数据是否能够恢复,如果不能恢复,备份功能再强大,策略再好,那也没有任何含义。

五、恢复

假定我们需要恢复cp3上一些丢失的数据,首先用amandabackup帐号登录cp2机器,创建/etc/amanda/amanda-client.conf文件,内容如下:

File /Users/wgzhao/Sites/blog.wgzhao.com/source/downloads/code/amanda/amanda-client.conf could not be found

然后使用root帐号运行amrecover命令来初始化data恢复过程

[root@cp3 ~]# amrecover
AMRECOVER Version 2.6.1p1. Contacting server on cp1.domain.com ...
220 cp1 AMANDA index server (2.6.1p1) ready.
Setting restore date to today (2009-04-13)
200 Working date set to 2009-04-13.
200 Config set to FileBackup.
200 Dump host set to cp3.domain.com.
Use the setdisk command to choose dump disk to recover
amrecover>

接下的一些命令演示如何将特定的一些数据恢复到指定的目录(这里指定/tmp)

File /Users/wgzhao/Sites/blog.wgzhao.com/source/downloads/code/amanda/amrecover could not be found

我们看看/tmp下面是不是已经有了我们刚才恢复的数据:

[root@cp3 ~]# tree  /tmp/admin/domain.com/subdomains/ |more
/tmp/admin/domain.com/subdomains/
|-- 404
|   |-- bin
|   |   |-- bash
|   |   |-- cat
|   |   |-- echo
|   |   |-- false
|   |   |-- gunzip
|   |   |-- ls
|   |   |-- mkdir
|   |   |-- mv
|   |   |-- pwd
|   |   |-- rm
|   |   |-- scp
|   |   |-- sftp
|   |   |-- sh
|   |   |-- tar
|   |   |-- unzip
|   |   `-- zip
|   |-- cgi-bin
|   |-- dev
|   |   |-- null
|   |   |-- random
|   |   `-- urandom
......

Ok,到此为止,Amanda备份配置基本完成,当然我们很多的配置都是基于缺省定义,比如槽位数,备份周期等,更多详细的定义,可以参考/usr/lib/amanda/example/amanda.conf文件。

Comments