Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Each jail is in its own fileset. Snapshot each fileset, back it up. Destroy the filesets.
#!/bin/sh
ACTION=$1
JLS=/usr/sbin/jls
JAILS=`${JLS} -h path`
SNAPSHOTDIRECTORY='/.zfs/snapshot/'
SNAPNAME="snapshot-for-backup"
for jail in ${JAILS}
do
# ignore the one value of path, usually the first, because that's the header line.
if [ ${jail} = 'path' ]
then
continue
fi
DATASETNAME=`/sbin/zfs get -H -o name mountpoint ${jail}`
# the snapshot name is of the form: system/usr/local/jails/fedex@snapshot-for-backup
SNAPSHOTFORBACKUP="${DATASETNAME}@${SNAPNAME}"
# the backup dir is of the form: /usr/local/jails/fedex/.zfs/snapshot/snapshot-for-backup
BACKUPDIR="${jail}${SNAPSHOTDIRECTORY}${SNAPNAME}"
case ${ACTION} in
"create")
zfs snapshot ${SNAPSHOTFORBACKUP}
;;
"list")
# echo back out the directory for backup...
echo ${BACKUPDIR}
;;
"destroy")
zfs destroy ${SNAPSHOTFORBACKUP}
;;
esac
done
Job {
Name = "zuul jail snapshots"
JobDefs = "DefaultJob"
Client = zuul-fd
FileSet = "zuul jail snapshots"
Write Bootstrap = "/usr/local/bacula/bsr/zuul-fd-jail-snapshots.bsr"
RunScript {
RunsWhen = Before
FailJobOnError = Yes
Command = "/usr/home/dan/bin/jail-snapshots-for-backup.sh create"
}
RunScript {
RunsWhen = After
FailJobOnError = No
Command = "/usr/home/dan/bin/jail-snapshots-for-backup.sh destroy"
}
}
# the filesets will be different on each jail, thus, we'll always be doing a full unless
# we define this for each jail server
#
FileSet {
Name = "zuul jail snapshots"
Include {
Options {
signature = MD5
Exclude = yes
}
Exclude Dir Containing = .NOBACKUP
File = "\\|/usr/home/dan/bin/jail-snapshots-for-backup.sh list"
}
}
@allanjude

This comment has been minimized.

Copy link

allanjude commented Jan 3, 2015

A recursive snapshot might be better:
zfs snapshot -r system/usr/local/jails@${SNAPNAME}

as it would be faster than creating the snapshots individually, and avoid flushing all of the extra transaction groups, but then you'd have to hard code the jail root dataset somewhere.

Thanks for sharing Dan

@dlangille

This comment has been minimized.

Copy link
Owner Author

dlangille commented Jan 20, 2015

I know we talked about this via IRC, and I think we concluded recursive had a down-side, but I cannot recall what that was. I'll check logs when I'm on my other laptop.

@dlangille

This comment has been minimized.

Copy link
Owner Author

dlangille commented Mar 27, 2015

I know now why 'zfs snapshot -r' doesn't help much. Well, it help for creating the snapshots, but you still (for now) have to remove them one at a time. You also have to iterate through the datasets (zfs list -r) to find the mountpoints.

I realized this as I was rewriting the script to work with iocage (https://github.com/pannon/iocage).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.