# Save and restore a machine

Take a snapshot before you change a machine, and revert to it if the change goes wrong.

## Take and revert snapshots

1. Select the machine and choose **Machine > Take Snapshot**.
2. Enter a name. To save the running state too, select **Include Memory**.
3. Click **Take Snapshot**.

To revert, choose **View > Snapshots**, select the snapshot, and click **Revert**. In Terminal:

```sh
osmates snapshot create dev --name clean --memory
osmates snapshot revert dev clean
```

Reverting discards everything since the snapshot. A running machine is turned off without a shutdown, which loses unfinished writes to shared folders. The current state is saved first, unless you turn off **Before Reverting** in **Snapshots** settings.

## Keep, rename, and delete snapshots

Choose **View > Snapshots**, then select a snapshot.

![The Snapshots view, listing each snapshot's name, date, contents, origin, size, and Keep checkbox](https://osmates.com/docs/images/snapshots-list.png)

- To keep a snapshot from automatic deletion, select **Keep**.
- To rename it, press Return.
- To delete it, press Delete and confirm.

The listed size includes blocks shared with the machine's disk, so deleting frees less. The confirmation and `osmates snapshot list dev` show what it frees.

## Schedule automatic snapshots

1. Choose **Machine > Machine Settings**, then click **Snapshots**.
2. Set **While Running** to **Every Hour** or **Every Day**.
3. Choose whether to **Include Memory**, and how many to **Keep**.

OSMates also takes snapshots by itself, and deletes the oldest of each kind:

| Snapshot | Kept |
|---|---|
| Scheduled, or asked for by an agent | The newest, up to the Keep number, 5 by default |
| Before a revert, an update, or Windows drivers | The newest of each kind, up to the Keep number |
| At the start of an agent session | The newest 3 |
| Taken or kept by you | All of them |

While your Mac is hot or low on memory, a scheduled snapshot waits up to 30 minutes.

## Recover after an OSMates update

Before a machine first starts on a new OSMates version, it saves a `Before OSMates <version>` snapshot, or doesn't start. If the update breaks the machine, revert to that snapshot.

## Go back to an earlier version

1. Read the Changed section of your version's release notes. It says whether older versions open the machines it started.
2. Choose **OSMates > Settings**, then click **Updates**. Turn off **Download and install updates automatically**.
3. Download the earlier version from its release notes. During the beta, see [Join the beta](https://osmates.com/docs/beta).
4. Replace OSMates in Applications and open it. If machines are running, see [Update OSMates](https://osmates.com/docs/update).
5. Revert each machine to `Before OSMates <newer version>`, then start it.
6. When the newer version is offered, click **Skip This Version**.

Replace the app before you revert, or the reverted machine starts on the newer version again.

## Let programs in a machine save and restore it

1. Choose **Machine > Machine Settings**, then click **Agents**.
2. Turn on **Programs in This Machine May Ask for Snapshots, Reverts and Restarts**.
3. In the machine, run `osmates-agent host` as root or an administrator, from an administrator command prompt on Windows.

Inside a Linux machine:

```sh
sudo osmates-agent host snapshot --name before-tests
sudo osmates-agent host revert before-tests
sudo osmates-agent host restart
```

The command exits with 0 when done, 1 when refused, and 3 if the machine was reverted while it waited. `revert` sees only snapshots taken from inside the machine. Find the requests in **Activity**, with **From** set to **Everyone**.

## Limits

- A snapshot needs 6 GiB of free space on the machine's volume. Temporary machines can't take one.
- A running machine pauses for a moment, and about half a second more per GiB of memory saved.
- A Windows snapshot with memory needs free space for the machine's memory plus 1 GiB.
- Memory isn't saved while a [USB device](https://osmates.com/docs/usb) is attached, or in Linux with [3D acceleration](https://osmates.com/docs/linux-graphics) on.
- A Linux machine with a display saves memory only with its USB controller driver. See [Linux guest isn't using its USB controller](https://osmates.com/docs/known-issues#linux-guest-isnt-using-its-usb-controller).
- Windows can stall during the pause. See [Windows stalls around snapshots](https://osmates.com/docs/known-issues#windows-stalls-around-snapshots).
