How to backup jails and configurations

mbrunelli87

Explorer
Joined
Sep 17, 2016
Messages
65
Hi guys,

I've just managed to install a new Freenas server, with Plex, Transmission and aMule.
It took me a very long time to manually install aMule (approx 5-6 hours) and make everything work (permissions, mounting storage, ...), so now I would like to save the jails and the system configuration. At least, I want to be able to save the aMule jail, since it takes such a long time to install that it would be a nightmare to redo everything.

I searched on Google and found this two threads, but I'm so new to the system that I have a little bit of struggle to understand what to do and how to restore the backup in case of an emergency.
  1. https://forums.freenas.org/index.php?threads/how-to-backup-safely-jails-of-freenas.35874/
  2. [URL]https://forums.freenas.org/index.php?threads/backup-and-restore-help.39402/[/URL]
Can anyone help me and show me how to safely backup jails and system configurations?

Cheers
M.
 

mbrunelli87

Explorer
Joined
Sep 17, 2016
Messages
65
Hey guys,
So I deepened my research and on the documentation I found that you can make a backup with a dedicated wizard if you go to:
System --> Advanced --> Backup
Question: does this save plugins and jails? (I don't want to save ALL the data - movies, etc. - on my HD, but only plugins and jails and the overall config)

Anyway, I could not manage to make the backup work, probably because I make some mistakes in filling out the backup wizard.
I attach the screenshot of the error and of the wizard that you have to fill
 

Attachments

  • backup.jpg
    backup.jpg
    103.9 KB · Views: 1,064
  • error.png
    error.png
    5 KB · Views: 1,082
D

dlavigne

Guest
Question: does this save plugins and jails? (I don't want to save ALL the data - movies, etc. - on my HD, but only plugins and jails and the overall config)

Nope, wrt jails that option only saves the location of the jail root.

You could replicate a snapshot of the jail's dataset to another system should you need to restore that jail.
 

SweetAndLow

Sweet'NASty
Joined
Nov 6, 2013
Messages
6,421
If you end up backing them up make sure you try to restore them also. The gate part isn't backing up it's the restore process.

Sent from my Nexus 5X using Tapatalk
 

mbrunelli87

Explorer
Joined
Sep 17, 2016
Messages
65
Nope, wrt jails that option only saves the location of the jail root.

You could replicate a snapshot of the jail's dataset to another system should you need to restore that jail.

I'm not sure I understood (forgive me, I'm very new to freenas and BSD). So do I have to snapshot the single jails and then (if something bad happens) re-create the jail and run the snapshot?

I'm asking because once I made a snapshot of my entire volume and I tried to recover from it, but it didn't work so I had to re-install aMule and everything else from the beginning
 

mbrunelli87

Explorer
Joined
Sep 17, 2016
Messages
65
I wrote a script a while back to gzip jails using zfs send/recv. You might find it useful:

http://pastebin.com/pr4ACrs0

Hi m0nkey

Just to clarify (as I said i'm an absolute beginner)...
  1. Am I just supposed to run the script you wrote for each jail?
  2. When I'm done with the backup, how can I save the backup files on another machine (a Windows PC in my case), so that if I mess up things with the system I can recover from that?
Thanks
 

akpai

Cadet
Joined
Jun 2, 2019
Messages
2
Thanks, script works fine for iocage jails as well (have to add iocage to the jail path).
 

Wolfeman0101

Patron
Joined
Jun 14, 2012
Messages
428
The link was dead but I found it via Wayback.

Code:
#!/bin/sh

JAIL_LIST="/mnt/tank/opt/scripts/jail-list.txt" # jail list file
JAIL_POOL="tank"                                # pool containing jails
JAIL_PATH="jails"                               # jails dataset
JAIL_DATE=`date +%Y%m%d`                        # todays date (YYYYMMDD)
JAIL_DEST="/mnt/tank/opt/jails"                 # 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:
### gunzip -c jailname.gz | zfs receive pool/jails/jailname
 

Samuel Tai

Never underestimate your own stupidity
Moderator
Joined
Apr 24, 2020
Messages
5,399
This is built into iocage. After shutting down the jail, run iocage export <name of jail>, and the jail will be saved as a TAR file under /mnt/<your pool name>/iocage/images. If you need to rebuild your jail, you can run iocage destroy <name of jail> and then iocage import <name of jail>, which will rebuild the jail from the previous export.
 

Koen_Oostende

Dabbler
Joined
May 20, 2017
Messages
12
Hi Samuel Tai,
Is it possible to export the jail if the data disks (pool) have been imported to a new server. Or do I have to import the pool onto the old server before proceeding the export. We tried to move forward too fast.
Thanks in advance.
 

Samuel Tai

Never underestimate your own stupidity
Moderator
Joined
Apr 24, 2020
Messages
5,399
Hi Samuel Tai,
Is it possible to export the jail if the data disks (pool) have been imported to a new server. Or do I have to import the pool onto the old server before proceeding the export. We tried to move forward too fast.
Thanks in advance.

I don't see why you couldn't export on the new server. The new server should use the old pool as the iocage pool..
 

Koen_Oostende

Dabbler
Joined
May 20, 2017
Messages
12
I made a new boot pool on the new server. (The old boot pool was legacy boot) The new one UEFI boot. I could add the old boot pool disks to the new server. How do I proceed from there?
 

Samuel Tai

Never underestimate your own stupidity
Moderator
Joined
Apr 24, 2020
Messages
5,399
The boot pool has nothing to do with the iocage pool. In Jails, click the little gear:

1611945611535.png


This will allow you to set the pool iocage uses. If this is already set to your data pool, you're set. If not, set it to where your data pool resides. Then you can run the import.
 

Koen_Oostende

Dabbler
Joined
May 20, 2017
Messages
12
So sorry to have troubled you...
The iocage was pointing to a new location.

Never underestimate your own stupidity...
 

TristanM

Dabbler
Joined
Sep 16, 2017
Messages
20
I have been meaning to get around to backing up my Jails for over a couple years. Limited time and knowledge being the major hinderance with several failed attempts followed by "I'll leave it for later" moments. But I am glad to say perseverance won today and I have finally got my jail backup on a schedule and offsite backup to the cloud sorted.

I am extremely new to bash scripting but found the script above by @Wolfeman0101, and read @Samuel Tai 's replies and thought I would take the script and create an iocage version as suggested by Samuel. It has two options for back retention by days or number of files. Hopefully it will be useful to a noob like myself.

Thank you to you both very much (and the original author of the script). Although a very obvious need, finding the information to achieve it with limited knowledge, I found very difficult.

What to do:
  1. Create a folder for you Jail backups. No need to create one with Jail name as script does this.
  2. Created and copy script and text file to a folder in the Jails pool e.g. Vault\Backup\Jails\Scripts that is shared so I can access it. Backup is a SMB share for me
  3. Edit the script file you just copied to suit requirements e.g. update JAIL_POOL, JAIL_PATH, JAIL_DEST. Do not touch JAIL_DATE
  4. Edit the Jails text file and add you jail names. The last entry must have a linebreak on the end
  5. To test:
    1. Open TrueNAS shell
    2. type: bash and you full path to script e.g. bash /mnt/Vault/Backup/freenas_cron_scripts/cron_jails_backup.sh
    3. You can check the backup either in your OS File Explorer or in shell
  6. Schedule. In TrueNAS, create a Cron Job
    1. Go to Tasks > Cron Jobs
    2. Add a new job
    3. Add Description
    4. Add Command: the full path to your script eg. /mnt/Vault/Backup/freenas_cron_scripts/cron_jails_backup.sh.
    5. Run as User: root
    6. Schedule: your choice
    7. Check enabled
    8. Save
  7. For offsite I simply schedule a Cloud Sync Task. You can use AWS S3 or Backblaze B2. Simply create a Cloud Sync Task pointing to your jJAIL_PATH.

cron_jails_backup.sh

Code:
#!/bin/sh
JAIL_POOL="Vault"                                                                 # pool containing jails
JAIL_PATH="jails"                                                                 # jails dataset
JAIL_DATE=`date +%Y-%m-%d`                                                        # todays date (YYYY-MM-DD)
JAIL_DEST="/mnt/Vault/Backup/Jails"                                               # backup destination
RETENTION_TYPE="RET_NUM_OF_BACKUPS"                                                # retention type - possible values RET_NUM_OF_DAYS or RET_NUM_OF_BACKUPS. The respective variables below come into effect based on selection.
RET_NUM_OF_DAYS="10"                                                            # keep number of backups based on age (days)
RET_NUM_OF_BACKUPS="4"                                                           # Keep number of backups - if both .zip and .sha256 ensure even number, double required number backups. Example: if 10 backups, with both .zip and .sha256 set to 20.
JAIL_LIST="/mnt/Vault/Backup/freenas_cron_scripts/cron_jails_backup_list.txt"    # jail list file                  
# JAIL_LIST text file must contain a list of jails to backup. last entry must have carriage return (line break). Example:
#
# plex
# znc
# owncloud
#

echo "-- Start backup of Jails --"

while read JAIL_NAME; do

        echo "Backing up ${JAIL_NAME}"
        iocage stop ${JAIL_NAME}
        iocage export ${JAIL_NAME}
        echo "Making directory ${JAIL_DEST}/${JAIL_NAME}"
        mkdir -p ${JAIL_DEST}/${JAIL_NAME}
        echo "Copying backup from /mnt/${JAIL_POOL}/iocage/images/${JAIL_NAME}_${JAIL_DATE}.* to ${JAIL_DEST}/${JAIL_NAME}"
        cp /mnt/${JAIL_POOL}/iocage/images/${JAIL_NAME}_${JAIL_DATE}.* ${JAIL_DEST}/${JAIL_NAME}/
        iocage start ${JAIL_NAME}      
        COUNT=0
        echo "Retention type set to ${RETENTION_TYPE}"
        if [ $RETENTION_TYPE == "RET_NUM_OF_BACKUPS" ]; then
            JAIL_FILE=$(ls -b ${JAIL_DEST}/${JAIL_NAME}/${JAIL_NAME}*.* | sort -r)
            for FILE in ${JAIL_FILE}; do      
                COUNT=`expr ${COUNT} + 1`
                if [ ${COUNT} -gt ${RET_NUM_OF_BACKUPS} ]; then
                    echo "Removing file #${COUNT} - ${FILE}"
                    rm -f ${FILE}
                else
                    echo "Skipping file #${COUNT} - ${FILE}"
                fi
            done
        elif  [ $RETENTION_TYPE == "RET_NUM_OF_DAYS" ]; then
            JAIL_FILE=$(find ${JAIL_DEST}/${JAIL_NAME}/${JAIL_NAME}*.* -mtime +${RET_NUM_OF_DAYS})
            for FILE in ${JAIL_FILE}; do      
                COUNT=`expr ${COUNT} + 1`      
                echo "Removing file #${COUNT} - ${FILE}"
                rm -f ${FILE}      
            done  
        else
         echo "Retention type ${RETENTION_TYPE} is unknown. Valid options RET_NUM_OF_BACKUPS or RET_NUM_OF_DAYS."
        fi
        rm /mnt/${JAIL_POOL}/iocage/images/${JAIL_NAME}_*
 
 
done < ${JAIL_LIST}
echo "-- End of Backup --"


cron_jails_backup_list.txt

Code:
MQTT
MySQL
NodeRED
 
Last edited:

phier

Patron
Joined
Dec 4, 2012
Messages
398
@Samuel Tai
hello,
is the mentioned way of the jail backup still up to date and the "most reliable"?
Assuming running jail cant be backup - up using "similar toolset... "
thanks!

This is built into iocage. After shutting down the jail, run iocage export <name of jail>, and the jail will be saved as a TAR file under /mnt/<your pool name>/iocage/images. If you need to rebuild your jail, you can run iocage destroy <name of jail> and then iocage import <name of jail>, which will rebuild the jail from the previous export.
 

Patrick M. Hausen

Hall of Famer
Joined
Nov 25, 2013
Messages
7,740
Jails are just files in datasets. Setup a snapshot and a replication task. For restore just copy the datasets back. It's dead easy, which is one of the advantages of jails. You can clone them, adjust the config.json - voila, jail copied. Etc.
 
Top