Linux 文件系统管理 第9天

2012-09-11 22:28:47 0  category: linux

文件系统构成
/usr/bin、/bin:存放所有用户可以执行的命令
/usr/sbin、/sbin:存放只有 root 可以执行的命令
/home:用户缺省宿主目录
/proc:虚拟文件系统,存放当前进程信息
/dev:存放设备文件
/lib:存放系统程序运行所需的共享库
/lost+found:存放一些系统出错的检查结果
/tmp:存放临时文件
/etc :系统配置文件
/var:
包含经常发生变动的文件,
如邮件、
日志文件、
计划任务等/var/log /var/spool/mail
/var/spool/cron
/usr:存放所有命令、库、手册页等,类似 Windows 的 c:\windows
/boot:内核文件及自举程序文件保存位置
/mnt:临时文件系统的安装点
[root@helen ~]# ls -l /dev/hda
brw-r—– 1 root disk 3, 0 02-23 11:01 /dev/hda
b – block 块设备,以数据块为存储单位,硬盘、光盘
[root@helen ~]# ls -l /dev/tty
crw-rw-rw- 1 root tty 5, 0 02-23 11:01 /dev/tty
c – charset 字符设备,以字符为单位读写的设备,终端,打印机
[root@helen tmp]# mkdir /testdir
[root@helen tmp]# chmod 777 /testdir/
[root@helen ~]# touch /testdir/testfile
[root@helen ~]# su – samlee
[samlee@helen ~]$ rm /testdir/testfile
rm:是否删除有写保护的 一般空文件 “/testdir/testfile”? y
[samlee@helen ~]$ ls
[samlee@helen ~]$
[samlee@helen ~]$ ls -ld /tmp
drwxrwxrwt 5 root root 4096 02-23 11:21 /tmp
t – 黏着位(1)权限为 777 的目录设置黏着位,每个用户在目录下都可以创建文件,但只
可以删除自己是文件所有者的文件。
[root@helen ~]# ls -ld /testdir/
drwxrwxrwx 2 root root 4096 02-23 15:02 /testdir/
[root@helen ~]# touch /testdir/testfile2
[root@helen ~]# chmod o+t /testdir/
[root@helen ~]# ls -ld /testdir/
drwxrwxrwt 2 root root 4096 02-23 15:02 /testdir/
[root@helen ~]# su – samlee
[samlee@helen ~]$ rm /testdir/testfile2
rm:是否删除有写保护的 一般空文件 “/testdir/testfile2”? y
rm: 无法删除 “/testdir/testfile2”: 不允许的操作
[samlee@helen ~]$ touch /testdir/test
[samlee@helen ~]$ rm /testdir/test
[samlee@helen ~]$
常用命令
查看分区使用情况:df
[samlee@helen ~]$ df
文件系统
1K-块
已用
可用 已用%
/dev/hda1
5952252 1276536 4368476 23%
/dev/hda6
669080
17104
617440 3%
/dev/hda5
497829
10577
461550 3%
/dev/hda3
497861
10544
461613 3%
tmpfs
127704
0
127704 0%
[samlee@helen ~]$ df -h
文件系统
容量 已用 可用 已用% 挂载点
/dev/hda1
5.7G 1.3G 4.2G 23% /
/dev/hda6
654M 17M 603M 3% /web
/dev/hda5
487M 11M 451M 3% /home
/dev/hda3
487M 11M 451M 3% /test
tmpfs
125M
0 125M 0% /dev/shm
查看文件、目录大小:du
[root@helen ~]# du -h /home/
8.0K
/home/mysql
12K
/home/lost+found
4.0K
/home/samlee
26K
/home/
[root@helen ~]# du -h /home/samlee/apache_1.3.31.tar.gz
2.4M
/home/samlee/apache_1.3.31.tar.gz
挂载点
/
/web
/home
/test
/dev/shm
[root@helen ~]# du -sh /home
2.5M
/home
检测修复文件系统:fsck、e2fsck(单用户模式执行)
正常模式操作会造成文件丢失
fsck 硬盘名/分区名(物理设备名)
[root@helen ~]# fsck /dev/hda
fsck 1.39 (29-May-2006)
e2fsck 1.39 (29-May-2006)
fsck.ext2: Device or resource busy while trying to open /dev/hda
Filesystem mounted or opened exclusively by another program?
[root@helen ~]# fsck /dev/hda1
fsck 1.39 (29-May-2006)
e2fsck 1.39 (29-May-2006)
/dev/hda1 is mounted.
WARNING!!! Running e2fsck on a mounted filesystem may cause
SEVERE filesystem damage.
Do you really want to continue (y/n)? yes
/: recovering journal
/: clean, 45222/1537088 files, 367281/1536207 blocks
[root@helen ~]# e2fsck /dev/hda
e2fsck 1.39 (29-May-2006)
e2fsck: Device or resource busy while trying to open /dev/hda
Filesystem mounted or opened exclusively by another program?
[root@helen ~]# e2fsck /dev/hda1
e2fsck 1.39 (29-May-2006)
/dev/hda1 is mounted.
WARNING!!! Running e2fsck on a mounted filesystem may cause
SEVERE filesystem damage.
Do you really want to continue (y/n)? yes
/: clean, 45222/1537088 files, 367281/1536207 blocks
添加硬盘或分区
划分分区(disk)
创建文件系统(mkfs)
尝试挂载(mount)
写入配置文件(/etc/fstab)
添加硬盘:
1、固件 CMOS/BIOS-是否识别
2、内核 Kernel-是否识别 dmesg
[root@helen ~]# dmesg | grep hdb
ide0: BM-DMA at 0×1050-0×1057, BIOS settings: hda:DMA, hdb:DMA
hdb: VMware Virtual IDE Hard Drive, ATA DISK drive
hdb: max request size: 128KiB
hdb: 16777216 sectors (8589 MB) w/32KiB Cache, CHS=17753/15/63, UDMA(33)
hdb: unknown partition table
3、划分分区 fdisk 硬盘设备名
[root@helen ~]# dmesg | grep hda
ide0: BM-DMA at 0×1050-0×1057, BIOS settings: hda:DMA, hdb:DMA
hda: VMware Virtual IDE Hard Drive, ATA DISK drive
hda: max request size: 128KiB
hda: 16777216 sectors (8589 MB) w/32KiB Cache, CHS=17753/15/63, UDMA(33)
hda: hda1 hda2 hda3 hda4 < hda5 hda6 >
SELinux: initialized (dev hda1, type ext3), uses xattr
EXT3 FS on hda1, internal journal
EXT3 FS on hda6, internal journal
SELinux: initialized (dev hda6, type ext3), uses xattr
EXT3 FS on hda5, internal journal
SELinux: initialized (dev hda5, type ext3), uses xattr
EXT3 FS on hda3, internal journal
SELinux: initialized (dev hda3, type ext3), uses xattr
Adding 522104k swap on /dev/hda2. Priority:-1 extents:1 across:522104k
[root@helen ~]# dmesg | grep hdb
ide0: BM-DMA at 0×1050-0×1057, BIOS settings: hda:DMA, hdb:DMA
hdb: VMware Virtual IDE Hard Drive, ATA DISK drive
hdb: max request size: 128KiB
hdb: 16777216 sectors (8589 MB) w/32KiB Cache, CHS=17753/15/63, UDMA(33)
hdb: unknown partition table
[root@helen ~]#
[root@helen ~]# fdisk /dev/hdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF
disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won’t be recoverable.
The number of cylinders for this disk is set to 17753.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0×0000 of partition table 4 will be corrected by w(rite)
Command (m for help): m
Command action
a
toggle a bootable flag
b
edit bsd disklabel
c
toggle the dos compatibility flag
d
delete a partition 删除分区
l
list known partition types
m
print this menu
n
add a new partition 添加分区
o
create a new empty DOS partition table
p
print the partition table
q
quit without saving changes 退出
s
create a new empty Sun disklabel
t
change a partition’s system id 改变分区文件系统类型
u
change display/entry units
v
verify the partition table
w
write table to disk and exit 保存退出
x
extra functionality (experts only)
Command (m for help): p
Disk /dev/hdb: 8589 MB, 8589934592 bytes
15 heads, 63 sectors/track, 17753 cylinders
Units = cylinders of 945 * 512 = 483840 bytes
Device Boot
Start
End
Blocks
Id System
Command (m for help): q
[root@helen ~]# fdisk -l /dev/hda
Disk /dev/hda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot
Start
End
Blocks Id System
/dev/hda1 *
1
765
6144831 83 Linux
/dev/hda2
766
830
522112+ 82 Linux swap / Solaris
/dev/hda3
831
894
514080 83 Linux
/dev/hda4
895
1044
1204875
5 Extended
/dev/hda5
895
958
514048+ 83 Linux
/dev/hda6
959
1044
690763+ 83 Linux
Device 设备名
Boot 活动分区 Start-End 分区范围(cylinders) Blocks 分区大小
ID/System 分区文件系统类型
PC Server :分区分为三种:主分区、扩展分区、逻辑分区
(主分区和扩展分区只能有四个,扩展分区存放了逻辑分区)
[root@helen ~]# fdisk /dev/hdb
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-17753, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-17753, default 17753): +500M
Command (m for help): p
Disk /dev/hdb: 8589 MB, 8589934592 bytes
15 heads, 63 sectors/track, 17753 cylinders
Units = cylinders of 945 * 512 = 483840 bytes
Device Boot
/dev/hdb1
Start
1
End
1034
Blocks Id System
488533+ 83 Linux
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): L
82 Linux swap / So
83 Linux
Hex code (type L to list codes): 83
Command (m for help): p
Disk /dev/hdb: 8589 MB, 8589934592 bytes
15 heads, 63 sectors/track, 17753 cylinders
Units = cylinders of 945 * 512 = 483840 bytes
Device Boot
/dev/hdb1
Start
1
End
1034
Blocks Id System
488533+ 83 Linux
Command (m for help): d
Selected partition 1
Command (m for help): p
Disk /dev/hdb: 8589 MB, 8589934592 bytes
15 heads, 63 sectors/track, 17753 cylinders
Units = cylinders of 945 * 512 = 483840 bytes
Device Boot
Start
End
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
重新建立一个分区/dev/hdb1
4.重启后,创建文件系统(格式化)
mkfs 分区名
-t ext3 指定文件系统类型
mkfs.ext3 分区名
[root@helen ~]# mkfs -t ext3 /dev/hdb1
[root@helen ~]# mkfs.ext3 /dev/hdb1
5、尝试挂载测试分区是否可用
mount 物理设备名 挂载点
Blocks
Id System
卸载 umount 挂载点
[root@helen ~]# mkdir /backup
[root@helen ~]# mount /dev/hdb1 /backup
[root@helen ~]# df -h
文件系统
容量 已用 可用 已用% 挂载点
/dev/hda1
5.7G 1.3G 4.2G 23% /
/dev/hda6
654M 17M 603M 3% /web
/dev/hda5
487M 13M 449M 3% /home
/dev/hda3
487M 11M 451M 3% /test
tmpfs
125M
0 125M 0% /dev/shm
/dev/hdb1
462M 11M 428M 3% /backup
[root@helen ~]# umount /backup
[root@helen ~]# df -h
文件系统
容量 已用 可用 已用% 挂载点
/dev/hda1
5.7G 1.3G 4.2G 23% /
/dev/hda6
654M 17M 603M 3% /web
/dev/hda5
487M 13M 449M 3% /home
/dev/hda3
487M 11M 451M 3% /test
tmpfs
125M
0 125M 0% /dev/shm
6、写入配置文件 /etc/fstab(保存了系统所有的分区信息)
[root@helen ~]# cat /etc/fstab
LABEL=/
/
ext3
LABEL=/web
/web
ext3
LABEL=/home
/home
ext3
LABEL=/test
/test
ext3
tmpfs
/dev/shm
tmpfs
devpts
/dev/pts
devpts
sysfs
/sys
sysfs
proc
/proc
proc
LABEL=SWAP-hda2
swap
swap
1)LABEL=/
分区对应的标签或物理设备名 LABEL=/
/dev/hda1
e2label 物理设备名 标签
[root@helen ~]# e2label LABEL=/
/
[root@helen ~]# e2label /dev/hdb1
backup
2)/ 挂载点
3)ext3 文件系统类型
4)defaults 挂载设置
5)1 是否启动时检测,0 不检测,1 检测
6)1 检测顺序,0 不检测,1 优先检测,2 其后检测
另一个情况
defaults
defaults
defaults
defaults
defaults
gid=5,mode=620
defaults
defaults
defaults
1
1
1
1
0
0
0
0
0
1
2
2
2
0
0
0
0
0
第一个数字表示是否检测和检测顺序 0 1 2
第二个数字表示是否自动备份(dump) 0 1
[root@helen ~]# vi /etc/fstab
/dev/hdb1
/backup ext3
defaults
1 2
物理设备名 挂载点 文件系统类型 设置
检测及检测顺序
创建逻辑分区并进行挂载
创建逻辑分区:
[root@helen ~]# fdisk /dev/hdb
Command (m for help): p
Device Boot
Start
/dev/hdb1
1
End
1034
Blocks Id System
488533+ 83 Linux
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 2
First cylinder (1035-17753, default 1035):
Using default value 1035
Last cylinder or +size or +sizeM or +sizeK (1035-17753, default 17753): +2048M
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (1035-5268, default 1035):
Using default value 1035
Last cylinder or +size or +sizeM or +sizeK (1035-5268, default 5268): +1024M
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (3152-5268, default 3152):
Using default value 3152
Last cylinder or +size or +sizeM or +sizeK (3152-5268, default 5268):
Using default value 5268
Command (m for help): p
Device Boot
/dev/hdb1
/dev/hdb2
/dev/hdb5
/dev/hdb6
Start
1
1035
1035
3152
End
1034
5268
3151
5268
Blocks
488533+
2000565
1000251
1000251
Id
83
5
83
83
System
Linux
Extended
Linux
Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
[root@helen ~]# reboot
[root@helen ~]# mkfs -t ext3 /dev/hdb5
[root@helen ~]# mkfs -t ext3 /dev/hdb6
[root@helen ~]# mkdir /logical1
[root@helen ~]# mkdir /logical2
[root@helen ~]# mount /dev/hdb5 /logical1
[root@helen ~]# mount /dev/hdb6 /logical2
[root@helen ~]# df
文件系统
1K-块
已用
可用 已用% 挂载点
/dev/hda1
5952252 1276520 4368492 23% /
/dev/hda6
669080
17104
617440 3% /web
/dev/hda5
497829
13001
459126 3% /home
/dev/hda3
497861
10544
461613 3% /test
tmpfs
127704
0
127704 0% /dev/shm
/dev/hdb1
473084
10544
438114 3% /backup
/dev/hdb5
984496
1244
933240 1% /logical1
/dev/hdb6
984496
1244
933240 1% /logical2
[root@helen ~]# vi /etc/fstab
/dev/hdb5
/logical1
ext3
defaults
1 2
/dev/hdb6
/logical2
ext3
defaults
1 2
[root@helen ~]# reboot
[root@helen ~]# df
文件系统
1K-块
已用
可用 已用% 挂载点
/dev/hda1
5952252 1343096 4301916 24% /
/dev/hda6
669080
17104
617440 3% /web
/dev/hda5
497829
64202
407925 14% /home
/dev/hda3
497861
10537
461620 3% /test
tmpfs
127704
0
127704 0% /dev/shm
/dev/hdb1
473084
26571
422087 6% /backup
/dev/hdb5
/dev/hdb6
984496
984496
17652
17652
916832
916832
2% /logical1
2% /logical2
SWAPFILE 实现
SWAPFILE 作用:没有新硬盘和空闲空间情况下添加 swap 空间
1、创建一个 swapfile 存放的目录
mkdir /var/swap
chmod 700 /var/swap
2、创建一个指定大小的文件
dd if=/dev/hda of=/dev/hdb 硬盘对拷
inputfile outputfile
dd if=/dev/zero of=/var/swap/file.swp bs=1024 count=65536
/dev/zero 伪设备,不断的往输出中写零
/dev/null 黑洞
bs-block size 字节,建议设置为 1024,即为 1K
count-创建文件的大小(以 bs 为单位) 64m=64x1024k
[root@helen ~]# mkdir /var/swap
[root@helen ~]# dd if=/dev/zero of=/var/swap/file.sawp bs=1024 count=65536
65536+0 records in
65536+0 records out
67108864 bytes (67 MB) copied, 0.749254 seconds, 89.6 MB/s
[root@helen ~]# du -h /var/swap/file.sawp
65M
/var/swap/file.sawp
3、生成 swapfile
mkswap 文件
[root@helen ~]# mkswap /var/swap/file.sawp
Setting up swapspace version 1, size = 67104 kB
4、测试
启用 swapfile
swapon 文件
关闭
swapoff
[root@helen ~]# free
total
used
free
shared
buffers
cached
Mem:
255412
182256
73156
0
9488
120436
-/+ buffers/cache:
52332
203080
Swap:
522104
0
522104
[root@helen ~]# swapon /var/swap/file.sawp
[root@helen ~]# free
total
used
free
shared
buffers
cached
Mem:
255412
182256
73156
0
9504
120428
-/+ buffers/cache:
52324
203088
Swap:
587632
0
587632
5、写入/etc/fstab
设备名位置写 swapfile 文件的绝对路径
/var/swap/file.swap
swap
swap
defaults
[root@helen ~]# reboot
[root@helen ~]# free
total
used
free
shared
buffers
Mem:
255412
114048
141364
0
8512
-/+ buffers/cache:
50864
204548
Swap:
587632
0
587632
[root@helen ~]# df
文件系统
1K-块
已用
可用 已用% 挂载点
/dev/hda1
5952252 1342348 4302664 24% /
/dev/hda6
669080
17104
617440 3% /web
/dev/hda5
497829
13001
459126 3% /home
/dev/hda3
497861
10544
461613 3% /test
tmpfs
127704
0
127704 0% /dev/shm
/dev/hdb1
473084
10544
438114 3% /backup
[root@helen ~]# swapoff /var/swap/file.swap
[root@helen ~]# free
total
used
free
shared
buffers
Mem:
255412
116716
138696
0
9000
-/+ buffers/cache:
50928
204484
Swap:
522104
0
522104
[root@helen ~]# reboot
[root@helen ~]# free
total
used
free
shared
buffers
Mem:
255412
114128
141284
0
8536
-/+ buffers/cache:
50864
204548
Swap:
587632
0
587632
0 0
cached
54672
cached
56788
cached
54728
磁盘配额
[samlee@helen ~]$ dd if=/dev/zero of=zerofile bs=1024 count=10240000000
dd: 写入 “zerofile”: 设备上没有空间
457358+0 records in
457357+0 records out
468333568 bytes (468 MB) copied, 9.14625 seconds, 51.2 MB/s
[samlee@helen ~]$ du -h zerofile
449M
zerofile
zhangsan 100m
c:\
d:\
e:\
1)限制 zhangsan 不管在任何一个盘符,总共能用空间就是 100m
用户
x 所有操作
系统都没实现
2)限制 zhangsan c:\ 20m d:\ 70m e:\ 10m
分区
磁盘配额概念:只能针对分区设置用户配额
创建配额的选项
软限制(Soft limit)
:定义用户可以占用的磁盘空间数。当用户超过该限制后会收到以超
过配额的警告。
硬限制(Hard limit)
:当用户试图将文件存放在其已经超过该限制值目录时,报告文件系
统错误。
宽限期(Grace period)
:定义用户在软限制下可以使用其文件系统的期限。
第一步:开启分区配额功能
在/etc/fstab 文件设置属性(defaults 位置)加上设置选项,用户配额 usrquota ,用户
组配额 grpquota
如果划分了/home 分区,就开启用户磁盘配额,如果没有划分/home 分区,就开启/分区的用
户磁盘配额
在/etc/fstab 文件设置属性(defaults 位置)加上设置选项,用户配额 usrquota ,用户
组配额 grpquota
LABEL=/home
/home
ext3
defaults,usrquota
1 2
生效:1、重启系统
2、mount -o remount /home
-o options
第二步,建立配额数据库文件 (进入单用户模式)
用户配额 aquota.user 用户组配额 aquota.group
quotacheck -cvuga
quotacheck -cvu /home
c-创建配额数据库文件
v-详细信息
u-生成用户配额文件
g-生成用户组配额文件
a-检测所有开启配额分区
[root@helen ~]# vi /etc/fstab
LABEL=/home
/home
ext3
defaults,usrquota
1 2
[root@helen ~]# mount -o remount /home
[root@helen ~]# quotacheck -cvu /home
quotacheck: Scanning /dev/hda5 [/home] quotacheck: Cannot stat old user quota file:
没有那个文件或目录
quotacheck: Old group file not found. Usage will not be substracted.
done
quotacheck: Checked 10 directories and 25 files
quotacheck: Old file not found.
临时设置:mount -o remount,usrquota /home
[root@helen ~]# ls /home
aquota.user hellen liming lisi lost+found mary samlee wangwu zhaoliu
第三步,启动配额功能
quotaon 分区名
[root@helen ~]# quotaon /home
第四步,编辑用户配额
edquota 用户名
edquota -g 用户组
开去两个硬盘上的多磁盘配额
blocks 文件大小(KB)
inodes 文件多少(inode)
soft 软限制 51200 (超出软限制在宽限期内可以使用,超过宽限期系统会自动删除)
hard 硬限制 102400 (无法超出)
[root@helen ~]# edquota lisi
Disk quotas for user lisi (uid 504):
Filesystem
blocks
soft
hard
inodes
soft
hard
/dev/hda5
8
0
51200
4
0
0
[root@helen ~]# edquota -t
Grace period before enforcing soft limits for users:
Time units may be: days, hours, minutes, or seconds
Filesystem
Block grace period
Inode grace period
/dev/hda5
7days
7days
[root@helen ~]# su – lisi
[lisi@helen ~]$ dd if=/dev/zero of=zerofile bs=1024 count=10240000000
hda5: write failed, user block limit reached.
dd: 写入 “zerofile”: 超出磁盘限额
50991+0 records in
50990+0 records out
52213760 bytes (52 MB) copied, 0.792811 seconds, 65.9 MB/s
[lisi@helen ~]$ su – root
Password:
[root@helen ~]# quota lisi
Disk quotas for user lisi (uid 504):
Filesystem blocks quota limit grace files quota limit grace
/dev/hda5 51200*
0 51200
5
0
0
[root@helen ~]# repquota -a
*** Report for user quotas on device /dev/hda5
Block grace time: 7days; Inode grace time: 7days
Block limits
File limits
User
used
soft
hard grace
used soft hard grace
———————————————————————-
root
– 12969
0
0
6
0
0
samlee

