m0nkey_
MVP
- Joined
- Oct 27, 2015
- Messages
- 2,739
This doesn't come up very often, but it was something I needed. I wanted to backup my jails to an external backup source. Now, my target doesn't support ZFS, if it did, this would be a non-issue. So, I developed a little script that will automate the backups of jails to compressed files.
So, without further a do, here's the script:
Great, thanks @m0nkey_! Now what do I do? Well my friend, that's up to you. I plan to run this script about once every week, however I feel that might still be too often and reduce it down to once a fortnight or a month.
Where do I put the file? That's entirely up to you. I have a dataset known as 'opt' on my main pool where I usually keep scripts, configuration backups, etc. I also write my jail backups to this dataset too, since it's a dataset I backup on a regular basis.
How do I define what jails are backed up? You will need to create yourself a plain text file, doesn't matter what it's called, listing all your jails. For example; I have three jails: plex, unifi and znc. So I have a jail-list.txt with the contents:
My environment isn't the same as yours! What do I need to modify to make it work? You will need to modify various parameters to make it work in your environment. You need to modify JAIL_LIST, JAIL_POOL, JAIL_PATH and JAIL_DEST.
How do I schedule this fantastic script of yours? Well, that can be explained by the wonderful FreeNAS documentation. You can read up about 'Tasks' or cron jobs here: http://doc.freenas.org/9.3/freenas_tasks.html#cron-jobs
How do I restore a jail from a file? I've not had to do this yet, but I imagine this would work. You'd need to test it!
How do I get help for this script? Reply to this post, I might answer. Or you can find me on IRC in #freenas on Freenode.
YOUR SCRIPT F**KED UP! SOMETHING BROKE! FIX IT! FIX IT! Sorry, but this script is provided as-is without warranty. You should really know not to run un-tested 3rd party code in production. Always test in a virtual machine or something prior. Don't do what I do, wrote and tested this on a live box.
So, without further a do, here's the script:
Code:
#!/bin/sh JAIL_LIST="/mnt/tank/opt/jail-list.txt" # jail list file JAIL_POOL="bucket" # pool containing jails JAIL_PATH="jails" # jails dataset JAIL_DATE=`date +%Y%m%d` # todays date (YYYYMMDD) JAIL_DEST="/mnt/tank/opt/jailbackup" # backup destination NUM_OF_BACKUPS="1" # Keep number of backups # jail-list.txt should contain the list of jails you want to backup, for example: # # plex # znc # owncloud tabs 4 echo "Start backup of jails" while read JAIL_NAME; do printf '\t%s' "${JAIL_NAME}" JAIL_SNAP="${JAIL_POOL}/${JAIL_PATH}/${JAIL_NAME}@backup-${JAIL_DATE}" JAIL_GZIP="${JAIL_DEST}/${JAIL_NAME}-${JAIL_DATE}.gz" zfs snapshot ${JAIL_SNAP} zfs send ${JAIL_SNAP} | gzip > ${JAIL_GZIP} JAIL_SIZE=`stat -f%z ${JAIL_GZIP}` printf ' (%s bytes)\n' "${JAIL_SIZE}" zfs destroy ${JAIL_SNAP} JAIL_FILE=`ls -b ${JAIL_DEST}/${JAIL_NAME}-*.gz | sort -r` COUNT=0 for FILE in ${JAIL_FILE}; do COUNT=`expr ${COUNT} + 1` if [ ${COUNT} -gt ${NUM_OF_BACKUPS} ]; then rm -f ${FILE} fi done done < ${JAIL_LIST} echo "Backup complete" ### To restore a gzip'd jail: ### zfs receive pool/jails/jailname < gzip -c jailname.gz
Great, thanks @m0nkey_! Now what do I do? Well my friend, that's up to you. I plan to run this script about once every week, however I feel that might still be too often and reduce it down to once a fortnight or a month.
Where do I put the file? That's entirely up to you. I have a dataset known as 'opt' on my main pool where I usually keep scripts, configuration backups, etc. I also write my jail backups to this dataset too, since it's a dataset I backup on a regular basis.
How do I define what jails are backed up? You will need to create yourself a plain text file, doesn't matter what it's called, listing all your jails. For example; I have three jails: plex, unifi and znc. So I have a jail-list.txt with the contents:
Code:
plex unifi znc
My environment isn't the same as yours! What do I need to modify to make it work? You will need to modify various parameters to make it work in your environment. You need to modify JAIL_LIST, JAIL_POOL, JAIL_PATH and JAIL_DEST.
How do I schedule this fantastic script of yours? Well, that can be explained by the wonderful FreeNAS documentation. You can read up about 'Tasks' or cron jobs here: http://doc.freenas.org/9.3/freenas_tasks.html#cron-jobs
How do I restore a jail from a file? I've not had to do this yet, but I imagine this would work. You'd need to test it!
Code:
zfs receive pool/jails/jailname < gzip -c jailname.gz
How do I get help for this script? Reply to this post, I might answer. Or you can find me on IRC in #freenas on Freenode.
YOUR SCRIPT F**KED UP! SOMETHING BROKE! FIX IT! FIX IT! Sorry, but this script is provided as-is without warranty. You should really know not to run un-tested 3rd party code in production. Always test in a virtual machine or something prior. Don't do what I do, wrote and tested this on a live box.
Last edited: