Chinaunix首页 | 论坛 | 博客
  • 博客访问: 93499
  • 博文数量: 41
  • 博客积分: 1905
  • 博客等级: 上尉
  • 技术积分: 335
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-10 20:07
文章分类

全部博文(41)

文章存档

2011年(1)

2010年(18)

2009年(22)

我的朋友

分类: LINUX

2009-07-13 17:17:34

Amanda集中备份实例

Amanda Network Backup(高级马里兰自动网络磁盘归档 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,可以从 找到适合里的版本,包括服务端和客户端。

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

因为ubuntu仓库里自带的版本比较低,而且与官方版本在配置方面有些不同,所以建议先卸载自带的版本,然后安装下载的版本。

Amanda服务的启动依赖xinetd超级服务,编辑或者创建/etc/xinetd.d/amandaserver文件,内容应该像下面这个样子:

# default: on
#
# description: Amanda services for Amanda server and client.
#
 
service amanda
{
        disable         = no
#        flags           = IPv6
        socket_type     = stream
        protocol        = tcp
        wait            = no
        user            = amandabackup
        group           = disk
        groups          = yes
        server          = /usr/libexec/amanda/amandad
        server_args     = -auth=bsdtcp amdump amindexd amidxtaped
}

重启xinetd服务
root@cp1:~# /etc/init.d/xinetd reload
* Reloading internet superserver configuration xinetd [ OK ]
root@cp1:~#

二、安装客户端

用root登录cp2机器,下载对应的客户端并安装

[root@cp2 /tmp]# rpm -i amanda-backup_client-2.6.1p1-1.rhel5.i386.rpm
 411 2009 12:16:32: Preparing to install: Amanda Community Edition - version
2.6.1p1
 411 2009 12:16:32: Checking for 'amandabackup' user...
 411 2009 12:16:32:
 411 2009 12:16:32:  The 'amandabackup; user account has been successfully cr
eated.
 4月 11 2009 12:16:32:  Furthermore, the account has been automatically locked f
or you
 4月 11 2009 12:16:32:  for security purposes.  Once a password for the  'amanda
backup'
 4月 11 2009 12:16:32:  account has been set, the user can be unlocked by issuin
g
 4月 11 2009 12:16:32:  the following command as root.:
 4月 11 2009 12:16:32:
 4月 11 2009 12:16:32:  # passwd -u amandabackup
 4月 11 2009 12:16:32:
 4月 11 2009 12:16:32:  If this is not a new installation of Amanda and you have
 4月 11 2009 12:16:32:  pre-existing Amanda configurations in /etc/amanda
 4月 11 2009 12:16:32:  you should ensure that 'dumpuser' is set to 'amandabacku
p'
 4月 11 2009 12:16:32:  in those configurations.  Additionally, you should ensur
e
 4月 11 2009 12:16:32:  that /var/lib/amanda/.amandahosts on your client systems
 4月 11 2009 12:16:32:  is properly configured to allow connections for the user
 4月 11 2009 12:16:32:  'amandabackup'.
 4月 11 2009 12:16:32:
 4月 11 2009 12:16:32:
 4月 11 2009 12:16:32: === Amanda backup client installation started. ===
 4月 11 2009 12:16:35: Updating system library cache...done.
 4月 11 2009 12:16:46: Reloading xinetd configuration...停止 xinetd:[失败]
启动 xinetd:[确定]
success.
 4月 11 2009 12:16:47: Installing '/var/lib/amanda/amandates'.
 4月 11 2009 12:16:47: The file '/var/lib/amanda/amandates' has been created.
 4月 11 2009 12:16:47: Ensuring correct permissions for '/var/lib/amanda/amandat
es'.
 4月 11 2009 12:16:47: '/var/lib/amanda/amandates' Installation successful.
 4月 11 2009 12:16:47: Checking '/var/lib/amanda/.amandahosts' file.
 4月 11 2009 12:16:47: Checking '/etc/amanda/amanda-client.conf' file.
 4月 11 2009 12:16:47: Installing '/var/lib/amanda/.gnupg'.
 4月 11 2009 12:16:47: '/var/lib/amanda/.gnupg' will be created.
 4月 11 2009 12:16:47: The directory '/var/lib/amanda/.gnupg' created successful
ly.
 4月 11 2009 12:16:47: Ensuring correct permissions for '/var/lib/amanda/.gnupg'
.
 4月 11 2009 12:16:47: '/var/lib/amanda/.gnupg' Installation successful.
 4月 11 2009 12:16:47: Creating directory '/var/lib/amanda/.ssh'.
 4月 11 2009 12:16:47: Creating ssh RSA key in '/var/lib/amanda/.ssh/id_rsa_amre
cover'
 4月 11 2009 12:16:48: Setting permissions for '/var/lib/amanda/.ssh' and '/var/
lib/amanda/.ssh/id_rsa_amrecover*'
 4月 11 2009 12:16:48: Checking for '/var/lib/amanda/.profile' and ensuring corr
ect environment.
 4月 11 2009 12:16:48: Setting ownership and permissions for '/var/lib/amanda/.p
rofile'
 4月 11 2009 12:16:48: Sending anonymous distribution and version information to
 Zmanda
 4月 11 2009 12:16:48: Setting ownership and permissions for '/var/lib/amanda/.p
rofile'
 4月 11 2009 12:16:53: === Amanda backup client installation complete. ===

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

# default: on
#
# description: The Amanda backup client should be enabled for systems
#              which will be backed up by an Amanda backup server.
 
service amanda
{
	disable		= no
	flags           = IPv6
	socket_type	= stream
	protocol	= tcp
	wait		= no
	user		= amandabackup
	group		= disk
	groups		= yes
	server		= /usr/libexec/amanda/amandad
	server_args     = -auth=bsdtcp amdump
}

重启xinetd服务

[root@cp2:~]# /etc/init.d/xinetd reload
重新载入配置:                                             [确定]

切换到amandabackup用户,编辑/var/lib/amanda/.amandahosts文件,增加下面一行
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
-bash-3.00$ mkdir /etc/amanda/FileBackup

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

-su-3.2$ cp /var/lib/amanda/example/amanda.conf /etc/amanda/FileBackup /

当前这个配置文件有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]*$”)

