From 101c8d5525a923a70ba7f7df303944167816e3a0 Mon Sep 17 00:00:00 2001
From: Edwin Eefting <edwin@datux.nl>
Date: Wed, 28 Oct 2015 13:22:36 +0100
Subject: [PATCH] also support non gnu xargs

---
 zfs_autobackup | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)

diff --git a/zfs_autobackup b/zfs_autobackup
index 3692024..2909335 100755
--- a/zfs_autobackup
+++ b/zfs_autobackup
@@ -132,9 +132,11 @@ def zfs_get_selected_filesystems(ssh_to, backup_name):
 
 """destroy list of filesystems or snapshots"""
 def zfs_destroy(ssh_to, filesystems):
+
+    debug("Destroying on {0}:\n{1}".format(ssh_to, "\n".join(filesystems)))
     #zfs can only destroy one filesystem at once so we use xargs and stdin
-    run(ssh_to=ssh_to, test=args.test, input="\n".join(filesystems)+"\n", cmd=
-        [ "xargs", "-d", "\\n", "-n", "1", "zfs", "destroy", "-d" ]
+    run(ssh_to=ssh_to, test=args.test, input="\0".join(filesystems), cmd=
+        [ "xargs", "-0", "-n", "1", "zfs", "destroy", "-d" ]
     )
 
 
@@ -345,18 +347,6 @@ for source_filesystem in source_filesystems:
             latest_target_snapshot=send_snapshot
 
 
-#cleanup old target snapshots
-target_destroys=[]
-for target_filesystem in target_snapshots:
-    destroy_count=len(target_snapshots[target_filesystem])-args.keep_target
-    if  destroy_count>0:
-        for snapshot in target_snapshots[target_filesystem][0:destroy_count-1]:
-            target_destroys.append(target_filesystem+"@"+snapshot)
-
-if target_destroys:
-    verbose("Destroying old snapshots on target")
-    zfs_destroy(ssh_to=args.ssh_target, filesystems=target_destroys)
-
 #cleanup old source snapshots
 source_destroys=[]
 for source_filesystem in source_snapshots:
@@ -369,5 +359,19 @@ if source_destroys:
     verbose("Destroying old snapshots on source")
     zfs_destroy(ssh_to=args.ssh_source, filesystems=source_destroys)
 
+
+#cleanup old target snapshots
+target_destroys=[]
+for target_filesystem in target_snapshots:
+    destroy_count=len(target_snapshots[target_filesystem])-args.keep_target
+    if  destroy_count>0:
+        for snapshot in target_snapshots[target_filesystem][0:destroy_count-1]:
+            target_destroys.append(target_filesystem+"@"+snapshot)
+
+if target_destroys:
+    verbose("Destroying old snapshots on target")
+    zfs_destroy(ssh_to=args.ssh_target, filesystems=target_destroys)
+
+
 verbose("All done")