xfs文件系统的备份和恢复(redhat7.2)

Posted on Posted in linux

一、基本配置

systemctl status NetworkManager # 查看服务状态
systemctl restart network # 重启网络服务

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736  # 网络配置
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=c4c0c7db-f3bc-4aab-b229-cf8f445e8807
DEVICE=eno16777736
ONBOOT=yes

systemctl status firewalld.service     # 查看防火墙状态
systemctl stop firewalld       #关闭
systemctl disable firewalld     #开机自动关闭

[root@localhost ~]# setenforce 0 # 关闭selinux
[root@localhost ~]# cat /etc/sysconfig/selinux 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

[root@localhost ~]# cat /etc/fstab # 挂载光盘

#
# /etc/fstab
# Created by anaconda on Wed Jun 21 21:29:09 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=e9e5aa1b-d4a9-4cb3-a4a1-c5fc0579d246 /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/cdrom  /mnt    iso9660     defaults    0   0
[root@localhost ~]# mount -a

[root@localhost ~]# cat /etc/yum.repos.d/rhel7.repo # 添加源
[rhel7-server]
name=rhel-server
baseurl=file:///mnt
enabled=1
gpgcheck=0

[root@localhost ~]# yum clean all && yum list # 清空并生成缓存列表

linux 基本概念

  • tty:终端设备的统称(控制台终端)tty一词源于Teletypes,或者teletypewriters,原来指的是电传打字机,是通过串行线用打印机键盘通过阅读和发送信息的东西,后来这东西被键盘与显示器取代,所以现在叫终端比较合适。终端是一种字符型设备,它有多种类型,通常使用tty来简称各种类型的终端设备
  • pts虚拟终端。如果我们远程telnet到主机或使用xterm时不也需要一个终端交互么?是的,这就是虚拟终端pty(pseudo-tty),我们通过XSHELL连接上服务器上显示出来的就是pts。pts(rpseudo-teminal slave)是pty的实现方法,与ptmx(pseudo-terminal master)配合使用实现pty
  • “#” 表示root用户
  • “$” 表示普通用户
# 查看所有的解释器
cat /etc/shells 
chsh -l

# 查看硬件时间
[root@localhost ~]# hwclock

# 查看系统时间
[root@localhost ~]# date

# 区分外部命令和内部命令
[root@localhost ~]# type cd
cd 是 shell 内嵌
[root@localhost ~]# type vim
vim 是 /usr/bin/vim

shutdown 关机命令

  • -r => 重新启动计算机
  • -h => 关机
  • -h + 时间 =>定时关机
[root@localhost ~]# shutdown -h +10 # 10分钟之后关机
[root@localhost ~]# shutdown -h 23:30 # 指定具体的时间点进行关机
[root@localhost ~]# shutdown -h now # 立即关机
[root@localhost ~]# shutdown -r  22:22 # 22:22 以后重启

init 命令

  • 0 系统停机模式,系统默认运行级别不能设置为0,否则不能正常启动,机器关
  • 1 单用户模式,root权限,用于系统维护,禁止远程登陆,就像Windows下的安全模式登录
  • 2 多用户模式,没有NFS网络支持
  • 3 完整的多用户文本模式,有NFS,登陆后进入控制台命令行模式
  • 4 系统未使用,保留一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本电脑的电池用尽时,可以切换到这个模式来做一些设置
  • 5 图形化模式,登陆后进入图形GUI模式,X Window系
  • 6 重启模式,默认运行级别不能设为6,否则不能正常启动。运行init 6机器就会重启

启动级别

RHEL7不再使用/etc/inittab文件进行默认的启动级别配置,systemd使用比sysvinit的运行级更为自由的target替代。

  • 第3运行级用multi-user.target替代
  • 第5运行级用graphical.target替代

runlevel3.target和runlevel5.target分别是指向 multi-user.target和graphical.target的符号链接。

# 切换到第3运行级,两种方法
[root@localhost ~]# systemctl isolate multi-user.target
[root@localhost ~]# systemctl isolate runlevel3.target
# 设置默认的运行级别
[root@localhost ~]# systemctl set-default multi-user.target
[root@localhost ~]# runlevel
N 3
[root@localhost ~]# systemctl get-default # 查看默认级别
multi-user.target
[root@localhost ~]# cat /etc/inittab # 可以查看一下说明

bios 定时开机

进入bios -> Power Management Setup(电源管理) -> Wake Up Event Setup -> Resume By RTC Alarm

二、xfs文件系统的备份和恢复

XFS提供了 xfsdump 和 xfsrestore 工具协助备份XFS文件系统中的数据。xfsdump 按inode顺序备份一个XFS文件系统。
与传统的UNIX文件系统不同,XFS不需要在dump前被卸载;对使用中的XFS文件系统做dump就可以保证镜像的一致性。这与XFS对快照的实现不同,XFS的dump和restore的过程是可以被中断然后继续的,无须冻结文件系统。xfsdump 甚至提供了高性能的多线程备份操作——它把一次dump拆分成多个数据流,每个数据流可以被发往不同的目的地
首先了解一下xfsdump的备份级别有以下两种,默认为0(即完全备份)

  • 完全备份 0
  • 增量备份 1-9
