Set up a Samba backup server with a Raspberry PI

Introduction

This article describes how I set up a Samba backup server using a Raspberry Pi (3).

Just for fun.

Preparing the Raspberry Pi

Installing the Linux Distro

The RPI Imager Tool was very helpful in getting this done:

  • Raspberry Pi, headless image (no desktop).
  • Configure the username.
  • Configure the password.
  • Used /etc/os-release command to confirm I had installed bullseye.

LAN Setup

Set up the RPI LAN like so:

  • At the command line, type: sudo raspi-config.
  • Navigate to System Options > Wireless LAN and configure the SSID and Passphrase for Wi-Fi.
  • Complete the change by selecting OK and exit the config menu.
  • You can use ifconfig to check that the wireless interface is now connected.
  • This would be apparent if the interface for wlan0 has an IP address.
  • That's because the RPI uses DHCP to obtain the IP address from your home router.

Other Setup

This would be a good time to complete some other configurations:

  • At the command line, type: sudo raspi-config.
  • Change the locale to something more relevant to you.
  • Change the time zone to your local zone.
  • Enable SSH if you think you're going to use an application like PuTTY to connect to your RPI.
  • Change the hostname - because it's useful to differentiate your RPI from all the others in the house.
  • Reboot the RPI.

Update the RPI

Take the opportunity to update the RPI before continuing.

  • apt-get update
  • apt-get upgrade
  • Reboot the RPI.

Static IP - NOT

While static IPs can be useful, it's often more manageable to use DHCP with a reserved hostname on your router. This allows dynamic IP assignment while maintaining consistent access via hostname.

Setup the External Drive

Prepare the drive

  • Look for the drive that's connected to your RPI by typing: sudo fdisk -l
  • Look for something like this:
    Device Boot Start End Sectors Size Id Type
    /dev/sda1 2048 1953521663 1953521663 931.5G 7 HPFS/NTFS/exFAT
  • Unmout the drive with the following command: unmount /dev/sda1
  • Erase and format the drive for Linux usage with: sudo parted /dev/sda1
  • The partitioner will respond with: mklabel gpt
  • If prompted to erase the drive, type Y and press Enter. Then run:
    mkpart
    MyExternalDrive
    ext4
    0%
    100%
    quit
  • Then partition the drive with the following:
    sudo mkfs.ext4 /dev/sda1
  • Press Y and Enter when asked if you want to proceed. Then run:
    sudo e2label /dev/sda1 rpiExternalDrive
  • Formatting will take a few minutes, especially for large drives. Be patient.
  • When ready, run this command and reboot the RPI:
    sudo shutdown -r now

Map the drive to a directory

When the RPI powers back up, type the following:

  • Confirm where the drive is: sudo fdisk -l
  • Create a mounting point: sudo mkdir /media/rpiExternalDrive
  • Mount the USB drive at that location: sudo mount /dev/sda1 /media/rpiExternalDrive
  • Confirm that the drive was mounted correctly: mount | grep sda1
  • You should see something like this: /dev/sda1 on /media/rpiExternalDrive type ext4 (rw,relatime)

Create permanent map to the disk

  • Use the information from the mount | grep sda1 command (as in the last step) to confirm the map path and disk format.
  • Edit the /etc/fstab file and add the following
    /dev/sda1 /media/rpiExternalDrive ext4 defaults 0 0
  • Save and exit your editor and then reboot the system.
  • After the RPI powers up, you can run the mount command to confirm that the mapping survived the reboot.

Set permissions to the drive

Type the following commands to set up permissions to the drive:

  • sudo chown -R username /media/rpiExternalDrive/
  • sudo chown -R root:users /media/rpiExternalDrive/
  • sudo chmod -R ug=rwx,o=rw /media/rpiExternalDrive/

And your drive should be ready.

Install SMB

SMB stands for Server Message Block, a network protocol that allows users to share files, printers, and other resources on a network. It allows users to read, create, and update files on remote servers. It also allows applications to request services from server programs in a computer network

Type the following commands to get going with the installation:

  • sudo apt update
  • sudo apt upgrade
  • sudo apt install samba samba-common

Edit the configuration file /etc/samba/smb.conf

Then, in text editor scroll to the bottom of the document and look for something like this. If it doesn't exist, then just add the text:

[UsernameBackups]
path = /media/rpiExternalDrive/
browsable = yes
writeable = yes
create mask = 0777
directory mask = 0777
public = no
read only = no


Then save and exit.

Configure the User Account

  • Create a password for Samba so you can see your share from other machines.
    sudo smbpasswd -a username
  • And follow the prompts to define the password for the user, which could be different from that of the RPI (recommended)
  • Restart Samba with:
    sudo systemctl restart smbd

Setup Backup and Restore on Windows

Simple access from Windows 11 computers

  1. On your Windows computer, open a File Explorer window.
  2. Type \\raspberrypi\UsernameBackups in the address bar.
    Note that this is the same UsernameBackups that you have in the Samba config file.
  3. When you press Enter, you should be able to enter your Samba username and password, and see your shared drive.

Map a network drive

  1. On your Windows computer, open a File Explorer window.
  2. On the left hand panel, left-click on the Network option.
  3. In the path field at the top, type \\ followed by the hostname of the server, followed by \ and the folder name, and press Enter.
  4. Log onto the server (if asked).
  5. In the right hand panel, right-click on your server (e.g. \\hostname\newExternalDrive).
  6. From the pop-up menu, select Map Network Drive.
  7. Pick a drive letter, and you're done!