Linux学习笔记(23) Linux备份

1. 备份概述

Linux系统需要备份的数据有/root,/home,/var/spool/mail,/etc及日志等其他目录。

安装服务的数据需要备份,如apache需要备份的数据有配置文件、网页主目录、日志文件;源码包安装的mysql需要备份/usr/local/mysql/data,RPM包安装的mysql需要备份/var/lib/mysql。

(1) 备份策略

完全备份:就是指把所有需要备份的数据全部备份,当然完全备份可以备份整块硬盘,整个分区或某个具体的目录。特点备份较慢,且占用的空间很大。

增量备份:每次备份只备份新增加的数据,是与上一次备份的数据相比。特点:数据恢复比较麻烦。

差异备份:前两者的折中办法,与增量备份的差别在于,第二次备份是与第一次的完全备份相比。

2. 备份命令

(1) dump命令

其格式为:

dump [选项] 备份之后的文件名 原文件或目录

其中,-level选项就是我们所说的0-9十个备份级别;-f 文件名选项指定备份之后的文件名;-u选项表示备份成功之后,把备份时间记录在/etc/dumpdates文件;-v选项显示备份过程中更多的输出信息;-j选项表示调用bzlib库压缩备份文件,其实就是把备份文件压缩为.bz2格式;-W选项显示被dump的分区的备份等级及备份时间

例1:备份分区

# 备份命令。先执行一次完全备份,并压缩和更新备份时间
[root@localhost ~]# dump -0uj -f /root/boot.bak.bz2 /boot/
......
  DUMP: Wrote 22370kB uncompressed, 20288kB compressed, 1.103:1
  DUMP: DUMP IS DONE

# 查看备份时间文件
[root@localhost ~]# cat /etc/dumpdates 
/dev/sda1 0 Sun Aug 16 06:54:09 2015 +0800
/dev/sda1 1 Tue Aug  4 02:33:29 2015 +0800

#复制日志文件到boot分区
[root@localhost ~]# cp install.log /boot/

#增量备份boot分区,并压缩
[root@localhost ~]# dump -1uj -f /boot/boot.bak1.bz2 /boot/

# 查看文件的大小
[root@localhost ~]# ll -h /root/
-rw-r--r--. 1 root root  18K Aug 16 07:00 boot.bak1.bz2
-rw-r--r--. 1 root root  20M Aug 16 06:54 boot.bak.bz2

# 查询分区的备份时间及备份级别的
[root@localhost ~]# dump -W
Last dump(s) done (Dump '>' file systems):
> /dev/sda5     (     /) Last dump: never
  /dev/sda1     ( /boot) Last dump: Level 1, Date Sun Aug 16 06:58:05 2015
> /dev/sda2     ( /home) Last dump: never
> /dev/sdb1     (/root/disk1) Last dump: never
> /dev/sdb5     (/root/disk5) Last dump: never

 

例2:备份文件或目录

# 完全备份/etc/目录,只能使用0级别进行完全备份,而不再支持增量备份
[root@localhost ~]# dump -0j -f /root/etc.dump.bz2 /etc/

 

(2) restore命令

 恢复命令restore的格式为:

restore [模式选项] [选项]

其中,restore命令常用的模式有四种,且这四种模式不能混用。包括:

-C-----比较模式,比较备份数据和实际数据的变换

-i-----进入交互模式,手工选择需要恢复的文件

-t-----查看模式,用于查看备份文件中拥有哪些数据

-r-----还原模式,用于数据还原

而选项包括:-f------指定备份文件的文件名

例1:比较备份和实际数据的变化

#把boot目录中内核镜像文件改个名字
[root@localhost ~]# mv /boot/vmlinuz-2.6.32-431.el6.i686 /boot/vmlinuz-2.6.32-431.el6.i686.bak

#restore发现内核镜像文件丢失
[root@localhost ~]# restore -C -f /root/boot.bak.bz2 
Dump tape is compressed.
Dump   date: Sun Aug 16 06:54:09 2015
Dumped from: the epoch
Level 0 dump of /boot on localhost.localdomain:/dev/sda1
Label: none
filesys = /boot
restore: unable to stat ./vmlinuz-2.6.32-431.el6.i686: No such file or directory
Some files were modified!  1 compare errors

 

例2:查看模式

[root@localhost ~]# restore -t -f /root/boot.bak.bz2 
Dump tape is compressed.
Dump   date: Sun Aug 16 06:54:09 2015
Dumped from: the epoch
Level 0 dump of /boot on localhost.localdomain:/dev/sda1
Label: none
         2      .
        11      ./lost+found
     32385      ./grub
     32408      ./grub/grub.conf
     32386      ./grub/splash.xpm.gz
     32392      ./grub/menu.lst
     32393      ./grub/device.map
     32394      ./grub/stage1
     32395      ./grub/stage2
     32396      ./grub/e2fs_stage1_5
     32397      ./grub/fat_stage1_5
     32398      ./grub/ffs_stage1_5
     32399      ./grub/iso9660_stage1_5
     32400      ./grub/jfs_stage1_5
     32401      ./grub/minix_stage1_5
     32402      ./grub/reiserfs_stage1_5
     32403      ./grub/ufs2_stage1_5
     32404      ./grub/vstafs_stage1_5
     32405      ./grub/xfs_stage1_5
     32406      ./grub/.grub.conf.swp
     32387      ./efi
     32388      ./efi/EFI
     32389      ./efi/EFI/redhat
     32390      ./efi/EFI/redhat/grub.efi
        12      ./.vmlinuz-2.6.32-431.el6.i686.hmac
        13      ./System.map-2.6.32-431.el6.i686
        14      ./config-2.6.32-431.el6.i686
        15      ./symvers-2.6.32-431.el6.i686.gz
        16      ./vmlinuz-2.6.32-431.el6.i686
        17      ./initramfs-2.6.32-431.el6.i686.img

 

例3:还原模式

#(1) 还原boot.bak.bz2分区备份

#先还原完全备份的数据
[root@localhost ~]# mkdir boot.test
[root@localhost ~]# cd boot.test/
#解压缩
[root@localhost boot.test]# restore -r -f /root/boot.bak.bz2 
Dump tape is compressed.

#再恢复增量备份数据
[root@localhost boot.test]# restore -r -f /root/boot.bak1.bz2 
Dump tape is compressed.
./boot.bak1.bz2: (inode 19) not found on tape


#(2) 还原/etc目录的备份etc.dump.bz2
restore -r -f etc.dump.bz2

 

posted @ 2015-08-15 23:56  mengrennwpu  阅读(666)  评论(0编辑  收藏  举报