Backing up a Raspberry Pi to a Mac

I try to keep most of my amateur radio applications on a Raspberry Pi, although some are on Mac and some on Windows. For general use I have a Mac that is always on, and this machine is backed up on-site using Time Machine and off-site using Backblaze. So in order to keep my Pi backed up, the simplest method is to synchronise it periodically to the Mac which in turn will be included in the automatic on-site and off-site backups.

Should be simple, but in practice I have found that Mac and Pi are tricky to configure over an SMB (Samba) network. Too many versions and options! Hence this blog which documents what works for me.

There are two steps:

1. Configure a shared directory on the Mac, and configure the Pi to mount it automatically on its desktop.

2. Set a backup routine on the Pi, with the destination as the shared directory.

It is assumed that the two machines are on the same LAN.

1. Configuring a shared folder

On the Mac, create a new folder that will be shared with the Pi. In my case I did this under my home folder, and I called it "pimac".

Go to System Preferences / Sharing, and tick the File Sharing option (if it isn't already ticked). Now press the "+" button under the list of shared folders, and add your new shared folder - "pimac" in my example. Click the "Options..." button, check that SMB is enabled, and that your user name has a tick next to it.

Find the Mac's IP address. You can do this in System Preferences / Network. In my case the IP address is 192.168.0.5.

If you haven't already done this, go to your router's settings and make sure that the Mac's IP address is fixed. You don't want it to change every time your Mac and/or router are rebooted. The method of doing this varies between routers, but it is normally to be found in LAN settings / Address Reservation or similar. 

On the Pi, create a credentials file called ".pimac" (you can call it something different if you like). The command to do this is:

sudo nano /media/Credentials/.pimac

The contents of the file are as follows:

username=[your Mac user name]
password=[your Mac password]
domain=WORKGROUP

"WORKGROUP" is the default name for an SMB domain, and only needs to be changed if you have explicitly named your domain to something else.

Note: For better security, you could create a separate user account and password on the Mac.

Create a mount point and set permissions for the shared folder. You can of course choose a different name and location for the folder:

sudo mkdir /media/pimac
sudo chown -R pi:pi /media/pimac

Now edit the /etc/fstab file. The command to do this is:

    sudo nano /etc/fstab

Add the following line to the end of the file, as a single line:

//192.168.0.5/pimac /media/pimac cifs rw,credentials=/media/Credentials/.pimac,uid=996,gid=996,file_mode=0660,dir_mode=0770,iocharset=utf8,vers=1.0,noperm 0 0

Replace the IP address with that of your Mac, and make any other changes if you have used different names for the various folders and files. The options in this line are very important!

Now execute the following command, and a new folder called "pimac" should appear on the desktop:

sudo mount -a

If successful, reboot the Pi, and the new folder should remain on the desktop.

The shared folder can be opened from the Pi or from the Mac.

2. Backing up to the shared folder

I based my script on the instructions to be found here: https://raspberrytips.com/backup-raspberry-pi/

To create the script file:

sudo nano /usr/local/bin/backup.sh

The contents of my file is as follows:

#!/bin/bash

DEST_FOLDER='/media/pimac'
DEST_FILE='backup.tar'
BACKUP_CMD='/bin/tar -rvf'

/bin/rm $DEST_FOLDER/$DEST_FILE.gz
$BACKUP_CMD $DEST_FOLDER/$DEST_FILE /home/pi
/bin/gzip $DEST_FOLDER/$DEST_FILE

Note that I am only backing up my home folder (/home/pi), but you can add other directories to back up if you wish, by adding additional $BACKUP_CMD lines.

Because my Mac is itself securely backed up with a full history (using Time Machine and also Backblaze), I do not need to create multiple snapshots. So each time the backup is run, it deletes the old file on the Mac and creates a new one. If you need to create multiple snapshots, the instructions in RaspberryTips explain how.

Set the permissions of the script file using:

sudo chmod +x /usr/local/bin/backup.sh

Finally, to run the backup automatically, add a line to crontab. Edit the crontab using:

   crontab -e

...and add a line something like this:

0 12 * * * /usr/local/bin/backup.sh

I have chosen to run the script at midday (12). My pi isn't always switched on, but if I have been using it I make sure I leave it on over lunchtime.








Comments

Popular posts from this blog

Building a basic codeplug for DMR (DMR Part 2)

Adding Brandmeister to the Hotspot (DMR Part 4)

D-STAR, Pi-Star and IC-705