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 enable the File Sharing option (if it isn't already ticked). Now go to the list of shared folders - that's the "i" symbol alongside the File Sharing switch on recent versions of iOS, or just scroll down for the list on older versions. 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). First create a location for the credentials file (if it doesn't already exist):
sudo mkdir /media/Credentials
Then create and edit a credentials file:
sudo nano /media/Credentials/.pimac
The contents of the file are as follows:
username=[your Mac user name]
password=[your Mac password]
"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
...where "pi" is the default username for the Raspberry Pi. If you have a different username, substitute this (you can see it in the prompt, which looks like pi@raspberrypi: or username@raspberrypi:)
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!
Reload stab by rebooting, or execute the following command:
systemctl daemon-reload
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
To create the script file:
sudo nano /usr/local/bin/backup.sh
The contents of my file is as follows:
DEST_FOLDER='/media/pimac'
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.
Improved version - February 2025
Although the above works very nicely, it creates and processes the backup file on the remote Mac, which is wasteful of network bandwidth and hence can run quite slowly. An improvement is to create and compress the backup file on the Pi, before transferring it to the Mac as the last step. This requires the Pi to have sufficient spare space to hold the uncompressed backup file, i.e. the spare space must be at least as large as the /home folder.
To do this, first create a backup folder.
Then modify the backup.sh file to do the following:
- Create the backup file locally
- Compress the file using gzip
- Copy the file to the remote Mac
- Delete the local copy (optional)
Remember to exclude the backup folder itself from the tar command:
DEST_FOLDER='/home/pi/backup'
DEST_FILE='pi4backup.tar'
REMOTE_FOLDER='/media/pimac'
BACKUP_CMD='/bin/tar -rvf'
$BACKUP_CMD $DEST_FOLDER/$DEST_FILE --exclude='$DEST_FOLDER' /home/pi
$BACKUP_CMD $DEST_FOLDER/$DEST_FILE /var/lib/mysql
/bin/gzip -fv $DEST_FOLDER/$DEST_FILE
/bin/cp -v $DEST_FOLDER/$DEST_FILE.gz $REMOTE_FOLDER/$DEST_FILE.gz
/bin/rm -v $DEST_FOLDER/$DEST_FILE.gz
Comments
Post a Comment