Migrating multiple Bastille jails from one FreeBSD host to another
Since I wasn’t happy with my hosting provider anymore I chose to switch to a local data center.
Migrating multiple hosts with multiple jails isn’t very scalable, so here’s a script to help you with that.
You can easily automate that using ansible if have a larger number of hosts.
I suggest you use ZFS, otherwise change the compression format from .xz to .txz (in the for loop an import script).
On the source host
Export all jails from on the source host:
for jail in $(bastille list | tail -n +2 | awk '{print $1}'); do bastille export --xz "$jail"; done
scp /usr/local/bastille/backups/* root@newhost:/usr/local/bastille/backups/
On the target host
You need to install bastille first. I like the following howto for that https://hackacad.net/freebsd/2021/01/18/easy-freebsd-jail-management-bastille.html
You can simply run
fetch https://hackacad.net/static/autoimport.sh && chmod +x autoimport.sh && ./autoimport.sh
or create the script by yourself if you don’t trust me:
#!/bin/sh
# configure the export type
target_type=".xz"
# specify the directory to list files from
backups="/usr/local/bastille/backups"
# file to store processed filenames
processed_backups="/tmp/processed_backups.txt"
touch "$processed_backups"
# loop through all files in the directory with extension target_type
for file in "$backups"/*"$target_type"; do
# check if the filename has already been processed
if ! grep -q "^${file}$" "$processed_backups"; then
# add the filename to the processed file
echo "$file" >> "$processed_backups"
# run the bastille import command for the file
bastille import "$file"
fi
done
# remove the processed backups file
rm "$processed_backups"
done