{"id":107,"date":"2009-02-28T18:12:20","date_gmt":"2009-02-28T16:12:20","guid":{"rendered":"http:\/\/www.shukko.com\/x3\/2009\/02\/28\/cloneback-uprestore-openvz-vms-with-vzdump\/"},"modified":"2009-02-28T18:13:36","modified_gmt":"2009-02-28T16:13:36","slug":"cloneback-uprestore-openvz-vms-with-vzdump","status":"publish","type":"post","link":"https:\/\/www.shukko.com\/x3\/2009\/02\/28\/cloneback-uprestore-openvz-vms-with-vzdump\/","title":{"rendered":"Clone\/Back Up\/Restore OpenVZ VMs With vzdump"},"content":{"rendered":"<h2>Clone\/Back Up\/Restore OpenVZ VMs With vzdump<\/h2>\n<p>Version 1.0<br \/>\nAuthor: Falko Timme &lt;ft [at] falkotimme [dot] com&gt;<br \/>\nLast edited 11\/20\/2008<\/p>\n<p>vzdump is a backup and restore utility for <a href=\"http:\/\/wiki.openvz.org\/\" target=\"_blank\">OpenVZ<\/a> VMs. This tutorial shows how you can use it to clone\/back up\/restore virtual machines with vzdump.<\/p>\n<p>I do not issue any guarantee that this will work for you!<\/p>\n<h3>1 Preliminary Note<\/h3>\n<p>I&#8217;m using two OpenVZ servers in this tutorial:<\/p>\n<ul>\n<li><span class=\"system\">server1.example.com<\/span>: IP <span class=\"system\">192.168.0.100<\/span><\/li>\n<li><span class=\"system\">server2.example.com<\/span>: IP <span class=\"system\">192.168.0.101<\/span><\/li>\n<\/ul>\n<p>(Both are using Debian Etch and are set up according to this tutorial: <a href=\"http:\/\/www.howtoforge.com\/installing-and-using-openvz-on-debian-etch\" target=\"_blank\">Installing And Using OpenVZ On Debian Etch<\/a> &#8211; but it works with any other distribution as well.)<\/p>\n<p>I&#8217;m running a virtual machine with the hostname <span class=\"system\">test.example.com<\/span>, the IP address <span class=\"system\">192.168.0.102<\/span> and the VEID <span class=\"system\">102<\/span> on <span class=\"system\">server1.example.com<\/span>, and I want to back up that machine and restore it on <span class=\"system\">server2.example.com<\/span>.<\/p>\n<p>We can restore it on <span class=\"system\">server2.example.com<\/span> with no changes (e.g. same IP address and hostname), but in that case we must stop the VM on <span class=\"system\">server1.example.com<\/span> because otherwise the IP address and hostname would conflict; the second possibility is to restore it on <span class=\"system\">server2.example.com<\/span>, but change some parameters like the IP address and hostname with the <span class=\"system\">vzctl set<\/span> command &#8211; in this case we can run both VMs (the original one on <span class=\"system\">server1.example.com<\/span> and the clone on <span class=\"system\">server2.example.com<\/span>) at the same time. This is a great method to clone VMs.<\/p>\n<h3>2 Preparing The OpenVZ Servers<\/h3>\n<p>First we must install vzdump and rsync which is a dependency. On Debian, the command is as follows:<\/p>\n<p class=\"highlight\">server1\/server2:<\/p>\n<p class=\"command\">apt-get install vzdump rsync<\/p>\n<h3>3 Creating A Backup Of A VM<\/h3>\n<p class=\"highlight\">(This chapter is for server1 only!)<\/p>\n<p>On <span class=\"system\">server1.example.com<\/span>, I want to create a backup of my VM with the VEID <span class=\"system\">102<\/span>. Take a look at<\/p>\n<p class=\"command\">man vzdump<\/p>\n<p>to learn how to use <span class=\"system\">vzdump<\/span>.<\/p>\n<p>To back up all VMs on your server, you&#8217;d use something like<\/p>\n<p class=\"command\">vzdump &#8211;compress &#8211;dumpdir \/home\/backup &#8211;stop &#8211;all<\/p>\n<p><span class=\"system\">&#8211;compress<\/span> means: compress the dump file (results in a <span class=\"system\">.tgz<\/span>).<\/p>\n<p><span class=\"system\">&#8211;dumpdir<\/span> specifies the directory in which you want to store the dump. If you don&#8217;t specify a dumpdir, it defaults to <span class=\"system\">\/vz\/dump<\/span> or <span class=\"system\">\/var\/lib\/vz\/dump<\/span> (depends on your distribution).<\/p>\n<p><span class=\"system\">&#8211;stop<\/span> stops the VM, creates the backup, and starts it again afterwards. Your VM can be down a few minutes if you use <span class=\"system\">&#8211;stop<\/span>. A faster solution would be to use&#8230;<\/p>\n<p><span class=\"system\">&#8211;suspend<\/span>: it suspends the VM; the VM is then copied via rsync to a temporary directory. The VM gets resumed right afterwards so that it&#8217;s down only a few seconds, and then the dump is created using the copy in the temporary directory. I recommend to use this one if you can&#8217;t afford long downtimes.<\/p>\n<p>You can as well leave out <span class=\"system\">&#8211;stop<\/span> and <span class=\"system\">&#8211;suspend<\/span> and dump a running VM. In most cases this makes no problem, but it is possible that the dump is inconsistent, so be warned!<\/p>\n<p><span class=\"system\">&#8211;all<\/span> creates a dump of all available VMs. If you want to dump only a specific VM, replace <span class=\"system\">&#8211;all<\/span> with the <span class=\"system\">VEID<\/span> of the VM.<\/p>\n<p>To create a dump of our VM <span class=\"system\">102<\/span> in <span class=\"system\">\/home\/backup<\/span> and stop the VM during the backup, use<\/p>\n<p class=\"command\">vzdump &#8211;compress &#8211;dumpdir \/home\/backup &#8211;stop 102<\/p>\n<p>To create a dump in the default directory (<span class=\"system\">\/vz\/dump<\/span> or <span class=\"system\">\/var\/lib\/vz\/dump<\/span>), use<\/p>\n<p class=\"command\">vzdump &#8211;compress &#8211;stop 102<\/p>\n<p>The output could look as follows:<\/p>\n<p class=\"system\">server1:\/vz\/dump# vzdump &#8211;compress &#8211;stop 102<br \/>\nINFO: starting backup for VPS 102 (\/var\/lib\/vz\/private\/102)<br \/>\nINFO: starting first sync \/var\/lib\/vz\/private\/102 to \/var\/lib\/vz\/dump\/tmp9009<br \/>\nINFO: stopping vps<br \/>\nStopping container &#8230;<br \/>\nContainer was stopped<br \/>\nContainer is unmounted<br \/>\nINFO: final sync \/var\/lib\/vz\/private\/102 to \/var\/lib\/vz\/dump\/tmp9009<br \/>\nINFO: restarting vps<br \/>\nStarting container &#8230;<br \/>\nContainer is mounted<br \/>\nAdding IP address(es): 192.168.0.102<br \/>\nSetting CPU units: 1000<br \/>\nConfigure meminfo: 65536<br \/>\nSet hostname: test.example.com<br \/>\nFile resolv.conf was modified<br \/>\nContainer start in progress&#8230;<br \/>\nINFO: vps is online again after 15 seconds<br \/>\nINFO: Creating archive &#8216;\/var\/lib\/vz\/dump\/vzdump-102.tgz&#8217; (\/var\/lib\/vz\/dump\/tmp9009\/102)<br \/>\nTotal bytes written: 340428800 (325MiB, 11MiB\/s)<br \/>\nINFO: backup for VPS 102 finished successful (1.37 minutes)<br \/>\nserver1:\/vz\/dump#<\/p>\n<p>To not stop, but suspend the VM, use<\/p>\n<p class=\"command\">vzdump &#8211;compress &#8211;suspend 102<\/p>\n<p>This is a sample output:<\/p>\n<p class=\"system\">server1:~#\u00a0vzdump\u00a0&#8211;compress\u00a0&#8211;suspend\u00a0102<br \/>\nINFO:\u00a0starting\u00a0backup\u00a0for\u00a0VPS\u00a0102\u00a0(\/var\/lib\/vz\/private\/102)<br \/>\nINFO:\u00a0starting\u00a0first\u00a0sync\u00a0\/var\/lib\/vz\/private\/102\u00a0to\u00a0\/var\/lib\/vz\/dump\/tmp10842<br \/>\nINFO:\u00a0suspend\u00a0vps<br \/>\nSetting\u00a0up\u00a0checkpoint&#8230;<br \/>\nsuspend&#8230;<br \/>\nget\u00a0context&#8230;<br \/>\nCheckpointing\u00a0completed\u00a0succesfully<br \/>\nINFO:\u00a0final\u00a0sync\u00a0\/var\/lib\/vz\/private\/102\u00a0to\u00a0\/var\/lib\/vz\/dump\/tmp10842<br \/>\nINFO:\u00a0resume\u00a0vps<br \/>\nResuming&#8230;<br \/>\nINFO:\u00a0vps\u00a0is\u00a0online\u00a0again\u00a0after\u00a04\u00a0seconds<br \/>\nINFO:\u00a0Creating\u00a0archive\u00a0&#8216;\/var\/lib\/vz\/dump\/vzdump-102.tgz&#8217;\u00a0(\/var\/lib\/vz\/dump\/tmp10842\/102)<br \/>\nTotal\u00a0bytes\u00a0written:\u00a0340428800\u00a0(325MiB,\u00a024MiB\/s)<br \/>\nINFO:\u00a0backup\u00a0for\u00a0VPS\u00a0102\u00a0finished\u00a0successful\u00a0\u00a0(1.57\u00a0minutes)<br \/>\nserver1:~#<\/p>\n<p>After the backup, take a look at the dump directory&#8230;<\/p>\n<p class=\"command\">ls -l \/vz\/dump\/<\/p>\n<p>&#8230; and you should see a <span class=\"system\">.tgz<\/span> file:<\/p>\n<p class=\"system\">server1:~#\u00a0ls\u00a0-l\u00a0\/vz\/dump\/<br \/>\ntotal\u00a0147864<br \/>\n-rw-r&#8211;r&#8211;\u00a01\u00a0root\u00a0root\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01170\u00a02008-11-20\u00a017:40\u00a0vzdump-102.log<br \/>\n-rw-r&#8211;r&#8211;\u00a01\u00a0root\u00a0root\u00a0151249685\u00a02008-11-20\u00a017:40\u00a0vzdump-102.tgz<br \/>\nserver1:~#<\/p>\n<p>You can now copy the dump to the other OpenVZ server, e.g. with scp (this copies <span class=\"system\">\/vz\/dump\/vzdump-102.tgz<\/span> to the <span class=\"system\">\/home<\/span> directory on <span class=\"system\">server2.example.com<\/span>):<\/p>\n<p class=\"command\">scp \/vz\/dump\/vzdump-102.tgz root@192.168.0.101:\/home<\/p>\n<h3>4 Restoring A VM<\/h3>\n<p><span class=\"highlight\">(This chapter is for server2 only!)<\/span><\/p>\n<p>On <span class=\"system\">server2.example.com<\/span>, you can now restore the VM as follows&#8230;<\/p>\n<p class=\"command\">vzdump &#8211;restore \/home\/vzdump-102.tgz 250<\/p>\n<p>&#8230; where <span class=\"system\">250<\/span> is the new VEID of the restored VM &#8211; you can use any VEID that is unused on <span class=\"system\">server2.example.com<\/span> &#8211; you could even use <span class=\"system\">102<\/span> again if it is unused on <span class=\"system\">server2.example.com<\/span>.<\/p>\n<p>If you don&#8217;t want to modify the settings of the VM (e.g. IP address, hostname), you can start it now, <span class=\"highlight\">but please make sure that the original VM is stopped on server1.example.com<\/span> because otherwise the IP addresses conflict:<\/p>\n<p class=\"command\">vzctl start 250<\/p>\n<p>If you want to run both VMs (the original one and the clone) at the same time, <span class=\"highlight\">you must change the IP address and hostname of the clone<\/span> before you start it.<\/p>\n<p>To set a new hostname, run sonething like this:<\/p>\n<p class=\"command\">vzctl set 250 &#8211;hostname test2.example.com &#8211;save<\/p>\n<p>To set a new IP address, we must first delete the original one&#8230;<\/p>\n<p class=\"command\">vzctl set 250 &#8211;ipdel 192.168.0.102 &#8211;save<\/p>\n<p>&#8230; and then set a new one:<\/p>\n<p class=\"command\">  vzctl set 250 &#8211;ipadd 192.168.0.250 &#8211;save<\/p>\n<p>Afterwards we can start the clone:<\/p>\n<p class=\"command\">vzctl start 250<\/p>\n<h3>5 Links<\/h3>\n<ul>\n<li>OpenVZ: <a href=\"http:\/\/wiki.openvz.org\/\" target=\"_blank\">http:\/\/wiki.openvz.org\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Clone\/Back Up\/Restore OpenVZ VMs With vzdump Version 1.0 Author: Falko Timme &lt;ft [at] falkotimme [dot] com&gt; Last edited 11\/20\/2008 vzdump is a backup and restore utility for OpenVZ VMs. This tutorial shows how you can use it to clone\/back up\/restore virtual machines with vzdump. I do not issue any guarantee that this will work for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-107","post","type-post","status-publish","format-standard","hentry","category-kategerisiz"],"_links":{"self":[{"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/posts\/107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":0,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}