1
0
I have this code:
#!/bin/sh
echo "--------- Backup config files -------------"
find /vmfs/volumes/datastore1/ \
-type f \
-regex '.*\.\(vmx\|nvram\|vmsd\|vmdk\)$' \
! -name '*-flat.vmdk' \
-exec sh -c "$(cat << 'EOF'
addrs=XX.XX.XX.XX
for pth; do
drctry="$(printf '%s' "${pth%/*}" | sed "s/'/'\"'\"'/g")"
ssh "root@$addrs" "mkdir -p '$drctry'" && scp -pr "$pth" "$addrs:'$drctry/'"
done
EOF
)" sh {} +
echo "----------- Backup VM disks ---------------"
vim-cmd vmsvc/getallvms | sed -e '1d' -e 's/ \[.*$//' | while read -r line; do
addrs=XX.XX.XX.XX
vmid="$(printf '%s' "${line%}" | awk '{print $1;}'| sed "s/'/'\"'\"'/g")"
vmname="$(printf '%s' "${line%}" | awk '{for (i=2; i<=NF; i++) print $i}'| sed "s/'/'\"'\"'/g")"
vim-cmd vmsvc/snapshot.create $vmid backup 'Snapshot created by Backup Script' 0 0
scp -pr "/vmfs/volumes/datastore1/$vmname/$vmname-flat.vmdk" $addrs:"/vmfs/volumes/datastore1/$vmname"
vim-cmd vmsvc/snapshot.removeall $vmid
done
the first part (backup config files) works flawless. the second part (backup VM disks) doesn't. The output of vim-cmd line (which starts the loop) is:
1 VM_1
10 VM_2 with_space
11 VM_3 with space
12 VM_4 with_space
14 VM_5
i need to:
- assign first word (the numeric id) to var $vmid
- assign the rest of words to var $vmname so that i can concatenate the path for scp command and run it succesfully without bumping into problems generated by spaces in dir name and file name
At this time i get success for all VMs that doesn't have space in name. For the others i get:
Create Snapshot:
/vmfs/volumes/datastore1/VM_name
with_space/VM_name
with_space.vmdk: No such file or directory
sh: with_space: not found
Remove All Snapshots:
UPDATE 1: For those who need to know the output of "vim-cmd vmsvc/getallvm s", there it is:
Vmid Name File Guest OS Version Annotation
1 VM_1 [datastore1] VM_1/VM_1.vmx winXPProGuest vmx-13
10 VM_2 with_space [datastore1] VM_2 with_space/VM_2 with_space.vmx opensuse64Guest vmx-13
11 VM_3 with space [datastore1] VM_3 with space/VM_3 with space.vmx opensuse64Guest vmx-13
12 VM_4 with_space [datastore1] VM_4 with_space/VM_4 with_space.vmx opensuse64Guest vmx-13
14 VM_5 [datastore1] VM_5/VM_5.vmx winXPProGuest vmx-13
UPDATE 2:
Thanx to @Sorin i got this monster reduced by more than 60% to 8 lines. So, this is the poor man's cloning solution of a free ESXi 6.5 host to another host:
#!/bin/sh
addrs=XXX.XXX.XXX.XXX
IFS="\t" vim-cmd vmsvc/getallvms | sed -e '1d' -e 's/ \[.*//g' -e "s/\s\+/\t/" | while read id name; do
echo "----- Backup "$name" --------"
scp -pr "/vmfs/volumes/datastore1/$name/$name.vmx" "/vmfs/volumes/datastore1/$name/$name.nvram" "/vmfs/volumes/datastore1/$name/$name.vmsd" "/vmfs/volumes/datastore1/$name/$name.vmdk" $addrs:"'/vmfs/volumes/datastore1/$name'"
vim-cmd vmsvc/snapshot.create $id backup 'Snapshot created by Backup Script' 0 0
scp -pr "/vmfs/volumes/datastore1/$name/$name-flat.vmdk" $addrs:"'/vmfs/volumes/datastore1/$name'"
vim-cmd vmsvc/snapshot.removeall $id
done
Thank you !!
There's several way to do that but without example of the output of
vim-cmd vmsvc/getallvms
it's quite hard to help you ... – binarym – 2019-09-24T12:33:00.867it's not that important, the whole line output is the actual output of the script that is already mentioned (the command gives another 2 columns that provide unnecessary info and i scrap those columns with sed) – Costin – 2019-09-24T12:44:23.610
If i request a sample, it's because it's important in my opinion. Your're doing several
sed
that can probably be hugely simplified ... if we know the original output. Without it, i can't and won't help. As you wish ... – binarym – 2019-09-24T12:56:17.380well, if you insist... i added to the post. Thanx. – Costin – 2019-09-24T13:08:14.430
It seems that it is
vim-cmd vmsvc/snapshot.create
that is failing? Which means the script you posted is irrelevant to the problem? (As$vmid
is the only variable that gets passed to it and there shouldn't be problem with it? Which you can confirm easily with anecho
since it should be a number only anyway?) – Tom Yan – 2019-09-24T14:39:58.197@Tom Yan, no. It takes the snapshot. vim-cmd vmsvc/snapshot.create does NOT generate any output (it's a process that is seen only on vSphere). The error is generated by scp command, because of the spaces. Please, concentrate only on the string commands and scp. The others, vmware-related are my area of expertise and all are fine. – Costin – 2019-09-24T15:04:23.730
Well actually it might be easier to debug it yourself because of all your secrecy about the vm names. You can easily isolate whether all the errors are triggered by the scp line by wrapping it with two
echo
es. (The output you posted doesn't match with what you said.) After that you can use a something likefor i in "$arg1" "$arg2"; do echo "$i"; done
to check the two args that are passed to scp. (Just type them in the exact same way you type them afterscp -pr
.) – Tom Yan – 2019-09-24T15:16:34.137If you can't even provide a sample of the check result with only the "secret wordings" replaced and can't tell what's not right in the args yourself, there's nothing we can do. – Tom Yan – 2019-09-24T15:20:37.973