Vagrant Snapshot Tutorial - Women Who Code.
post-template-default,single,single-post,postid-52129,single-format-standard,qode-core-1.0.1,ajax_fade,page_not_loaded,,capri child-child-ver-1.0.0,capri-ver-1.6, vertical_menu_with_scroll,smooth_scroll,grid_1200,blog_installed,wpb-js-composer js-comp-ver-4.12.1,vc_responsive

Vagrant Snapshot Tutorial

Vagrant Snapshot Tutorial

Hi, here is a small tutorial on how to make Vagrant snapshots (Vagrant backups). Snapshot is a saved state of a system in a certain time point that allows you to return to that state in case you need it. This lets you experiment and try things and quickly restore back to a previous state.

Like in the case with operating systems, when you work with Vagrant and its virtual machines, it is a good practice to make snapshots to be able to go back in time later. You should always have a backup in case something goes wrong. Typical cases for using snapshots are:

  • You are about to experiment with your system or project
  • Unstable work of a system that you need to fix (reproduce and find bugs)
  • Before work with Chef, Puppet, etc. — make a snapshot before launching them and roll back after
  • Make a snapshot before manual testing and roll back to the clean DB state

In Vagrant, since v 1.8 you have a specific command for working with snapshots:
vagrant snapshot, which has 6 subcommands:

  • push
  • pop
  • save
  • restore
  • list
  • delete

You use these commands from the VM folder of your local machine, like:

Vagrant push and pop

Push and pop are used for quick switching between the states.
Workflow example:
vagrant snapshot push

vagrant ssh

vagrant snapshot pop

Here you do a snapshot with vagrant snapshot push, then enter your VM with vagrant ssh to work on your project, and once you feel you need to roll back, use the vagrant snapsot pop. Vagrant deletes your snapshot once you roll back to it, so if you want to save it, use vagrant snapsot pop --no-delete. Recap:

vagrant snapshot push — creates a snapshot and saves it. This is a shorter version of vagrant snapshot save, that allows you to avoid naming a snapshot.
vagrant snapshot pop — inversion of the vagrant snapshot push, it restores the pushed state.


--[no-]provision — to force running (or not running ) provision while using push and pop, example:
vagrant snapshot push --no-provision
--no-delete — helps to prevent deleting of your snapshot when you are rolling back to it (by default a snapshot gets deleted the moment you roll back to that point).

save and restore

save and restore are longer versions of push and pop.
WARNING: DON’T MIX USING push/pop WITH save/restore, they are unsafe to mix!
For instance, if you did push, proceed with pop, and not with restore!

Using save:
vagrant snapshot save [vm-name] NAME
Saves a new snapshot with the names of your VM and your snapshot.
Using restore:
vagrant snapshot restore [vm-name] NAME
Restores the named snapshot.
--[no-]provision — forces the provisioners to run (or prevent them from doing so).

Vagrant list

vagrant shapshot list — lists all your previously taken snapshots (if you have any) — oldest on top, newest at the bottom.

Vagrant delete

vagrant snapshot delete NAME — deletes a specified named snapshot.

Some providers (such as with VirtualBox) require all «child» snapshots to be deleted first. Vagrant itself doesn’t track «children», but if this is the case, be sure to delete the snapshots in the reverse order they were taken (first delete newer, than older ones).

This command is typically much faster if the machine is halted prior to snapshotting. The deletion can also be done online with most providers.

Official manual:
Vagrant Snapshot

2 комментария
  • Thank you!

    I know I should have assumed that using push/pop instead of save/restore might delete the snap popped off the stack but it still would have been nice if their own Docs on snapshots mentioned that.

    11.04.2018 at 23:28
  • In the case of «vagrant snapshot save [vm-name] NAME».

    How do you get the vm-name? Is it in vagrant box list command or VBoxManage list vms?

    In my case:
    me@me:~/stretch64/vagrant$ VBoxManage list vms
    «vagrant_default_1530710753694_49900» {fad72f9f-a489-4aec-914c-9686b5df5489}
    «Win10» {244a39c1-6bd4-412f-917e-6a19edb77063}

    me@me:~/stretch64/vagrant$ vagrant box list
    debian/stretch64 (virtualbox, 9.4.0)

    So vagrant snapshot save vagrant_default_1530710753694_49900 Backup_date_time ?

    Thank you

    06.07.2018 at 09:59

Post a Comment