I just needed a small shell (bash) script to synchronize the zfs snapshots of the dataset pool1/gesichert on two different PCs (PC1 and PC2) to be run on PC2.
I asked Grok (MechaHitler), here is what it came up with:
#!/bin/bash # Get latest snapshot on PC2 PC2_LATEST=$(zfs list -t snapshot -o name -s creation -r pool1/gesichert | tail -1 | cut -d' ' -f1 | cut -d'@' -f2) # Get latest snapshot on PC1 PC1_LATEST=$(ssh PC1 zfs list -t snapshot -o name -s creation -r pool1/gesichert | tail -1 | cut -d' ' -f1 | cut -d'@' -f2) # Send incremental snapshots from PC1 to PC2 ssh PC1 zfs send -i pool1/gesichert@$PC2_LATEST pool1/gesichert@$PC1_LATEST | zfs receive -F pool1/gesichert
DO NOT use this!
There are several things wrong with it, most of them just minor points but one possible danger:
- The the -s creation switch for the zfs list command is not necessary as that’s the default sorting order (within the snapshots of a single dataset)
- The -r switch for the zfs list command is not necessary unless the dataset has nested datasets.
- The cut -d' ' -f1 command is not necessary because zfs list -o name only outputs a single field.
- There is no check whether the snapshots on PC1 and PC2 are actually different, so the zfs send command may fail.
- The -i switch for the zfs send command will only send the difference between the given snapshots and leave out any intermediate ones. To get these too, the -I switch (upper case i) must be used.
- The -F switch for the zfs receive command forces it to roll back the local dataset to the last snapshot. This will lose any changes that may have been made to it afterwards. This may be what you want, but it is dangerous.
I could have written that script in a fraction of the time it took me to specify the requirements. And that doesn’t even count the time to fix it. But I wanted to see what Grok can do. I was able to actually understand what the script does and spot the problems with it. Somebody with less experience might have just used it as is.