15
0
0
6
0
0
mary

8
0
0
4
0
0
hellen

10
0
0
5
0
0
lisi
– 51200
0 51200
5
0
0
wangwu

10
0
0
5
0
0
#506

8
0
0
4
0
0
[root@helen ~]# edquota -p lisi wangwu hellen samlee
[root@helen ~]# repquota -a
*** Report for user quotas on device /dev/hda5
Block grace time: 7days; Inode grace time: 7days
Block limits
File limits
User
used
soft
hard grace
used soft hard grace
———————————————————————-
root
– 12969
0
0
6
0
0
samlee

15
0 51200
6
0
0
mary

8
0
0
4
0
0
hellen

10
0 51200
5
0
0
lisi
– 51200
0 51200
5
0
0
wangwu

10
0 51200
5
0
0
#506

8
0
0
4
0
0
[root@helen ~]# repquota /home
*** Report for user quotas on device /dev/hda5
Block grace time: 7days; Inode grace time: 7days
Block limits
File limits
User
used
soft
hard grace
used soft hard grace
———————————————————————-
root
– 12969
0
0
6
0
0
samlee

15
0 51200
6
0
0
mary

8
0
0
4
0
0
hellen

10
0 51200
5
0
0
lisi
– 51200
0 51200
5
0
0
wangwu

