Description

Bash script to snapshot zfs within same hierarchy and sync with secondary (backup) server. No need to specify new zfs, all are discovered and sent to receiver. This implementation is intended for KVMs LXCs and container volumes.

Solution

Bash script hosted HERE.

Configure

Copy the script to /usr/local/bin/.

Setup SSH access with keys to destination system.

Edit the variables in the script (or source a config file).

Add a crontab like below. I would normally do daily or twice daily, the 10 minute interval here is just for testing purposes with 2 LXC running nginx on a 1 hour (RETENTION=6) retention.

*/10 * * * * bash -l -c /usr/local/bin/zfs_snap.sh

Results on the source :

[[email protected] ~]# zfs list -t all -r data2/datacenter/lxc
NAME                                                USED  AVAIL     REFER  MOUNTPOINT
data2/datacenter/lxc/www1                            620M  1.45T      620M  /var/lib/lxc/www1
data2/datacenter/lxc/[email protected]:20:02         59K      -      620M  -
data2/datacenter/lxc/[email protected]:30:02         59K      -      620M  -
data2/datacenter/lxc/[email protected]:40:01         59K      -      620M  -
data2/datacenter/lxc/[email protected]:50:01         59K      -      620M  -
data2/datacenter/lxc/[email protected]:00:01         59K      -      620M  -
data2/datacenter/lxc/[email protected]:10:02        256K      -      620M  -
data2/datacenter/lxc/www2                            620M  1.45T      620M  /var/lib/lxc/www2
data2/datacenter/lxc/[email protected]:20:02         59K      -      620M  -
data2/datacenter/lxc/[email protected]:30:02         59K      -      620M  -
data2/datacenter/lxc/[email protected]:40:01         59K      -      620M  -
data2/datacenter/lxc/[email protected]:50:01         59K      -      620M  -
data2/datacenter/lxc/[email protected]:00:01         59K      -      620M  -
data2/datacenter/lxc/[email protected]:10:02        256K      -      620M  -

Results on the destination (only the top level zfs data1/backup/hosts was created here before the script ran from cron) As you can see no retention management implmented for the destination at this time.

[[email protected] ~]# zfs list -t all -r data2/datacenter/lxc
NAME                                                               USED  AVAIL     REFER  MOUNTPOINT
data1/backup/hosts/test1.home.lan/www1                            373M   393G      369M  /data1/backup/hosts/test1.home.lan/www
data1/backup/hosts/test1.home.lan/[email protected]:00:06         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:10:02         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:20:01         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:30:01         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:40:01         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:50:02         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:00:02         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:10:01         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:20:02         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:30:02         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:40:01         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:50:01         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:00:01         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:10:02        112K      -      369M  -
data1/backup/hosts/test1.home.lan/www2                            373M   393G      369M  /data1/backup/hosts/test1.home.lan/www
data1/backup/hosts/test1.home.lan/[email protected]:00:06         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:10:02         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:20:01         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:30:01         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:40:01         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:50:02         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:00:02         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:10:01         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:20:02         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:30:02         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:40:01         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:50:01         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:00:01         96K      -      369M  -
data1/backup/hosts/test1.home.lan/[email protected]:10:02        112K      -      369M  -

Conclusion

Refereneces