0

Not sure where to begin I only know basic text processing.

I am trying to create a script that will search for zfs snapshots based off a VM name (given as a command parametet) and then select the newest snapshot and finally transfer it to another server.

For example I will run

script.sh 2839

Which will first run

zfs list -t snapshot | grep "vm-2839"

find the following files, and select the newest one based off the date

zfs/backups/vm-2839-disk-0@ZAP_home1_2023-01-20T21:53:29p0000--1d      193M      -     11.9G  -
zfs/backups/vm-2839-disk-0@ZAP_home1_2023-01-22T11:54:19p0000--1d     18.2M      -     11.9G  -
zfs/backups/vm-2839-disk-0@ZAP_home1_2023-01-22T16:08:20p0000--1d        0B      -     11.9G  -

It should then finally send that file to an external server

zfs send zfs/backups/vm-2839-disk-0@ZAP_home1_2023-01-22T16:08:20p0000--1d | ssh [email protected] "zfs receive zfs/vm-2839-disk-0"
1
  • 2
    I wondered a lot about what snapshots you're referring to until I saw your zfs … command line :D Added that tage :) Commented Jan 22, 2023 at 20:28

1 Answer 1

0

Luckily, the date format you're using is numerically sortable.

So the approach to this is

  • Tell zfs list to not output data you don't care about
  • Tell zfs list to sort according to the date of creation
  • take the first line of output and roll with it

So, I don't have zfs set up on the machine I'm sitting in front of right now, so I'm just going of the official documentation – as you should have!

#!/bin/bash

machine=$1 # save the first passed argument in the variable "machine"

zfs list -o name -s creation -t snapshot | grep "vm-${machine}" |tail -n1 | xargs zfs send | ssh [email protected] "zfs receive zfs/vm-${machine}-disk-0"

#         ^------------------------------ specify the field(s) to display;
#                ^                        "name" is the first mentioned on the
#                |                        man page.
#                |
#                \----------------------- specify which field to sort by. As
#                                         linked to, zfsprops man page tells
#                                         that "creation" is the time the
#                                         snapshot was created
#
#                                                             ^---- pipe to "tail", which
#                                                                   ^---- keeps the last 1 line(s)
#
#                                                                        ^---- pipe to xargs, which
#                                                                              takes the input and
#                                                                              appends it as argument
#                                                                              to the passed command
#                                                                              "zfs send"
#          
#                                                                                      ^---- pipe to
#                                                                                            your ssh
#                                                                                            as before
6
  • 2839 is not the only virtual machine. If I have 10 virtual machines that wont work (2839 is the virtual machine ID)
    – Teddy77
    Commented Jan 22, 2023 at 20:52
  • hope the edit helps Commented Jan 22, 2023 at 20:59
  • Thanks works well. Maybe a silly question but if I have two SSH sessions running and I do this command in each window will the machine=$1 conflict? As it takes a while to move data
    – Teddy77
    Commented Jan 22, 2023 at 21:59
  • No it won't conflict. $1 is the first Argument passed to each execution of the script. The different executions are fully independent and isolated Commented Jan 23, 2023 at 7:12
  • I have one issue with this if a virtual machine has the ID 100 but another has ID 1000 it will conflict as they both contain "100" can can we adjust the command to require the VM ID to be exactly 100?
    – Teddy77
    Commented Jan 25, 2023 at 17:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.