[root@localhost ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb
[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0x5b0cc45b 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-10485759,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-10485759,默认为 10485759):+1G
分区 1 已设置为 Linux 类型,大小设为 1 GiB

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x5b0cc45b

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@localhost ~]# ls /dev/sdb*
/dev/sdb  /dev/sdb1
[root@localhost ~]# mkfs.xfs /dev/sdb1 # 格式化为xfs
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# mkdir /sdb1
[root@localhost ~]# mount /dev/sdb1 /sdb1/
[root@localhost ~]# mount | tail -l
/dev/sdb1 on /sdb1 .type xfs (rw,relatime,attr2,inode64,noquota)
# 准备备份测试文件2
[root@localhost ~]# cp -f /etc/passwd /sdb1/
[root@localhost ~]# mkdir /sdb1/test
[root@localhost ~]# ls /sdb1/
passwd  test
# 对整个分区进行备份
[root@localhost ~]# yum -y install xfsdump
[root@localhost ~]# xfsdump -f /opt/dump_sdb1 /sdb1 # 这里的不能写成 /sdb1/
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.4 (dump format 3.0) - type ^C for status and control

 ============================= dump label dialog ==============================

please enter label for this dump session (timeout in 300 sec)
 -> dump_sdb1 # 指定备份会话标签
session label entered: "dump_sdb1"

 --------------------------------- end dialog ---------------------------------

xfsdump: level 0 dump of localhost.localdomain:/sdb1
xfsdump: dump date: Wed Jun 21 23:15:35 2017
xfsdump: session id: 30ff5acb-a3a6-4057-ad76-3123d91b410b
xfsdump: session label: "dump_sdb1"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 25536 bytes
xfsdump: /var/lib/xfsdump/inventory created

 ============================= media label dialog =============================

please enter label for media in drive 0 (timeout in 300 sec)
 -> media0 # 指定设备标签
media label entered: "media0"

 --------------------------------- end dialog ---------------------------------

xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 23208 bytes
xfsdump: dump size (non-dir files) : 1568 bytes
xfsdump: dump complete: 22 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /opt/dump_sdb1 OK (success)
xfsdump: Dump Status: SUCCESS
[root@localhost ~]# rm -rf /opt/dump_sdb1
# 非交互式进行备份
[root@localhost ~]# xfsdump -f /opt/dump_sdb1 /sdb1 -L dump_sdb1 -M media0
# 针对指定文件或目录进行备份
[root@localhost ~]# xfsdump -f /opt/dump_passwd -s passwd /sdb1 -L dump_sd1_passwd -M media1
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.4 (dump format 3.0) - type ^C for status and control
xfsdump: level 0 dump of localhost.localdomain:/sdb1
xfsdump: dump date: Wed Jun 21 23:21:46 2017
xfsdump: session id: 9b1094d2-b1cc-4c2c-b3cd-1fb2cb024a2a
xfsdump: session label: "dump_sd1_passwd"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 25216 bytes
xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 22928 bytes
xfsdump: dump size (non-dir files) : 1568 bytes
xfsdump: dump complete: 0 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /opt/dump_passwd OK (success)
xfsdump: Dump Status: SUCCESS
[root@localhost ~]# ls /opt/
dump_passwd  dump_sdb1
# 查看备份信息
[root@localhost ~]# xfsdump -I

# 文件系统恢复
[root@localhost ~]# rm -rf /sdb1/*
[root@localhost ~]# xfsrestore -f /opt/dump_sdb1 /sdb1/
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.4 (dump format 3.0) - type ^C for status and control
xfsrestore: searching media for dump
xfsrestore: examining media file 0
xfsrestore: dump description: 
xfsrestore: hostname: localhost.localdomain
xfsrestore: mount point: /sdb1
xfsrestore: volume: /dev/sdb1
xfsrestore: session time: Wed Jun 21 23:18:09 2017
xfsrestore: level: 0
xfsrestore: session label: "dump_sdb1"
xfsrestore: media label: "media0"
xfsrestore: file system id: 7d1fba52-bed1-40c3-a03c-07e9631af2dc
xfsrestore: session id: fa296669-394c-4ae1-83a9-efc83ea35c69
xfsrestore: media id: 92f44bca-781d-4897-bddd-2aa8a517ce62
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 2 directories and 2 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 1 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /opt/dump_sdb1 OK (success)
xfsrestore: Restore Status: SUCCESS
[root@localhost ~]# ls /sdb1/
passwd  test
# 只恢复单个的目录或文件
[root@localhost ~]# rm -rf /sdb1/passwd 
[root@localhost ~]# xfsrestore -f /opt/dump_sdb1 -s passwd /sdb1/
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.4 (dump format 3.0) - type ^C for status and control
xfsrestore: searching media for dump
xfsrestore: examining media file 0
xfsrestore: dump description: 
xfsrestore: hostname: localhost.localdomain
xfsrestore: mount point: /sdb1
xfsrestore: volume: /dev/sdb1
xfsrestore: session time: Wed Jun 21 23:18:09 2017
xfsrestore: level: 0
xfsrestore: session label: "dump_sdb1"
xfsrestore: media label: "media0"
xfsrestore: file system id: 7d1fba52-bed1-40c3-a03c-07e9631af2dc
xfsrestore: session id: fa296669-394c-4ae1-83a9-efc83ea35c69
xfsrestore: media id: 92f44bca-781d-4897-bddd-2aa8a517ce62
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 2 directories and 2 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /opt/dump_sdb1 OK (success)
xfsrestore: Restore Status: SUCCESS

Leave a Reply

Your email address will not be published. Required fields are marked *

eight − four =