VirtualBox snapshot delete

Viewed 22

I have database and application server on Oracle Linux 7.6, VirtualBox 6 and I manage to create snapshot once per week with bash script (vboxmange snapshot $(name-%Y-%m-%d))

I would like to delete old snapshots to preserve disk space and I'm looking for model how to delete old snapshots. When list snapshots, I'm getting:

   Name: InitialState (UUID: 0207bfc1-5350-479c-b36b-f1d8fdb520c1)
      Name: odb01-2022-07-15 (UUID: e8982640-d24a-4834-9d55-4221f1ed58db)
         Name: odb01-2022-07-22 (UUID: 0ad82efc-506e-46c1-9d12-e333e74e0b54)
            Name: odb01-2022-07-29 (UUID: f8afbee1-28eb-451c-be79-ad5267a8f7bf)
               Name: odb01-2022-08-05 (UUID: 45cd1be8-9bb6-4f23-9544-6cf2baaf43e5)
                  Name: odb01-2022-08-12 (UUID: 9890309e-cf3e-44c4-95dd-cca96256194d)
                     Name: odb01-2022-08-19 (UUID: 48f567c9-13d7-4c0e-ac54-fe2a7244c9cc)
                        Name: odb01-2022-08-26 (UUID: a74fc590-c039-45ed-8225-c1ca0c9473d5)
                           Name: odb01-2022-09-02 (UUID: bf23548f-24d5-45d2-8d4b-40d8cab792d5) *

My idea is to delete always second or third snapshot (odb01-2022-07-15 or odb01-2022-07-22 in my case).

I'm wondering how to write bash script to extract those information from listing. Is it approach to write those info into txt file and then read second row in txt and extract snapshot name or UUID? The script does not need to be bash, may be python or other similar... anything I can run using CronTab.

Any help will be helpful.THX! Tom

1 Answers

Bash variant:

# Create list(array) of snapshots
mapfile -t snapshots < <(vboxmanage snapshot "$VM_NAME" list)

# Process second or third snapshot
read trash name trash uid trash <<< "${snapshots[1]}" # for 2-nd
read trash name trash uid trash <<< "${snapshots[2]}" # for 3-rd
    
vboxmanage snapshot "$VM_NAME" delete "$name"
Related