Linux的备份方案比选及Borg部署的关键技术

这篇文章 Linux Backup Solutions 列出了大部分的免费解决方案,和对它们的评价,可以用了进行全面的了解。还有一篇比较详细的关于备份方案选择的Blog,最终它推荐的是Attic: holy-grail-backups.Attic是一个被广泛推荐的解决方案,但是它在Git上已经由3年没有更新了,有一个它的分支 Borg 还在一直维护中。

还有一个新兴工具 Dumplicay ,  可以看看它和 几个常用工具的性能比较(A performance comparison of Duplicacy, restic, Attic, and duplicity),看描述应该技术比较新,准备作为选择它作为当前的尝试方案,但是又发现这个东西居然不是完全免费的,商业版要收费要收费!

这里还有一个中文版的关于Linux备份、迁移的一般解释,比较的详细,过程基本依赖与Linux原生工具。


因为版权和收费方面的原因,放弃了Dumplicay,而根据上面的比较,选择Borg作为测试方案。

1.安装Borg

Borg 的当前版本是 1.1.5, 查看了一下Debian的 Distribution Package 现在还是1.0.9版本,为了使用最新的版本,决定使用Binary方式安装。

wget https://github.com/borgbackup/borg/releases/download/1.0.12/borg-linux64
sudo cp borg-linux64 /usr/local/bin/borg
sudo chown root:root /usr/local/bin/borg
sudo chmod 755 /usr/local/bin/borg

2. 创建和还原备份的一般操作 Create & Restor a backup

#创建存储仓库 initializes an empty repository
borg init -e repokey /mnt/path-to-repository

#设定仓库的保留空间,防止出现磁盘满的异常
borg config /mnt/path-to-repository additional_free_space 2G 

#创建备份
borg create -someoptions /mnt/path-to-repository::archivename /path-tobe-backup

borg create -v -p -s /mnt/borgs/DS-Test::mounta-{now:%Y-%m-%d-%s} .

#Check & Mount a backup as a dir
borg info  /mnt/path-to-repository::archivename
borg list /mnt/path-to-repository::archivename
borg mount  /mnt/path-to-repository::archivename  /some-mount-point-path
borg umount /some-mount-point-path

#Restor. 
#Attention: extract always to the **current path**
borg extract -v --list /mnt/path-to-repository::archivename

关于存储仓库

是否可以用一个仓库来存储多台服务器的备份?结论是可以,但是不推荐。

Can I backup from multiple servers into a single repository?

Yes, but in order for the deduplication used by Borg to work, it needs to keep a local cache containing checksums of all file chunks already stored in the repository. This cache is stored in <span class="pre">~/.cache/borg/</span>. If Borg detects that a repository has been modified since the local cache was updated it will need to rebuild the cache. This rebuild can be quite time consuming.

So, yes it’s possible. But it will be most efficient if a single repository is only modified from one place. Also keep in mind that Borg will keep an exclusive lock on the repository while creating or deleting archives, which may make simultaneous backups fail.

** 使用windows共享(SMB)作为备份的存储仓库

由于我使用了一个磁盘阵列配置了一个比较大Windows共享作为公共备份存储,所以想把Borg的存储仓库也放到这个存储中。

实现的方法很简单,直接使用 mount.cifs来挂接共享路径到系统中,然后直接这个挂接点上创建Borg备份仓库

mount.cifs //10.5.183.126/asb/borgs /mnt/borgs -o user=autobackuper,domain=backup

3.整机备份和恢复

Borg:Backing up your entire root partition works just fine, but remember to exclude directories that make no sense to backup, such as /dev, /proc, /sys, /tmp and /run, and to use –one-file-system if you only want to backup the root partition (and not any mounted devices e.g.).

#创建备份,比较典型的全盘备份
borg create -v -p -s                \
    --exclude-caches                \
    --one-file-system               \
    --exclude '/home/*/.cache/*'    \
    --exclude '/var/cache/*'        \
    --exclude '/var/tmp/*'          \
    --exclude '/tmp/*'              \
    /mnt/path-to-borg-repository::'{hostname}-{now}' /

#ssh://user@host:port/path/to/repo
#简化版本的
borg create -v -p -s -exclude-caches --one-file-system   \
-e '/home/*/.cache/*' -e '/var/cache/*' -e '/var/tmp/*' -e '/tmp/*'  \
ssh://root@58.57.39.74:10002/mnt/borgs/DS136::HostVultr-20180514 \


borg create -v -p -s -exclude-caches --one-file-system \
ssh://root@58.57.39.74:10002/mnt/borgs/DS-Test::test-{now:%Y-%m-%d-%s} .

borg create -v -p -s -exclude-caches --one-file-system \
/mnt/borgs/DS-Test::test-{now:%Y-%m-%d-%s} .

#Restor
#Restore always put into current path
cd /
borg extract -v --list backup-arthive  

