Back up datasets to non-ZFS volume

All your general support questions for OpenZFS on OS X.

Re: Back up datasets to non-ZFS volume

Postby Jimbo » Wed Sep 05, 2018 6:44 pm

The other thing that you will probably need to do, is "be root"... sudo or otherwise. Thusly:

Code: Select all
sudo sh -c "zfs send -R -i ${snapshot_previous} ${snapshot_today} | zfs receive -Fduvs ${destination}"

This is actually from a script, hence the "sudo sh -c", if just doing it from a terminal:

Code: Select all
sudo zfs send -R -i ${snapshot_previous} ${snapshot_today} | sudo zfs receive -Fduvs ${destination}

Will suffice.

I'll leave it up to the reader for homework on the various options of send and receive. :)

I believe you can also send a pool without a snapshot, but the source cannot be mounted. It is not something I've found useful enough to investigate further. I find snapshots better as you know exactly what you're reference points are.

Cheers!
Jimbo
 
Posts: 149
Joined: Sun Sep 17, 2017 5:12 am

Re: Back up datasets to non-ZFS volume

Postby lundman » Thu Sep 06, 2018 4:53 pm

If using scripts, you may want to add the path to zfs to sudo without requiring passwords in the sudoers file.
User avatar
lundman
 
Posts: 1335
Joined: Thu Mar 06, 2014 2:05 pm
Location: Tokyo, Japan

Re: Back up datasets to non-ZFS volume

Postby Jimbo » Fri Sep 07, 2018 3:11 am

Certainly a good idea if you want to automate things without a meat-machine being involved! However, if its a manually initiated thing... typing a password once or twice still works. I don't judge (much) and the right answer depends on your level of paranoia. :)
Jimbo
 
Posts: 149
Joined: Sun Sep 17, 2017 5:12 am

Re: Back up datasets to non-ZFS volume

Postby tim.rohrer » Tue Sep 11, 2018 8:36 pm

Jimbo wrote:The other thing that you will probably need to do, is "be root"... sudo or otherwise. Thusly:

Code: Select all
sudo sh -c "zfs send -R -i ${snapshot_previous} ${snapshot_today} | zfs receive -Fduvs ${destination}"

This is actually from a script, hence the "sudo sh -c", if just doing it from a terminal:

Code: Select all
sudo zfs send -R -i ${snapshot_previous} ${snapshot_today} | sudo zfs receive -Fduvs ${destination}

Will suffice.

I'll leave it up to the reader for homework on the various options of send and receive. :)

I believe you can also send a pool without a snapshot, but the source cannot be mounted. It is not something I've found useful enough to investigate further. I find snapshots better as you know exactly what you're reference points are.

Cheers!


Care to share your script? :-)

I was trying to use `zxfer` but it hasn't been updated and so far I don't have it working.
tim.rohrer
 
Posts: 29
Joined: Tue Jul 24, 2018 6:49 pm

Re: Back up datasets to non-ZFS volume

Postby Jimbo » Thu Sep 13, 2018 4:48 pm

Sure...

This can be improved in many ways, but I've not had the impetus to actually do much with it. YMMV. It tries to be "safe" by dropping out if something fails, but this does mean that you will need to "manually recover" depending on where things broke. ;)

This is a script that I run on a macOS host to backup a directly attached pool (Work) to a directly attached backup pool (Tub). Tub is also used for backing up other pools too (same host and others).

Code: Select all
#!/bin/bash
 
pool="Work"
destination="Tub/Work"
prev_file="${HOME}/.backupWorkPreviousSnap"
type=work
 
today=$(date +"${type}-%Y-%m-%d_%H:%M:%S")

# Create today snapshot
snapshot_today="${pool}@${today}"

previous=$(cat ${prev_file})

if [ 0 -ne ${?} ]; then
  # Initial run...
  previous=${today}
  snapshot_previous=${pool}@${previous}
  echo "  No previous snapshot.  Creating first snapshot - \"${snapshot_previous}\"."
  sudo zfs snapshot -r ${snapshot_previous}
  echo "  Performing first sync."
  sudo sh -c "zfs send -R ${snapshot_previous} | zfs receive -Fduvs ${destination}"

  if [ 0 -ne ${?} ]; then
    echo "  Initial backup failed.  Removing snapshot ${snapshot_previous}."
    sudo zfs destroy -r ${snapshot_previous}
    exit 1
  else
    # Record new snapshot as previous backup.
    echo "${previous}" > ${prev_file}
  fi

  exit 0

else
  # Look for yesterday snapshot
  snapshot_previous="${pool}@${previous}"

fi
 
# Look for a snapshot with today's name
if sudo zfs list -H -o name -t snapshot | sort | grep "${snapshot_today}$" > /dev/null; then
  echo "  Snapshot \"${snapshot_today}\" already exists.  Stopping."
  exit 1
else
  echo "  Taking todays snapshot - \"${snapshot_today}\"."
  sudo zfs snapshot -r ${snapshot_today}
fi
 
if sudo zfs list -H -o name -t snapshot | sort | grep "${snapshot_previous}$" > /dev/null; then
  echo "  Previous snapshot \"${snapshot_previous}\" exists.  Proceeding with backup."
 
  sudo sh -c "zfs send -R -i ${snapshot_previous} ${snapshot_today} | zfs receive -Fduvs ${destination}"
 
  if [ 0 -ne ${?} ]; then
    echo "  Backup failed.  Removing snapshot ${snapshot_today}."
    sudo zfs destroy -r ${snapshot_today}
    exit 1
  else
    # Record today's snapshot as previous backup.
    echo "${today}" > ${prev_file}
  fi

  echo "  Backup complete destroying previous snapshot - \"${snapshot_previous}\"."
  sudo zfs destroy -r ${snapshot_previous}
  exit 0

else
  echo "  Missing previous snapshot ${snapshot_previous} aborting."
  exit 1

fi


Something-something about no warranty expressed or implied, this script could eat your homework or kill your favorite pot plant, ... caveat emptor.

Update: This script is intended for attended execution. I should reference Lundman's point here too - without having sudo NOPASS config for the zfs command, you will need to enter your password at least once. If the send/receive takes a while, you will be prompted again at the end for your password (sudo authentication for your session timing out).

Cheers!
Jimbo
 
Posts: 149
Joined: Sun Sep 17, 2017 5:12 am

Previous

Return to General Help

Who is online

Users browsing this forum: No registered users and 27 guests