Page 1 of 1
VBoxManage scripting with variable
Posted: 27. Mar 2020, 17:34
by efftee
Good day to all,
Currently aiming to automate backups of my VMs with some cron jobs, I'm running into a problem while trying to put a date variable in the clonevm name.
Here is my script :
Code: Select all
#!/bin/bash
NOW=$(date +"%Y%m%d")
VBoxManage clonevm BackupTest --basefolder="/home/x/VirtualBox VMs/BackupSync/" --mode=machine --name="$NOW_test" --options=KeepAllMACs --options=KeepDiskNames --options=KeepHwUUIDs
exit
So basically, i get the answer as follows :
VBoxManage: error: Machine name is invalid, must not be empty
is it not possible to agregate a variable date to a machine name ?
(PS: when i remove the variable and only use --name="test", the script works fine...)
Kind regards;
Re: VBoxManage scripting with variable
Posted: 27. Mar 2020, 17:54
by efftee
Ok, so I've found a solution...
it turns out the variable works when the syntax is changed from
--name="$NOW_test"
TO
--name test_$NOW
Because I had to remove the "", I've also had to place the --name argument at the very end of the command so that the --options would not end up in the name ...
Here is the script line that works :
Code: Select all
VBoxManage clonevm BackupTest --basefolder="/home/x/VirtualBox VMs/BackupSync/" --options=KeepAllMACs --options=KeepDiskNames --options=KeepHwUUIDs --mode=machine --name test_$NOW

Re: VBoxManage scripting with variable
Posted: 27. Mar 2020, 20:53
by efftee
Pursuing tests I have the impression that some of the arguments are not taken into account in this order...
Does the order matter for the arguments ?
Re: VBoxManage scripting with variable
Posted: 27. Mar 2020, 21:06
by efftee
VBoxManage: error: Cannot register the hard disk '/home/x/VirtualBox VMs/BackupSync/test_20200327/Ubuntu18Server-disk001.vdi' {0093b44f-4cd9-4473-837c-f3cedf627090} because a hard disk '/home/x/VirtualBox VMs/BackupSync/test_20200327/Ubuntu18Server-disk001.vdi' with UUID {dc25c2bd-3385-41c3-bdfc-800c18dfba53} already exists
Yet I did not include the register argument, it should not try to register by default correct ?
Re: VBoxManage scripting with variable
Posted: 27. Mar 2020, 21:35
by scottgus1
First, going to give you a different viewpoint than fixing the date variable:
Cloning is not a good backup. UUIDs get changed, which may interfere with activated software. Also, since things change the files cannot be FC'd or hashed to ensure backup integrity.
The best backup is a full file & folder copy of a completely shut-down (not save-stated) guest folder, containing all the files therein, as well as any disk files that may reside outside the folder. The backup can be FC file-compared, or hashes can be taken and compared for off-site backup where FC would not be practical. If the disk file(s) are inside the folder right next to the guest's .vbox file, then that backup can be restored to any capable Virtualbox host. If a disk file is outside the guest folder, then the .vbox file contains an absolute path to the disk file, and that path must be re-created on the new host (or the .vbox file needs to be manually edited).
As for dates in a file name, here is a snippet of Windows batch file code (I got off Stack Exchange I think) that renames a file to a time-stamped name:
rename myfile.txt "myfile_%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,5%.txt"
I think the "%date:~X,Y%" means, starting at the Xth character of the output of 'date', take Y characters". This because usual date/time outputs contain characters that aren't allowed in Windows filenames. Linux most likely has an equivalent function.