4. 更健壮的整机备份 – 静默状态的文件系统

事实上,全系统必须考虑到Mysql、数据库、mail等随时会发生变化的文件,为了使文件保存一致,必须使文件系统进入静默状态可以先看看这些参考:Debian safe backup of server system drive?   Backup and Restore Logical Volume using LVM Snapshot.

要想文件系统进入静默状态,可以使用停止服务等方式,可以直接关机,也可以使用LVMs等卷影副本模式。这里有个一篇关于备份Mysql的讲解(Using LVM for MySQL Backup and Replication Setup)。关于LVMS的部署安装见下面专门的章节。

LVMs的快照不会保存挂接的内容和信息,所以使用快照备份时需要额外的处理。比如,在启用LVMs的系统中,”/boot” is mount from a seprated device , usuall is sda1.
** Borg可以处理symlinks,但不会跟随进入symlinks.
** Borg可以处理mount的内容,除非指定了 –one-file-system

现在假设我们已经安装配置好了LVms,(安装和部署LVMs具体看后面的单独部分)典型的最佳实践操作如下:

#创建快照
#创建一个快照,制定差异存储空间为1G, 差异存储空间用于保存快照创建后的源LV的所有变化,
#如果空间被占用满了,则快照变为不可用
lvcreate -s -n snapshot_name -L 1G /dev/<somevg>/<somelv>

#挂接快照
mount /dev/mapper/<some-lv-device-name> /mnt/<some-mount-point>

#备份
cd / or snaproot
borg create -s -p -v -x --compression lz4   \
  ssh://root@58.57.39.74:10002/mnt/borgs/vultr-web::vultr-{now:%Y-%m-%d-%H%M} /
  
#取消挂接
umount /mnt/<some-mount-point>

#删除快照
Lvremove /dev/<somevg>/<some-snap>

#挂接SMB共享的存储
# user = smb user, domain= smb domain, uid=set local user as own
mount.cifs //10.5.183.126/asb/borgs /mnt/borgs -o user=autobackuper,domain=backup,uid=backup

#------------------------------------------
# Other commands maybe needed<br>
#显示所有逻辑卷
lvs
#列出所有卷组
vgs
#列出所有物理卷
pvs
#列出所有存储设备(Block Devie)
lsblk --output NAME,KNAME,TYPE,SIZE,MOUNTPOINT
#查看/dev/mapper/* 和 /dev/dm-x 关系
#/dev/dm-x 是LVM创建的虚拟卷,已数字为序编号;/dev/mapper/* 是指向上述虚拟卷的链接,提供了更有意义的长名称
ls -l /dev/mapper/

5.在全新计算机恢复备份(系统迁移)

  1. 尽可能使用和备份一致的硬件系统
  2. 预安装Linux系统,Linux的版本应该和备份基本一致 # (还可使用挂接硬盘的方式来恢复,这个示例只讨论使用单机还原的情形,实际也是一个类似迁移的过程)
  3. 安装过程中应该配置和备份一致的分区结构,如果不一致则恢复时应跳过 /boot 和 /etc/fstab. # 比如在新机器上希望使用LVM,而备份机未使用LVM,这时新旧间的磁盘分区结构差异很大,则应跳过上述内容
  4. 启动新机器,进入系统,按下述操作
#--------------------------------------------------------
#                     还原,使用全新计算器还原,
wget https://github.com/borgbackup/borg/releases/download/1.0.12/borg-linux64 
cp borg-linux64 /usr/local/bin/borg 
chown root:root /usr/local/bin/borg 
chmod 755 /usr/local/bin/borg
 
cd /  # !!! MUST !!!!

export BORG_REPO='ssh://backup@58.57.39.74:10002/mnt/borgs/vultr-web'
export BORG_REPO='ssh://backup@58.57.39.74:10002/./'
export BORG_REPO='ssh://backup@58.57.39.74:10002/~/'

export BORG_PASSPHRASE="password"
borg extract -v --list -e boot -e etc/fstab   \
ssh://backup@58.57.39.74:10002/mnt/borgs/vultr-web::vultr-2018-05-14-1526266870 

borg extract -v --list -e etc/fstab \
ssh://backup@58.57.39.74:10002/mnt/borgs/vultr-web::vultr-2018-05-14-1526266870 

grub-mkdevicemap
update-grub

#--------------------------------------------------------
ifconfig eth0 192.168.168.156 netmask 255.255.255.0
ip address add 192.168.1.101/24 dev eth0
ip link set dev eth0 up
ip route add default via 192.168.1.254

$ sudo dhclient -r eth0
$ sudo dhclient eth0

jour  -xn
systemctrl status networking.ser...

#修复(重装)Grub <a href="https://linux.cn/article-6892-1.html">参考</a>
GRUB grub-install /dev/sda  #etc sda not sda1 
grub-mkdevicemap 
update-grub