10
0 51200
5
0
0
#506

8
0
0
4
0
0
系统的潜在威胁
系统硬件故障
软件故障
电源故障
用户的误操作
人为破坏
缓存中的内容没有及时的写入磁盘
自然灾害
备份介质的选择
备份介质
硬盘
光盘
磁带
可移动存储设备
一般在选择备份介质时,要从可靠性、速度和介质价格之间进行权衡
备份策略
完全备份
每隔一段时间对系统进行一次完全的备份,
这样在备份时间间隔内,
一旦系统发生故障使得
数据丢失时,就可以用上一次的备份数据恢复到上一次备份时的情况。
增量备份
首先进行一次完全备份,
然后每隔一段较短的时间进行一次备份,
但是仅仅备份每个短时期
内更改的内容。
备份的分类
系统备份
实现对操作系统和应用程序的备份
尽量在系统崩溃以后能快速简单完全地恢复系统的运行
主要备份/etc、/boot、/var/log、/usr/local 等
一般只有当系统内容发生变化时才进行
用户备份
实现对用户文件的备份/home
用户的数据变动频繁
通常采用增量备份策略进行
记录更改建立备份日志
记录系统的更改
记录对系统进行了哪些修改的详细描述及为什么要进行修改
建立备份日志
使用备份日志表格
妥善保管
备份日志表格范例
机器名称、IP 地址、存放位置
备份时间
备份介质及其编号
备份的文件系统
备份的目录和文件
使用的备份命令
备份人员及其他
cp 命令备份举例
备份目录:cp -Rpu 备份目录 目标目录
-p 保持备份目录及文件属性
-u 增量备份
[root@helen test]# cp /etc/inittab /test
[root@helen test]# ls -l /etc/inittab /test/inittab
-rw-r–r– 1 root root 1666 02-22 23:16 /etc/inittab
-rw-r–r– 1 root root 1666 02-24 09:52 /test/inittab
[root@helen test]# cp -p /etc/inittab /test
cp:是否覆盖“/test/inittab”? y
[root@helen test]# ls -l /etc/inittab /test/inittab
-rw-r–r– 1 root root 1666 02-22 23:16 /etc/inittab
-rw-r–r– 1 root root 1666 02-22 23:16 /test/inittab
远程备份可用 scp
tar 命令使用举例
[root@helen test]# tar -zcf /backup/etc_20110304.tar.gz /etc
tar: 从成员名中删除开头的“/”
备份/etc 目录,可同时打包多个目录
[root@helen test]# tar -zcf backup_user_20110304.tar.gz /etc/passwd /etc/shadow
/etc/group /etc/gshadow
tar: 从成员名中删除开头的“/”
对/etc 目录下指定文件进行备份
[root@helen test]# tar -ztf backup_user_20110304.tar.gz
etc/passwd
etc/shadow
etc/group
etc/gshadow
查看备份包中文件(不解包)
[root@helen test]# ls
backup_user_20110304.tar.gz
[root@helen test]# tar -zxf /backup/etc_20110304.tar.gz
[root@helen test]# ls
backup_user_20110304.tar.gz etc
还原/etc 目录,默认还原到打包文件源目录
-C 可以指定还原目录
[root@helen test]# tar -zxf backup_user_20110304.tar.gz etc/group
[root@helen test]# ls
backup_user_20110304.tar.gz etc
[root@helen test]# cd etc
[root@helen etc]# ls
group
只恢复备份中的指定文件
[root@helen test]# ls /backup
[root@helen test]# cd
[root@helen ~]# tar -zcf /backup/user.bak.20110304.tar.gz /etc/passwd /etc/shadow
/etc/skel
tar: 从成员名中删除开头的“/”
[root@helen ~]# ls /backup
user.bak.20110304.tar.gz
[root@helen ~]# tar -ztf /backup/user.bak.20110304.tar.gz
etc/passwd
etc/shadow
etc/skel/
etc/skel/.bash_logout
etc/skel/.bashrc
etc/skel/.bash_profile
[root@helen ~]# tar -zxf /backup/user.bak.20110304.tar.gz
[root@helen ~]# ls
anaconda-ks.cfg etc install.log install.log.syslog mbox x11.find
[root@helen ~]# tar -zxf /backup/user.bak.20110304.tar.gz etc/group
tar: etc/group:归档中找不到
tar: 由于前面延迟的错误而退出
[root@helen ~]# rm /etc/passwd
rm:是否删除 一般文件 “/etc/passwd”? y
[root@helen ~]# tar -zxf /backup/user.bak.20110304.tar.gz etc/passwd
[root@helen ~]# ls /etc/passwd
ls: /etc/passwd: 没有那个文件或目录
[root@helen ~]# cd /
[root@helen /]# tar -zxf /backup/user.bak.20110304.tar.gz etc/passwd
[root@helen /]# ls /etc/passwd
/etc/passwd
[root@helen /]# ls /root/etc
ls: /root/etc: 没有那个文件或目录
[root@helen /]# rm /etc/passwd
rm:是否删除 一般文件 “/etc/passwd”? y
[root@helen /]# tar -zxf /backup/user.bak.20110304.tar.gz etc/passwd -C
/etc/passwd
[root@helen /]# ls /etc/passwd
/etc/passwd
tar -r 和-u 选项只能对 tar 包使用,不能用于.gz 操作
-r 加入修改过的文件,包中有原文件
-u 追加到包中原来没有的文件
为备份文件包添加时间
[root@helen /]# date
2011 年 02 月 24 日 星期四 10:25:11 CST
[root@helen /]# date +%F
2011-02-24
[root@helen /]# date +%Y%m%d%H%M
201102241025
[root@helen /]# date +%Y%m%d-%H:%M
20110224-10:26
[root@helen ~]# date +%Y 年%m 月%d 日\ %H 点%M 分(\ 转意空格)
2011 年 03 月 13 日 14 点 04 分
[root@helen /]# tar -zcf /backup/etc_$(date +%F).tar.gz /etc
tar: 从成员名中删除开头的“/”
[root@helen /]# ls /backup/
etc_2011-02-24.tar.gz
[root@helen /]# tar -zcf /backup/etc_$(date +%m-%d).tar.gz /etc
tar: 从成员名中删除开头的“/”
[root@helen /]# ls /backup/
etc_02-24.tar.gz
备份数据类型
1、系统备份 /etc /boot
2、数据备份 /home
备份策略
1、/etc /boot
一周做一次完全备份,做两次增量备份
crontab -e
0 4 * * 0 date >> /backup/etc.bak.err ; tar -zcf /backup/etc_$(date +%F).tar.gz
/etc 2>> /backup/etc.bak.err
crontab -e
0 4 * * 2,5 cp -Ru /etc /backup 2>> /etc.bak.err
tar -uf /backup/etc.bak.tar /etc
2、/home
0 */6 * * *
RAID
Redundant(多余的)Array(排列)of Independent(独立的)Disks 独立磁盘冗余阵列
RAID 的基本目的是把多个小型廉价的磁盘驱动器合并成一组阵列来达到大型昂贵的驱
动器所无法达到的性能或冗余性。组成磁盘阵列的不同方式成为 RAID 级别(RAID Levels)

