VM per Skript sichern

Allgemeine Diskussionen über den Einsatz von VirtualBox.
Post Reply
networker_23
Posts: 3
Joined: 26. Mar 2022, 21:29

VM per Skript sichern

Post by networker_23 »

Ich habe VirtualBox unter Windows 10 laufen und in VirtualBox eine VM mit Ubuntu erstellt (für NextCloud).

Nun möchte ich diese VM automatisiert sichern (wöchentlich). Wie kann ich per Skript die VM snapshotten und ggf. auch das VM File sichern? Die VM muss hierfür wahrscheinlich heruntergefahren, gesichert und wieder gestartet werden....
scottgus1
Site Moderator
Posts: 20945
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: VM per Skript sichern

Post by scottgus1 »

networker_23 wrote:use a script to snapshot the VM
Don't. :wink: VirtualBox's snapshots are changed-disk-sector-and-hardware files, all parts of a chain. If the chain breaks, the VM is dead and data will be lost. Snapshots are for 'time travel' in the VM's history, not backups. Snapshots make the VM more delicate.
networker_23 wrote:use a script to ... back up the VM file
Look over the 'vboxmanage' command, especially the 'controlvm acpipowerbutton' and 'startvm' subcommands. These shut down the VM and restart the VM respectively. Also 'vboxmanage showvminfo', which will tell the script when the VM has fully shut down.

Your script should:
1. Shut down the VM completely, not save-state. Set the VM OS to respond to an 'acpi power button' push with a complete shutdown, then 'vboxmanage controlvm "VM name" acpipowerbutton' will tap the VM's 'power button' and the OS will shut down.
2. Go through a delayed loop, calling 'vboxmanage showvminfo' and checking the line "State:" for "powered off".
3. Copy the VM's whole folder, and any disk files stored outside the VM folder, to backup media. Do file-compares to confirm that the copy is accurate. If you want to rsync the files over the internet to offsite backup, also run a hash on the originals beforehand and on the offsite backup later, to confirm that the offsite backup is accurate. (File compares and hashing take extra time, but knowing the backups are accurate is really heartening.)
4. Start the VM again.

If you must run backups while the VM is running, don't use any routine that calls on snapshots. Instead use 3rd-party in-the-VM backup software that is compatible with the services running in the VM, and save the backup images out through host shared folders over a network onto backup media.
networker_23
Posts: 3
Joined: 26. Mar 2022, 21:29

Re: VM per Skript sichern

Post by networker_23 »

Gibt es ein Beispiel Skript für die einzelnen Schritte, das man auf die eigenen Pfade anpassen kann?
scottgus1
Site Moderator
Posts: 20945
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: VM per Skript sichern

Post by scottgus1 »

Not that I know of on the forum. I did do this on a host once, but it was highly specialized to the needs of that backup routine, and I don't have that host anymore.

Try to come up with something, and if you have problems, post what you have and we'll try to get it running.
networker_23
Posts: 3
Joined: 26. Mar 2022, 21:29

Re: VM per Skript sichern

Post by networker_23 »

Kann ich den Virtual Box Ordner unter folgenden Pfad auch im laufenden Betrieb kopieren?

C:\Users\Username\VirtualBox VMs

Ist diese Sicherung brauchbar, wenn ich die VM vor der Sicherung nicht herunterfahre?
scottgus1
Site Moderator
Posts: 20945
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: VM per Skript sichern

Post by scottgus1 »

networker_23 wrote:C:\Users\Username\VirtualBox VMs
This is the default folder for new VMs. This default can be changed in Virtualbox, and a VM can exist anywhere on the host, not only in this folder.

Additionally, copying this folder backs up all the VMs inside it, which may or may not be what you want.

The 'VM folder' I mentioned above is the folder containing the VM's .vbox file, also 'Logs' subfolder and possibly 'Snapshots' subfolder. The VM's disk file may also be in the VM folder, or it may have been put elsewhere by the person setting up the VM.

Your host OS may not let you copy the VM's disk file if the VM is running. Even if it does let you copy, the VM will still be writing to the disk file while the host is reading it, leading to corrupted data in the backup and inability to file-compare or hash. The VM should be fully shut down for VM folder copying.

Use the 3rd-party in-the-VM backup software idea to back up a running VM. I should note that you should have at least one VM-folder-copy backup to go along with the 3rd-party software backups.
Emma2
Posts: 51
Joined: 16. Feb 2021, 11:59

Re: VM per Skript sichern

Post by Emma2 »

Hallo, networker_23.

Ich habe vor gut einem Jahr die gleiche Frage gestellt, und wir haben lange darüber diskutiert.
Ich habe damals erfahren, dass - anders als bei Hyper-V - ein Snapshot, der von einer "laufenden" VM genommen wird, nicht hilfreich ist, denn er ist auch nur in einem Zustand wie nach einer plump ausgeschalteten Maschine.
Eine VirtualBox-VM kann - wieder anders als bei Hyper-V - durch Kopieren gesichert werden, sie muss oder sollte dafür aber heruntergefahren sein.
Am Ende der für mich sehr lehrreichen Diskussion habe ich mehrere Skripten erstellt, mit denen ich seither erfolgreich meine VMs sichere. (Erfolgreich? Ja, ich musste schon mehrmals eine kaputt-konfigurierte aus dem Backup wiederherstellen.)

Diese Skripten machen im Prinzip das Folgende:
1. Prüfe, ob die VM läuft. Wenn ja, dann fahre sie mit dem Befehl vboxmanage controlvm <vm-name> acpipowerbutton herunter. Warte, bis sie aus ist. NB: Bei Windows-VMs schlägt das regelmäßig fehl, wenn dort jemand angemeldet ist.
2. Wenn die VM heruntergefahren ist (und nur dann!), kopiere sie in einen lokalen Sicherungsordner (lokal deshalb, weil das - zumindest bei mir - schneller geht). Da meine Hosts Ubuntu-Linux fahren, nutze ich rsync dafür. Danach kann ich die VM schon wieder starten: vboxmanagae startvm <vm-name> --type headless.
3. Auf dem Backup-Laufwerk (aber das ist meine individuelle Strategie) habe ich mehrere Ordner, z.B. Week1, Week2, Week3. Dort lösche ich Week3, umbenenne Week2 zu Week3 und Week1 zu Week2 und kopiere dann die lokale Sicherung zu Week1. (Da ich auf Linux arbeite kopiere ich nicht wirklich, sondern nutze auch hier rsync; das hat den Vorteil, dass kein unnötiger Platz verschwendet wird.

Das Skript kann ich - ohne Garantie! - gern zur Verfügung stellen, aber es ist eben ein Bash-Skript und ist unter Windows nicht direkt zu gebrauchen.

Off-Topic:
Ich bin übrigens den anderen Weg gegangen und habe seit drei Jahren fast alle meine Windows-Server und -Clients gegen Linuxe ausgetauscht (Ubuntu für die Server, Mint für die Clients, Debian für Spezialanwendungen). Das hat sich insbesondere für die Virtualisierungshosts gelohnt: Ubuntu mit Virtualbox läuft um ein Vielfaches besser, schneller, sicherer als Hyper-V auf dem Windows-Server - man muss sich nur ein bisschen an die Arbeit mit der Shell und vboxmanage gewöhnen, das ist aber kein Hexenwerk. Windows habe ich nur noch als VMs laufen und auch nur noch, wo ich es brauche (im Prinzip meine Buchhaltung und das VisualStudio). Ist vielleicht eine Überlegung wert...
Post Reply