** grub lost

还原(borg extract) 可能会覆盖 boot分区内容,特别是grub的配置,如果前后系统的分区配置是一致的,覆盖后的Grub应该也能正常启动。而如果不一致,可能会发生Grub错误,不能正常启动系统。为避免这种情况发生:

  1. 恢复时应该一般应该跳过boot分区,至少要排除掉 /boot/grub .
  2. 如果发生覆盖,或者因为某种原因必须覆盖,而又出现不能正常启动的情况,那么需要尝试手动调整Grub,恢复启动系统,下面是使用Grub命令行模式,手动启动系统的典型操作:
#press 'c' enter command line mode
grub> insmod lvm        #Load lvm mod
grub> ls                #show list of disk and pertaitions
grub> set root=(hd0,1)  #or ther pataintion 

#Maybe there are some img, choose a crected img version, and make sure it is bulid with lvms support.
grub> linux /vmlinuz-<version>-<arch> root=/dev/mapper/lvm-lv-name
grub> initrd /initramfs-<version>-<arch>.img
grub> boot

参见:Rescue a Non-Booting System from the Grub2 Command Prompt

6.LVMs的安装和部署

尝试使用LVMs时遇到了新的问题,LVMs不是像想象的那样安装后就可以直接操作原有硬盘分区数据的,它只能管理使用LVMs创建的Logic Volume,也就意味着,已经使用中的系统分区是没法用LVMs管理的!那么能不能把现有的系统分区转换成LVMs磁盘呢?不幸的是LVMs不支持直接的转换操作,唯一可行的方法是:创建新的LVMs分区 > 复制数据 > 切换启动分区到新的LVMs分区,这是一系列非常复杂的操作,这里由个详细的说明(Converting an Existing Root Filesystem to LVM Partition),但是看起来太复杂,太容易引出新的问题!

这里还有个转换普通分区到LVM是分区的工具Blocks。按照它的说明它可以进行转换( on fly or in place)。 但是,它也只能处理非系统分区,如果你想转换你的根文件系统(Converting your root filesystem to LVM )需要额外的操作。

参考:Logical Volume Management on Debian Linux

结论:

  1. 安装Linux时应该尽量启用LVM,已便于以后的系统维护、备份。部署LVM时务必要在VG中保留一些未分配空间,这些空间将会用于创建快照。
  2. 对运行中的系统,如果想部署LVMs,最好的办法是,安装一个启用LVMs的新系统,然后迁移(还原)原有系统到新的系统中去。
  3. 使用Borg backup的全盘备份和全盘恢复可以实现系统的迁移,迁移是必须注意到:
    (1)硬件应尽可能一致。
    (2)磁盘分区布局尽可能一致。
    (3)启用LVMs的系统的分区方式、分区名称和没有启用LVMs的系统是有着明显的差异,比如/boot在LVMs中的独立的分区,(/)文件系统的根路径也是不一样的,所以迁移时,必须避免覆盖Grub配置,一般来说/boot中的内容不能恢复和覆盖;也正是因为/boot被跳过,也就意味着还原后,启动时使用的新系统的Linux内核,而不是备份中的内核,为了保持还原(迁移)后的正常运行,新安装的系统应该使用和备份系统一致或接近的内核版本
#Extract(restore). <br>#extract always writes into the current working directory (“.”)
#Pay attention to the pattners of exclude(-e), there isn't the root path (/). 
borg extract -v --list -e boot -e etc/fstab -e tmp root@10.5.183.136:/mnt/borgs/DS136::achivename

LVMs 创建快照(Snapshot),居然需要苛刻的前置条件

好不容易把原有的系统转移到新的支持LVMs的新根文件系统上来,准备创建快照…可是,非常意外的发现,LVMs创建快照要求必须在VG中有剩余的未分配的空间,TM,谁在分区时还会想到故意留一些未分配的空间?为什么安装过程中不给提示?有多少人遇到这个问题时一下傻眼了?

第一个反应是,想法缩小已经创建的LV分区,腾出空间来,好创建快照。LVMs到时支持动态改变LV分区大小,不影响数据,当然前提是分区必须有足够的空闲空间。但是我们又遇到了非常特殊的“根文件系统 (/)”, 调整LV分区大小必须让分区先unmount,而根文件系统时不能unmount的,想让他umount只有一个办法,使用一个LiveCD启动系统,在LiveCD的独立系统中操作磁盘。

大概的步骤是:  LiveCD启动 – 缩减LV – 创建快照

#install lvm
sudo apt-get install lvm2

#show logiv volumes
sudo lvs

#Active vgs and lvs
sudo vgchange --available y [somevg]

#reduce
sudo lvreduce --resizefs --size -5G /dev/<somevg>/root

How do I shrink the root logical volume (LV) on LVM?

RHCSA – LVM names and the Device Mapper

 

发表评论