简体   繁体   English

如何备份/移动LXC容器?

[英]How do I Backup / Move LXC containers?

I want to take lxc container backup. 我想采取lxc容器备份。 We have server with 12.04 LTS ubuntu server and I have installed LXC - 1.0.0.alpha2 in it. 我们有12.04 LTS ubuntu server ,我已经安装了LXC - 1.0.0.alpha2 I wanted to update our ubuntu server to 14.04 LTS. 我想将我们的ubuntu服务器更新为14.04 LTS。 So what I want to do is have LXC containers backed up -> upgrade OS to 14.04 -> restore LXC containers. 所以我想做的是备份LXC容器 - >将OS升级到14.04 - >恢复LXC容器。 With earlier version(0.7.5 I guess) there was lxc-backup and lxc-restore but with 1.0.0.alpha2 we don't have backup and restore operations. 使用早期版本(我猜是0.7.5)有lxc-backup和lxc-restore但是1.0.0.alpha2我们没有备份和恢复操作。 How can I have lxc containers backup. 我怎样才能备份lxc容器。 I spent more than 3 hours with copy container folder(/var/lib/lxc/my_container/) into pendrive and paste it in another 12.04 LTS server but it is not working the error am getting is, 我花了超过3个小时将副本容器文件夹(/ var / lib / lxc / my_container /)放入pendrive并将其粘贴到另一个12.04 LTS服务器中,但它无法正常运行,我得到的错误是,

#sudo lxc-start -n my_container
lxc-start: invalid sequence number 1, expected 4.
lxc-start: failed to spwan "my_container"

then how can I expect that it will work in upgraded 14.04 server OS. 那么我怎么能期望它在升级的14.04服务器操作系统中有效。

Any Idea to have lxc-container backup? 有想法进行lxc-container备份吗?

EDIT: March 29, 2016 编辑:2016年3月29日

In case you stumbled upon this post, my answer is really about moving the LXC containers between systems, since that seemed to be the question being asked. 如果您偶然发现这篇文章,我的答案实际上是关于在系统之间移动 LXC容器,因为这似乎是被问到的问题。

If you want to backup your LXC containers, see @Stuart's answer for some great options. 如果您想备份 LXC容器,请参阅@Stuart的答案,了解一些很棒的选择。

Moving LXC containers between host systems 在主机系统之间移动LXC容器

This is how I migrate LXC containers between systems. 这就是我在系统之间迁移LXC容器的方法。 I've successfully moved ubuntu based 12.04 containers to a 14.04 host, and they work great. 我已成功将基于12.04容器的ubuntu移动到14.04主机,并且它们运行良好。

  • Shutdown the container 关闭容器

     # lxc-stop -n $NAME 
  • Archive container rootfs & config 归档容器rootfs&config

     # cd /var/lib/lxc/$NAME/ # tar --numeric-owner -czvf container_fs.tar.gz ./* 

    The --numeric-owner flag is very important! --numeric-owner标志非常重要! Without it, the container may not boot because the uid/gids get mangled in the extracted filesystem. 没有它,容器可能无法启动,因为uid / gids在提取的文件系统中受到损坏。 When tar creates an archive, it preserves user / group ownership information. 当tar创建存档时,它会保留用户/组所有权信息。 By default, when extracting, tar tries to resolve the archive user/group ownership names with the ids on the system running tar. 默认情况下,在解压缩时,tar会尝试使用运行tar的系统上的ID来解析存档用户/组所有权名称。 This is intended to ensure that user ownership is resolved on the new system, in case the UID numeric values differ between systems. 这旨在确保在新系统上解析用户所有权,以防系统之间的UID数值不同。

    This is bad for an LXC filesystem because the numeric uid/gid ownership is intended to be preserved for the whole filesystem. 这对LXC文件系统不利,因为数字uid / gid所有权旨在为整个文件系统保留。 If it gets resolved to a different value, bad things happen. 如果它被解析为不同的值,则会发生不好的事情。

  • Copy the file to your new server 将文件复制到新服务器

     # rsync -avh container_fs.tar.gz user@newserver:/var/lib/lxc/ 
  • Extract rootfs 提取rootfs

     # mkdir /var/lib/lxc/$NAME/ # cd /var/lib/lxc/$NAME/ # tar --numeric-owner -xzvf container_fs.tar.gz . 

If you're using an overlay backed container, you'll also need to migrate the container this new one is based off of. 如果您使用的是覆盖式备份容器,则还需要迁移此新容器所基于的容器。 Lastly, you might see a few warnings about skipped socket files: 最后,您可能会看到有关跳过套接字文件的一些警告:

tar: /var/lib/lxc/$NAME/rootfs/dev/log: socket ignored