RAID 工作方式
RAID 是一种在多个磁盘上分散信息的方法。它使用磁盘分条( disk striping,RAID
级别 0)
、磁盘镜像(diskmirroring,RAID 级别 1)
、和带有奇偶校验的磁盘分条(disk
striping with parity,RAID 级别 5)之类的技术来达到冗余性,减低潜伏时间,并且(或
者)增加磁盘读写的带宽,提高从硬盘崩溃中恢复的能力。
?RAID 的基本原理是,数据必须使用一致的形式被分散到阵列中的驱动器上。要打到这
个目的,数据必须被分割成大小一致的“块”
(大小通常是 32K 或 64K,也可使用不同大小)

每一块都会根据所用的 RAID 级别而写入其中的一个硬盘驱动器。当数据要被读取时,这个
进程就会反过来进行,造成一个多个驱动器好象是一个大驱动器的假象。
RAID0 提升数据读写速度,无数据自动备份机制
RAID1 自动完全备份,不提升数据读写速度
RAID5 条带化读写的奇偶校验,自动备份,读写速度提升
RAID10(0+1)
谁应该使用 RAID
任何需要使大量数据触手可及的人
(如一般的系统管理员)
都可以从 RAID 技术中受益。
使用 RAID 的主要原因包括:
加快速度
使用一个虚拟磁盘,从而增加贮存容量
减少磁盘失效带来的不利影响
RAID 卡(SATA/SCSI)
LVM
LVM 可以让我们灵活的动态调整分区大小。
1、不破坏现有数据及分区结构
2、/boot 分区不能纳入 LVM
3、在安装系统分区时部署
LVM 结构
[root@helen ~]# lvresize –help
-L|–size [+|-]LogicalVolumeSize[bBsSkKmMgGtTpPeE]}
逻辑卷的名