{"id":301,"date":"2012-07-16T12:25:02","date_gmt":"2012-07-16T10:25:02","guid":{"rendered":"http:\/\/elkano.org\/blog\/?p=301"},"modified":"2012-07-16T12:25:55","modified_gmt":"2012-07-16T10:25:55","slug":"kvmopenvz-dumps-with-specific-name-using-a-hook","status":"publish","type":"post","link":"https:\/\/elkano.org\/blog\/kvmopenvz-dumps-with-specific-name-using-a-hook\/","title":{"rendered":"KVM\/Openvz dumps with specific name using a hook"},"content":{"rendered":"<p>Vzdump is the tool used to back up virtual machines in Proxmox.<br \/>\nA few weeks ago, I wrote an entry describing how to do a dump with an specified name, rather than using the default naming for this backups in <a title=\"vzdump: create KVM backups with an specific name\" href=\"http:\/\/elkano.org\/blog\/vzdump-create-kvm-backups-with-an-specific-name\/\">Creating Kvm backups with an specific name<\/a>, but now, I&#8217;m comming with a much more elegant solution using a hook for vzdump. In Proxmox 2.x, you can back up machines from the web interface very easily and using a hook all is done transparently.<\/p>\n<p>This tool has an option &#8220;-script&#8221; to be able to call an script during its execution.<\/p>\n<pre class=\"prettyprint lang-xml\"> -script    string\r\n\t     Use specified hook script.<\/pre>\n<p>You can configure an script to do what ever you want in diferent states during vzdump execution. In this case, I only want to rename default file to a more representative one so I&#8217;ve only used the <em>backup-end<\/em> state to do it at the end of the dump process. My script located in \/root\/scripts\/vzdump-hook-script.pl:<\/p>\n<pre class=\"dontquote prettyprint lang-perl background:#C0FFB3\">\r\n#!\/usr\/bin\/perl -w\r\n\r\n# example hook script for vzdump (--script option)\r\n\r\nuse strict;\r\nuse File::Copy qw(move);\r\n\r\nmy $basedir=&quot;\/mnt\/pve\/pve-backups\/dump&quot;;\r\nprint &quot;HOOK: &quot; . join (&#039; &#039;, @ARGV) . &quot;\\n&quot;;\r\n\r\nmy $phase = shift;\r\nif ($phase eq &#039;backup-end&#039; ){\r\n    my $mode = shift; # stop\/suspend\/snapshot\r\n    my $vmid = shift;\r\n    my $vmtype = $ENV{VMTYPE} if defined ($ENV{VMTYPE}); # openvz\/qemu\r\n    my $dumpdir = $ENV{DUMPDIR} if defined ($ENV{DUMPDIR});\r\n    my $hostname = $ENV{HOSTNAME} if defined ($ENV{HOSTNAME});\r\n    # tarfile is only available in phase &#039;backup-end&#039;\r\n    my $tarfile = $ENV{TARFILE} if defined ($ENV{TARFILE});\r\n    # logfile is only available in phase &#039;log-end&#039;\r\n    my $logfile = $ENV{LOGFILE} if defined ($ENV{LOGFILE});\r\n    print &quot;HOOK-ENV: vmtype=$vmtype;dumpdir=$dumpdir;hostname=$hostname;tarfile=$tarfile;logfile=$logfile\\n&quot;;\r\n    if ($phase eq &#039;backup-end&#039; and defined ($tarfile) and defined ($hostname)) {\r\n        if ( $tarfile=~\/($basedir\\\/vzdump-(qemu|openvz)-\\d+-)(\\d\\d\\d\\d_.+)\/ ){\r\n          my $tarfile2=$1.$hostname.&quot;-&quot;.$3;\r\n          print &quot;HOOK: Renaming file $tarfile to $tarfile2\\n&quot;;\r\n          move $tarfile, $tarfile2;\r\n        }\r\n    }\r\n}\r\n\r\nexit (0);\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>With this script you get a file name like vzdump-qemu-106-HOSTNAME-2012_06_22-14_35_59.tar.gz and this files are correctly listed by Proxmox interface. You can check the sample script <strong>\/usr\/share\/doc\/pve-manager\/examples\/vzdump-hook-script.pl<\/strong> to view all available states.<\/p>\n<p>Configure the script in \/etc\/vzdump.conf<\/p>\n<pre class=\"prettyprint\">\r\n# vzdump default settings\r\n\r\n#tmpdir: DIR\r\n#dumpdir: DIR\r\n#storage: STORAGE_ID\r\n#mode: snapshot|suspend|stop\r\n#bwlimit: KBPS\r\n#ionice: PRI\r\n#lockwait: MINUTES\r\n#stopwait: MINUTES\r\n#size: MB\r\n#maxfiles: N\r\nscript: \/root\/scripts\/vzdump-hook-script.pl\r\n#exclude-path: PATHLIST\r\n<\/pre>\n<p>Now, every vzdump execution will call our script to rename the dump file name.<br \/>\nTo finish you only have to apply this changes to all nodes in the Proxmox cluster.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vzdump is the tool used to back up virtual machines in Proxmox. A few weeks ago, I wrote an entry describing how to do a dump with an specified name, rather than using the default naming for this backups in Creating Kvm backups with an specific name, but now, I&#8217;m comming with a much more [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,7],"tags":[39,147,35,34],"_links":{"self":[{"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/posts\/301"}],"collection":[{"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/comments?post=301"}],"version-history":[{"count":32,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/posts\/301\/revisions"}],"predecessor-version":[{"id":334,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/posts\/301\/revisions\/334"}],"wp:attachment":[{"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/media?parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/categories?post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/tags?post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}