ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    KVM Snapshot/Backup Script

    IT Discussion
    kvm snapshots qcow2 linux virtualization
    7
    48
    12.0k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • stacksofplatesS
      stacksofplates
      last edited by stacksofplates

      So I posted a snapshot export script on here a long time ago, and it wasn't really correct. It created an overlay disk of a backing store and exported the backing store. Red Hat doesn't include the most up to date version of QEMU, so they can push RHEV. RHEL/CentOS 7 only comes with QEMU 1.5, and for live snapshots you need at least 2.0. So if you ad the oVirt repos you can get the newest version with the package qemu-kvm-rhev, or use Fedora server. Here's a script I wrote to take a live external snapshot and compress it to a location. I used my normal template with the interactive function. vmLocation is the location of your VMs. I usually keep mine in the same area. I should add full paths for everything, but I'm lazy this evening. I'll add them later. I also need to add some error handling but this is a rough start if anyone needs to use it.

      #!/bin/bash
      
      
      vmLocation="/data/VMs"
      
      today=$(date +%m-%d-%Y)
      
      
      
      #Script functions
      function script_help () {
        echo "
            Usage: $(basename $0) [options] dom-name snap-name save-location disk-name
      
                -i   Ineractive mode
      
                -h   this help text
      
                dom-name        Name of domain to take snapshot of
      
                snap-name       Name of snapshot
      
                save-location   location to copy snapshot to
      
                disk-name       Name of disk in VM
      
            Example:
              $(basename $0) bind-server bind-snap01 /export/snaps/ vda"
      
        exit ${1:-0}
      }
      
      function interactive_snap () {
      
        echo "Domain name"
        read name
      
        echo "Snapshot name"
        read snap
      
        echo "Location to save snap"
        read location
      
        echo "Disk name"
        read disk
      
        virsh dumpxml $name > $location/$name.xml
      
        diskPath=$(virsh domblklist $name | grep -i $disk | awk '{ print $2 }')
      
        virsh snapshot-create-as --domain $name $snap --diskspec $disk,file=$vmLocation/$snap.qcow2 --disk-only --atomic
      
        tar -czvf $location/$snap-$today.tar.gz $diskPath
      
        virsh blockcommit $name $disk --active --verbose --pivot
      
        rm -f $vmLocation/$snap.qcow2
      
        virsh snapshot-delete --metadata $name $snap
      
        exit ${1:-0}
      
      }
      
      function argument_snap () {
      
        virsh dumpxml $name > $location/$name.xml
      
        diskPath=$(virsh domblklist $name | grep -i $disk | awk '{ print $2 }')
      
        virsh snapshot-create-as --domain $name $snap --diskspec $disk,file=$vmLocation/$snap.qcow2 --disk-only --atomic
      
        tar -czvf $location/$snap-$today.tar.gz $diskPath
      
        virsh blockcommit $name $disk --active --verbose --pivot
      
        rm -f $vmLocation/$snap.qcow2
      
        virsh snapshot-delete --metadata $name $snap
      
        exit ${1:-0}
      
      
      
      }
      
      #Show help if no arguments or options are passed
      [[ ! "$*" ]] && script_help 1
      OPTIND=1
      
      
      #Read command line options
      while getopts "ih" opt; do
          case "$opt" in
            i) interactive_snap ;;
            h) script_help ;;
            \?) script_help 1 ;;
          esac
      done
      shift $(($OPTIND-1));
      
      #Run argument function
      name=$1
      snap=$2
      location=$3
      disk=$4
      argument_snap
      
      matteo nunziatiM B 2 Replies Last reply Reply Quote 11
      • matteo nunziatiM
        matteo nunziati @stacksofplates
        last edited by

        @stacksofplates maybe of interest: my libvirt backup wrapper

        1 Reply Last reply Reply Quote 1
        • RomoR
          Romo
          last edited by

          @stacksofplates this basically is a super fast clone of the original vm using snapshots?

          stacksofplatesS 1 Reply Last reply Reply Quote 0
          • RomoR
            Romo
            last edited by

            I haven't used external snapshots for anything, but now that I am reading about them I should be using them more.

            stacksofplatesS 1 Reply Last reply Reply Quote 2
            • stacksofplatesS
              stacksofplates @Romo
              last edited by

              @Romo said in KVM Snapshot/Backup Script:

              @stacksofplates this basically is a super fast clone of the original vm using snapshots?

              It takes a snapshot, which directs writes to the new file. Then tars and gzips the backing store (original disk image) to wherever you put in for the location, and then merges the snapshot back into the backing store.

              RomoR 1 Reply Last reply Reply Quote 2
              • stacksofplatesS
                stacksofplates @Romo
                last edited by

                @Romo said in KVM Snapshot/Backup Script:

                I haven't used external snapshots for anything, but now that I am reading about them I should be using them more.

                I still use internal for things like updates and large changes, but the external ones are nice.

                1 Reply Last reply Reply Quote 1
                • RomoR
                  Romo @stacksofplates
                  last edited by

                  @stacksofplates said in KVM Snapshot/Backup Script:

                  @Romo said in KVM Snapshot/Backup Script:

                  @stacksofplates this basically is a super fast clone of the original vm using snapshots?

                  It takes a snapshot, which directs writes to the new file. Then tars and gzips the backing store (original disk image) to wherever you put in for the location, and then merges the snapshot back into the backing store.

                  This is too keep the system live during the backup?

                  stacksofplatesS 1 Reply Last reply Reply Quote 0
                  • stacksofplatesS
                    stacksofplates @Romo
                    last edited by

                    @Romo said in KVM Snapshot/Backup Script:

                    @stacksofplates said in KVM Snapshot/Backup Script:

                    @Romo said in KVM Snapshot/Backup Script:

                    @stacksofplates this basically is a super fast clone of the original vm using snapshots?

                    It takes a snapshot, which directs writes to the new file. Then tars and gzips the backing store (original disk image) to wherever you put in for the location, and then merges the snapshot back into the backing store.

                    This is too keep the system live during the backup?

                    Right. If you don't do it this way, you have to either shut the VM down or suspend it.

                    1 Reply Last reply Reply Quote 0
                    • RomoR
                      Romo
                      last edited by

                      Are you using external snapshots to thin provision any vms? Is there a performance hit on doing this?

                      stacksofplatesS 1 Reply Last reply Reply Quote 0
                      • stacksofplatesS
                        stacksofplates @Romo
                        last edited by

                        @Romo said in KVM Snapshot/Backup Script:

                        Are you using external snapshots to thin provision any vms? Is there a performance hit on doing this?

                        No. I have a template that uses a qcow2 disk. It's only a 15GB disk, but since it's thin provisioned it's only around 1.5GB. I can clone it in about 1-2 seconds so I haven't bothered with doing externals for that. However, it does make a nice VDI tool. If you have an image and do qemu-img create -b and use it as the backing file, the overlays spin up really quickly. Like less than a second. But you can't write to the backing file as long as there are overlay files reading from it.

                        RomoR 1 Reply Last reply Reply Quote 1
                        • stacksofplatesS
                          stacksofplates
                          last edited by

                          I create the template, and run virt-sysprep on it. Then I can update the disk with virt-sysprep --update. It automatically spins up a temp VM that updates all of the packages in the disk. But if you do this, you need to run virt-sysprep --selinux-relabel so it relabels the disk on the next clone. If not, labels can get screwed up;

                          RomoR 1 Reply Last reply Reply Quote 1
                          • RomoR
                            Romo @stacksofplates
                            last edited by

                            @stacksofplates said in KVM Snapshot/Backup Script:

                            @Romo said in KVM Snapshot/Backup Script:

                            Are you using external snapshots to thin provision any vms? Is there a performance hit on doing this?

                            No. I have a template that uses a qcow2 disk. It's only a 15GB disk, but since it's thin provisioned it's only around 1.5GB. I can clone it in about 1-2 seconds so I haven't bothered with doing externals for that.

                            My clones take 30-40 seconds, how do you thin provision? Using virt-sparsify on an image?

                            stacksofplatesS 1 Reply Last reply Reply Quote 0
                            • stacksofplatesS
                              stacksofplates @Romo
                              last edited by

                              @Romo said in KVM Snapshot/Backup Script:

                              @stacksofplates said in KVM Snapshot/Backup Script:

                              @Romo said in KVM Snapshot/Backup Script:

                              Are you using external snapshots to thin provision any vms? Is there a performance hit on doing this?

                              No. I have a template that uses a qcow2 disk. It's only a 15GB disk, but since it's thin provisioned it's only around 1.5GB. I can clone it in about 1-2 seconds so I haven't bothered with doing externals for that.

                              My clones take 30-40 seconds, how do you thin provision? Using virt-sparsify on an image?

                              No, qcow2 is thin by default. But all of my templates are RHEL systems. So the OS doesn't use hardly any space. Are you cloning Windows machines?

                              RomoR 1 Reply Last reply Reply Quote 0
                              • RomoR
                                Romo @stacksofplates
                                last edited by

                                @stacksofplates said in KVM Snapshot/Backup Script:

                                @Romo said in KVM Snapshot/Backup Script:

                                @stacksofplates said in KVM Snapshot/Backup Script:

                                @Romo said in KVM Snapshot/Backup Script:

                                Are you using external snapshots to thin provision any vms? Is there a performance hit on doing this?

                                No. I have a template that uses a qcow2 disk. It's only a 15GB disk, but since it's thin provisioned it's only around 1.5GB. I can clone it in about 1-2 seconds so I haven't bothered with doing externals for that.

                                My clones take 30-40 seconds, how do you thin provision? Using virt-sparsify on an image?

                                No, qcow2 is thin by default. But all of my templates are RHEL systems. So the OS doesn't use hardly any space. Are you cloning Windows machines?

                                No, no windows

                                stacksofplatesS 1 Reply Last reply Reply Quote 0
                                • RomoR
                                  Romo @stacksofplates
                                  last edited by

                                  @stacksofplates said in KVM Snapshot/Backup Script:

                                  I create the template, and run virt-sysprep on it. Then I can update the disk with virt-sysprep --update. It automatically spins up a temp VM that updates all of the packages in the disk. But if you do this, you need to run virt-sysprep --selinux-relabel so it relabels the disk on the next clone. If not, labels can get screwed up;

                                  Is this is only available for RHEL guests or can it be used with other distros?

                                  stacksofplatesS 1 Reply Last reply Reply Quote 0
                                  • stacksofplatesS
                                    stacksofplates @Romo
                                    last edited by

                                    @Romo said in KVM Snapshot/Backup Script:

                                    @stacksofplates said in KVM Snapshot/Backup Script:

                                    @Romo said in KVM Snapshot/Backup Script:

                                    @stacksofplates said in KVM Snapshot/Backup Script:

                                    @Romo said in KVM Snapshot/Backup Script:

                                    Are you using external snapshots to thin provision any vms? Is there a performance hit on doing this?

                                    No. I have a template that uses a qcow2 disk. It's only a 15GB disk, but since it's thin provisioned it's only around 1.5GB. I can clone it in about 1-2 seconds so I haven't bothered with doing externals for that.

                                    My clones take 30-40 seconds, how do you thin provision? Using virt-sparsify on an image?

                                    No, qcow2 is thin by default. But all of my templates are RHEL systems. So the OS doesn't use hardly any space. Are you cloning Windows machines?

                                    No, no windows

                                    Hmm, I don't have anything special. Some 300G 10K SAS drives in RAID 10.

                                    Here's a video I did for Dash:
                                    Youtube Video

                                    And another of a script I wrote that names the VM and spins up how many instances you tell it:
                                    Youtube Video

                                    RomoR 1 Reply Last reply Reply Quote 1
                                    • stacksofplatesS
                                      stacksofplates @Romo
                                      last edited by

                                      @Romo said in KVM Snapshot/Backup Script:

                                      @stacksofplates said in KVM Snapshot/Backup Script:

                                      I create the template, and run virt-sysprep on it. Then I can update the disk with virt-sysprep --update. It automatically spins up a temp VM that updates all of the packages in the disk. But if you do this, you need to run virt-sysprep --selinux-relabel so it relabels the disk on the next clone. If not, labels can get screwed up;

                                      Is this is only available for RHEL guests or can it be used with other distros?

                                      You should be able to sysprep Ubuntu also. Not sure about any others.

                                      1 Reply Last reply Reply Quote 0
                                      • RomoR
                                        Romo @stacksofplates
                                        last edited by

                                        @stacksofplates said in KVM Snapshot/Backup Script:

                                        @Romo said in KVM Snapshot/Backup Script:

                                        @stacksofplates said in KVM Snapshot/Backup Script:

                                        @Romo said in KVM Snapshot/Backup Script:

                                        @stacksofplates said in KVM Snapshot/Backup Script:

                                        @Romo said in KVM Snapshot/Backup Script:

                                        Are you using external snapshots to thin provision any vms? Is there a performance hit on doing this?

                                        No. I have a template that uses a qcow2 disk. It's only a 15GB disk, but since it's thin provisioned it's only around 1.5GB. I can clone it in about 1-2 seconds so I haven't bothered with doing externals for that.

                                        My clones take 30-40 seconds, how do you thin provision? Using virt-sparsify on an image?

                                        No, qcow2 is thin by default. But all of my templates are RHEL systems. So the OS doesn't use hardly any space. Are you cloning Windows machines?

                                        No, no windows

                                        Hmm, I don't have anything special. Some 300G 10K SAS drives in RAID 10.

                                        Here's a video I did for Dash:
                                        Youtube Video

                                        And another of a script I wrote that names the VM and spins up how many instances you tell it:
                                        Youtube Video

                                        I want that speed!! I am on 4 500GB 7200 SATA in RAID 10

                                        stacksofplatesS 1 Reply Last reply Reply Quote 1
                                        • stacksofplatesS
                                          stacksofplates @Romo
                                          last edited by

                                          @Romo said in KVM Snapshot/Backup Script:

                                          @stacksofplates said in KVM Snapshot/Backup Script:

                                          @Romo said in KVM Snapshot/Backup Script:

                                          @stacksofplates said in KVM Snapshot/Backup Script:

                                          @Romo said in KVM Snapshot/Backup Script:

                                          @stacksofplates said in KVM Snapshot/Backup Script:

                                          @Romo said in KVM Snapshot/Backup Script:

                                          Are you using external snapshots to thin provision any vms? Is there a performance hit on doing this?

                                          No. I have a template that uses a qcow2 disk. It's only a 15GB disk, but since it's thin provisioned it's only around 1.5GB. I can clone it in about 1-2 seconds so I haven't bothered with doing externals for that.

                                          My clones take 30-40 seconds, how do you thin provision? Using virt-sparsify on an image?

                                          No, qcow2 is thin by default. But all of my templates are RHEL systems. So the OS doesn't use hardly any space. Are you cloning Windows machines?

                                          No, no windows

                                          Hmm, I don't have anything special. Some 300G 10K SAS drives in RAID 10.

                                          Here's a video I did for Dash:
                                          Youtube Video

                                          And another of a script I wrote that names the VM and spins up how many instances you tell it:
                                          Youtube Video

                                          I want that speed!! I am on 4 500GB 7200 SATA in RAID 10

                                          How big is your template?

                                          2.0G -rw-------. 1 root root  16G Feb 13 03:37 template.qcow2
                                          

                                          That's what I have.

                                          RomoR 1 Reply Last reply Reply Quote 0
                                          • stacksofplatesS
                                            stacksofplates
                                            last edited by

                                            Also it's not virt-sysprep --update it's virt-customize --update. I didn't think that was right, so I just went back and looked.

                                            I have a daily cron job that runs this

                                            /bin/virt-customize --update --selinux-relabel -a /data/VMs/template.qcow2
                                            
                                            1 Reply Last reply Reply Quote 1
                                            • 1
                                            • 2
                                            • 3
                                            • 1 / 3
                                            • First post
                                              Last post