I've ignored this error, and haven't had any issues with any of the containers I manage. 我忽略了这个错误,并且没有遇到任何我管理的容器的问题。 If you have further issues, add your error messages to the original post and I'll elaborate. 如果您还有其他问题,请将错误消息添加到原始帖子中,我将详细说明。

EDIT: November 2017 编辑:2017年11月

To backup an lxc container quickly to a remote host without a btrfs filesystem I mount a filesystem from the remote host with sshfs & cd into the mount. 要备份的lxc迅速容器到remote主机没有btrfs文件系统挂载我从一个文件系统remote与主机sshfscd插入安装。 Stop the container & create a tar.xz archive of it. 停止容器并创建它的tar.xz存档。


EDIT: March 2016 编辑:2016年3月

I now run my lxc containers on a btrfs filesystem to make it simpler to take a snapshot of the running containers. 我现在在btrfs文件系统上运行我的lxc容器,以便更简单地获取正在运行的容器的snapshot btrfs sub snap detects proc run sys are virtual filesystems & does not include them in the snapshot. btrfs sub snap检测到proc run sys是虚拟文件系统,并且不包含在快照中。


I use Duply to backup LXC containers. 我使用Duply来备份LXC容器。 Unlike backing up a normal machine you DO want to include /dev from the LXC container in the backup. 不像备份正常的机器想包括/dev从备份中LXC容器。

apt-get install duply
duply mybackup create

In ~/.duply/mybackup/exclude I used: ~/.duply/mybackup/exclude我用过:

- /cdrom
- /dev
- /lost+found
- /media
- /mnt
- /proc
- /run
- /sys
- /tmp
- /var/backup/restore/*
- /var/backup/tmp/*
- /var/lib/lxc/*/rootfs/lost+found
- /var/lib/lxc/*/rootfs/media/*
- /var/lib/lxc/*/rootfs/mnt/*
- /var/lib/lxc/*/rootfs/proc/*
- /var/lib/lxc/*/rootfs/run/*
- /var/lib/lxc/*/rootfs/sys/*
- /var/lib/lxc/*/rootfs/tmp/*
- /var/lib/lxcfs/*

The above will backup the whole machine & all the LXC containers. 以上将备份整个机器和所有LXC容器。

To just backup containers edit ~/.duply/mybackup/conf & change SOURCE='/' to SOURCE='/var/lib/lxc' & remove the non lxc lines from ~/.duply/mybackup/exclude 要只备份容器,请编辑~/.duply/mybackup/conf并将SOURCE='/'更改为SOURCE='/var/lib/lxc'并从~/.duply/mybackup/exclude删除非lxc行

Tested with running Alpine Linux LXC containers - will also work on Debian. 使用运行的Alpine Linux LXC容器进行测试 - 也可以在Debian上运行。

Simple Backups with Duply - you can also just do very simple unencrypted backups to a local file (set TARGET='file://[relative|/absolute]/local/path' in ~/.duply/mybackup/conf ) 使用Duply进行简单备份 - 您也可以对本地文件进行非常简单的未加密备份(在~/.duply/mybackup/conf设置TARGET='file://[relative|/absolute]/local/path'

To sign Duply backups see GnuPG in Automated Environments ( password-less signing key instead of storing the password in plaintext ). 要签署Duply备份,请参阅自动环境中的GnuPG (无密码签名密钥,而不是以明文形式存储密码)。

Set GPG_TEST='disabled' in the Duply conf file for cron jobs. 在Duply conf文件中为cron作业设置GPG_TEST='disabled'

If you do not store any plaintext passwords in your conf do not disable GPG_TEST on restores - so gpg-agent caches your passwords. 如果你存储在您的任何明文口令conf 不要禁用GPG_TEST恢复 -使gpg-agent缓存密码。

I agree with Brad Jasperson. 我同意Brad Jasperson的观点。 I do it this way: 我是这样做的:

lxc-clone -KMP /path/to/backup name name

If something goes wrong with your container, and downtime costs a lot, you can run the copy: 如果您的容器出现问题,停机成本很高,您可以运行副本:

lxc-start -n name -P /path/to/backup

and stop: 并停止:

lxc-stop -n name -P /path/to/backup

you can copy it back in place later in appropriate time. 您可以在适当的时间将其复制回原位。 Good luck! 祝好运!

I find the simplest way to back up a container is to just run lxc-clone. 我发现备份容器的最简单方法是运行lxc-clone。

lxc-clone -o NAMEOFCONTAINER -n NAMEOFCONTAINER -P BACKUPDIR

Restoring it is as simple as copying or moving the backup back to /var/lib/lxc You can also tar it to save space. 恢复它就像复制或将备份移回/ var / lib / lxc一样简单您还可以对其进行tar以节省空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM