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:
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.
vagrant snapshot push
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
restore are longer versions of
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!
vagrant snapshot save [vm-name] NAME
Saves a new snapshot with the names of your VM and your snapshot.
vagrant snapshot restore [vm-name] NAME
Restores the named snapshot.
--[no-]provision — forces the provisioners to run (or prevent them from doing so).
vagrant shapshot list — lists all your previously taken snapshots (if you have any) — oldest on top, newest at the bottom.
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.