-su-3.2$ for (( i=1;$i<=9;i++)); do amlable FileBackup FileBackup-0$i slot $i;done
labeling tape in slot 1 (file://yourpath/FileBackup/slots):
Reading label...
Found an empty tape.
Writing label FileBackup-01..
Checking label...
Success!
labeling tape in slot 2 (file://yourpath/FileBackup/slots):
Reading label...
Found an empty tape.
Writing label FileBackup-02..
Checking label...
Success!
.......
 
-su-3.2$ for ((i=10;$i<=25;i++));do amlabel FileBackup FileBackup-$i slot $i;done
labeling tape in slot 10 (file://yourpath/FileBackup/slots):
Reading label...
Found an empty tape.
Writing label FileBackup-10..
Checking label...
Success!
labeling tape in slot 11 (file://yourpath/FileBackup/slots):
Reading label...
Found an empty tape.
Writing label FileBackup-11..
Checking label...
Success!
.......

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

amtape FileBackup show
amtape: scanning all 25 slots in tape-changer rack:
slot   5: time 20090413030002 label FileBackup-05
slot   6: time X              label FileBackup-06
slot   7: time X              label FileBackup-07
slot   8: time X              label FileBackup-08
slot   9: time X              label FileBackup-09
slot  10: time X              label FileBackup-10
slot  11: time X              label FileBackup-11
slot  12: time X              label FileBackup-12
slot  13: time X              label FileBackup-13
slot  14: time X              label FileBackup-14
slot  15: time X              label FileBackup-15
slot  16: time X              label FileBackup-16
slot  17: time X              label FileBackup-17
slot  18: time X              label FileBackup-18
slot  19: time X              label FileBackup-19
slot  20: time X              label FileBackup-20
slot  21: time X              label FileBackup-21
slot  22: time X              label FileBackup-22
slot  23: time X              label FileBackup-23
slot  24: time X              label FileBackup-24
slot  25: time 20090412193808 label FileBackup-25
slot   1: time 20090412194109 label FileBackup-01
slot   2: time 20090412194420 label FileBackup-02
slot   3: time 20090412194747 label FileBackup-03
slot   4: time 20090412195334 label FileBackup-04

看上去一切正常,接下来,需要重置槽位
-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命令
-su-3.2$amdump FileBackup
这个命令没有任何输出,除非执行过程中报错,执行完后,将会发一封有关备份状态的细节信息邮件给amanda.conf里定义的收件人,内容大致如下:

To: 
Subject: Backup Centre AMANDA MAIL REPORT FOR April 11, 2009
Message-Id:
From: Amanda
Date: Sat, 11 Apr 2009 18:06:49 +0800

Hostname: 
Org     : Backup Centre
Config  : FileBackup
Date    : April 11, 2009

These dumps were to tape FileBackup-01.
The next tape Amanda expects to use is: 1 new tape.
The next new tape already labelled is: DailySet1-01.

STATISTICS:
                          Total       Full      Incr.
                        --------   --------   --------
Estimate Time (hrs:min)    0:00
Run Time (hrs:min)         0:06
Dump Time (hrs:min)        0:05       0:05       0:00
Output Size (meg)        1539.6     1539.6        0.0
Original Size (meg)      2211.3     2211.3        0.0
Avg Compressed Size (%)    69.6       69.6        --
Filesystems Dumped            2          2          0
Avg Dump Rate (k/s)      5342.0     5342.0        -- 

Tape Time (hrs:min)        0:01       0:01       0:00
Tape Size (meg)          1539.6     1539.6        0.0
Tape Used (%)               7.5        7.5        0.0
Filesystems Taped             2          2          0

Chunks Taped                  2          2          0
Avg Tp Write Rate (k/s) 21897.1    21897.1        -- 

USAGE BY TAPE:
  Label             Time      Size      %    Nb    Nc
  FileBackup-01       0:01  1576584K    7.5     2     2

NOTES:
  planner: Adding new disk :/opt/www/sites.
  planner: Adding new disk :/web/websites.
  taper: tape DailySet1-1 kb 1576585 fm 2 [OK]
  small estimate:  /web/websites 0
                  est: 1100832K    out 1545837K

DUMP SUMMARY:
                                       DUMPER STATS               TAPER STATS
HOSTNAME     DISK        L ORIG-KB  OUT-KB  COMP%  MMM:SS   KB/s MMM:SS   KB/s
-------------------------- ------------------------------------- -------------
 -b/websites 0 2201540 1545837   70.2    4:51 5317.0   1:12 21536.4
 -/www/sites 0   62850   30747   48.9    0:04 6994.2   0:00 138465.9

(brought to you by Amanda version 2.6.1p1)

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

-su-3.2$ amadmin  FileBackup find
 
date                host         disk           lv tape or file                                               file part status
2009-04-12 19:26:33 cp2.domain.com /web/websites   1 /dumps/amanda/20090412192633
/cp2.domain.com._web_websites.1     0   -- OK
2009-04-12 19:32:29 cp2.domain.com /web/websites   1 /dumps/amanda/20090412193229
/cp2.domain.com._web_websites.1     0   -- OK
2009-04-12 19:53:34 cp2.domain.com /web/websites   0 FileBackup-04                                                 2  1/1 OK
2009-04-13 03:00:02 cp2.domain.com /web/websites   0 FileBackup-05                                                 2  1/1 OK
2009-04-13 17:49:11 cp2.domain.com /web/websites   1 FileBackup-06                                                 3  1/1 OK
2009-04-12 19:26:33 cp3.domain.com /opt/www/sites  1 /dumps/amanda/20090412192633
/cp3.domain.com._opt_www_sites.1    0   -- OK
2009-04-12 19:32:29 cp3.domain.com /opt/www/sites  1 /dumps/amanda/20090412193229
/cp3.domain.com._opt_www_sites.1    0   -- OK
2009-04-12 19:53:34 cp3.domain.com /opt/www/sites  0 FileBackup-04                                                 1  1/1 OK
2009-04-13 03:00:02 cp3.domain.com /opt/www/sites  1 FileBackup-05                                                 1  1/1 OK
2009-04-13 17:49:11 cp3.domain.com /opt/www/sites  0 FileBackup-06                                                 2  1/1 OK
2009-04-13 17:49:11 localhost    /etc            0 FileBackup-06                                                 1  1/1 OK

看上去一切都不错,那就把备份加入到定时任务里吧。
用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文件,内容如下:

#
# amanda.conf - sample Amanda client configuration file.
#
# This file normally goes in /etc/amanda/amanda-client.conf.
#
 
conf "FileBackup"		# your config name
 
index_server "cp1.domain.com"	# your amindexd server
tape_server  "cp1.domain.com"	# your amidxtaped server
#tapedev      "tape:/dev/YOUR-TAPE-DEVICE-HERE"	# your tape device
			# if not set, Use configure or ask server.
			# if set to empty string "", ask server
			# amrecover will use the changer if set to the value
			# of 'amrecover_changer' in the server amanda.conf.
 
#   auth	- authentication scheme to use between server and client.
#		  Valid values are "bsd", "bsdudp", "bsdtcp", "krb5", "local",
#		  "rsh" and "ssh".
#		  Default: [auth "bsdtcp"]
auth "bsdtcp"
 
#ssh_keys ""			# your ssh keys file if you use ssh auth

然后使用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)

amrecover> listdisk
200- List of disk for host cp3.domain.com
201- /opt/www/sites
200 List of disk for host cp3.domain.com
amrecover> setdisk /opt/www/sites
200 Disk set to /opt/www/sites.
amrecover> ls
2009-04-13-17-49-11 newltf/
2009-04-13-17-49-11 lawburn/
2009-04-13-17-49-11 hnnoritz/
2009-04-13-17-49-11 fireyang/
2009-04-13-17-49-11 admin/
2009-04-13-17-49-11 .htpasswd
2009-04-13-17-49-11 .
amrecover> cd admin
/opt/www/sites/admin
amrecover> ls
2009-04-13-17-49-11 var
2009-04-13-17-49-11 usr
2009-04-13-17-49-11 tmp
2009-04-13-17-49-11 sbin
2009-04-13-17-49-11 lib
2009-04-13-17-49-11 etc
2009-04-13-17-49-11 dev
2009-04-13-17-49-11 bin
2009-04-13-17-49-11 domain.com/
2009-04-13-17-49-11 .
amrecover> cd domain.com
/opt/www/sites/admin/domain.com
amrecover> ls
2009-04-13-17-49-11 var
2009-04-13-17-49-11 usr
2009-04-13-17-49-11 tmp
2009-04-13-17-49-11 subdomains/
2009-04-13-17-49-11 sbin
2009-04-13-17-49-11 mysql/
2009-04-13-17-49-11 lib
2009-04-13-17-49-11 etc
2009-04-13-17-49-11 dev
2009-04-13-17-49-11 bin
2009-04-13-17-49-11 Mailboxs/
2009-04-13-17-49-11 .
amrecover> add subdomains/
Added dir /admin/domain.com/subdomains/ at date 2009-04-13-17-49-11
amrecover> lcd /tmp
amrecover> extract
 
Extracting files using tape drive changer on host cp1.domain.com.
The following tapes are needed: FileBackup-06
 
Restoring files into directory /tmp
Continue [?/Y/n]? y
 
Extracting files using tape drive changer on host cp1.domain.com.
Load tape FileBackup-06 now
Continue [?/Y/n/s/d]? y
./admin/domain.com/subdomains/
./admin/domain.com/subdomains/404/
./admin/domain.com/subdomains/404/bin/
./admin/domain.com/subdomains/404/cgi-bin/
./admin/domain.com/subdomains/404/dev/
./admin/domain.com/subdomains/404/etc/
./admin/domain.com/subdomains/404/html/
./admin/domain.com/subdomains/404/lib/
./admin/domain.com/subdomains/404/libexec/
./admin/domain.com/subdomains/404/logs/
./admin/domain.com/subdomains/404/logs/2009/
./admin/domain.com/subdomains/404/logs/2009/04/
./admin/domain.com/subdomains/404/sbin/
./admin/domain.com/subdomains/404/tmp/
......

我们看看/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文件。

阅读(3637) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~