btrfs snapshots for send and receive

I’m using MythTV for watching TV and videos. For that I have a separate HP micro server which 4 HDs. That system is quiet old and low-powered. Because of that I rip my DVDs on my new PC and copy over the files.

Previously I’ve used rsync to synchronize the films over to the MythTV system. Last week I made a mistake and destroyed several files. I only noticed my mistake after I had synchronized the files, so my “backup” was gone as well.

Uups.

Therefore I switched both systems to use btrfs which allows to create snapshots.

Common

SRC='/srv/misc/Media'
DST='/srv/media/'
CAT='Video'  # Music
REMOTE='....'
TODAY="$(date +%Y%m%d)"
LAST="$(cd "$SRC" && ls -1dt "$CAT".???????? | head -n1)"

Initial setup

  1. Create a new snapshot on the source:

     sudo -s btrfs sub snap -r "$SRC/$CAT" "$SRC/$CAT.$TODAY"
    
  2. Send over the initial snapshot:

     sudo -s btrfs send "$SRC/$CAT.$TODAY" |
         ssh -4 "root@$REMOTE" btrfs receive "$DST"
    
  3. Verify the volume is correctly received on the remote system:

     ssh -4 "root@$REMOTE" btrfs sub list -R "$DST"
    
  4. Create a read-only snapshot on the destination:

     ssh -4 "root@$REMOTE" btrfs sub snap -r "$DST/$CAT.$TODAY" "$DST/$CAT"
    

Update

  1. Create a new snapshot on the source:

     sudo -s btrfs sub snap -r "$SRC/$CAT" "$SRC/$CAT.$TODAY"
    
  2. Send over the new snapshot:

     sudo -s btrfs send -p "$SRC/$LAST" "$SRC/$CAT.$TODAY" |
         ssh -4 "root@$REMOTE" btrfs receive "$DST"
    
  3. Verify the volume is correctly received on the remote system:

     ssh -4 "root@$REMOTE" btrfs sub list -R "$DST"
    
  4. Update the read-only snapshot on the destination:

     ssh -4 "root@$REMOTE" btrfs sub delete "$DST/$CAT"
     ssh -4 "root@$REMOTE" btrfs sub snap -r "$DST/$CAT.$TODAY" "$DST/$CAT"
    
  5. Remove the previous snapshot:

     sudo -s btrfs sub delete "$SRC/$LAST"
     ssh -4 "root@$REMOTE" btrfs sub delete "$DST/$LAST"
    
Written on February 26, 2020