How to Set Up a Local Backup System Using Ubuntu on Raspberry Pi with a portable disk

In this guide, we’ll walk through the process of setting up a local backup system on a Raspberry Pi running Ubuntu, using an external NTFS-formatted drive. The backup system will be accessible via Samba from devices on your local network, ensuring that you can back up files securely across your devices.

Prerequisites

  • Raspberry Pi running Ubuntu (we are not using Raspberry Pi OS here)
  • External hard drive (NTFS formatted for compatibility across Windows and Linux)
  • Samba for file sharing
  • Devices on the same local network (such as Windows PC, Surface, or mobile phones)

Step 1: Install and Set Up Ubuntu on the Raspberry Pi

If you haven’t already, install Ubuntu headless on your Raspberry Pi. Ensure it’s up-to-date:

sudo apt update
sudo apt upgrade -y
sudo reboot

Step 2: Connect the External Drive

Plug your external hard drive into the Raspberry Pi. For compatibility with both Windows and Linux, we’ll use the NTFS file system.

Step 2.1: Identify the Drive

Check the device name for your external drive using lsblk:

lsblk

The external drive will likely show up as /dev/sda (or similar). If not, check if the power supply and the power cable of your Pi is compatible for 5V 3A. A portable hard drive will drain a lot of current from the PI. If it’s already mounted, unmount it:

sudo umount /dev/sda1

Step 2.2: Mount the External Drive

Create a directory for the mount point:

sudo mkdir -p /mnt/backup

Mount the external drive to this directory:

sudo mount /dev/sda1 /mnt/backup

To make the mount permanent across reboots, add an entry to /etc/fstab. First, get the UUID of the drive:

sudo blkid /dev/sda1

Then, edit /etc/fstab and add the following (replace your-uuid with the actual UUID from the blkid command):

sudo nano /etc/fstab

Add the line for the NTFS drive:

UUID=your-uuid /mnt/backup ntfs defaults 0 0

Step 2.3: Test the Mount

Reload the fstab configuration:

sudo mount -a

Now, your drive should be permanently mounted at /mnt/backup.


Step 3: Install and Configure Samba for File Sharing

  1. Install Samba:
sudo apt install samba samba-common-bin -y
  1. Create a Samba User:

For accessing the backup folders, create a user in Samba. This user will be required to authenticate when accessing shared files.

sudo adduser backupuser
sudo smbpasswd -a backupuser
sudo smbpasswd -e backupuser

Follow the prompts to set the password for backupuser.

  1. Configure Samba Shares:

Edit the Samba configuration file:

sudo nano /etc/samba/smb.conf

Scroll to the bottom and add the following configuration for your shared folders. We will set up a backup share at /mnt/backup.

[Backup]
path = /mnt/backup
valid users = backupuser
browseable = yes
read only = no
guest ok = no
create mask = 0775
directory mask = 0775
  1. Restart Samba:
sudo systemctl restart smbd
  1. Allow Samba Through the Firewall:

If you are using ufw (uncomplicated firewall), allow Samba to pass through:

sudo ufw allow samba

Now, your Raspberry Pi’s /mnt/backup folder should be accessible from any device on your local network.


Step 4: Access the Shared Folder on Windows

On your Windows PC:

  1. Open File Explorer.
  2. In the address bar, type the Raspberry Pi’s IP address followed by the share name: <Raspberry_Pi_IP>\Backup
  3. When prompted, enter the username (backupuser) and password you created earlier.

Your Raspberry Pi’s external hard drive should now be accessible from Windows or any device on the local network.


Step 5: Set Up Automated Backups

If you want to automatically back up files from your devices (such as your Surface or mobile devices), we can create cron jobs or use rsync to automate the process.

Step 5.1: Automate the Backup with Cron

To run this backup daily, edit the cron jobs for the Pi:

crontab -e

Add the following line to schedule the backup every day at 2 AM:

0 2 * * * /home/pi/backup.sh

This will automatically back up the specified folder to the shared backup directory every day at 2 AM.


Step 6: Set Up Folder Permissions

To ensure secure access and write permissions to the backup directory, adjust the ownership and permissions:

  1. Set Ownership:
sudo chown -R root:backupuser /mnt/backup
  1. Set Permissions:
sudo chmod -R 775 /mnt/backup

This ensures that only backupuser (and root) has full access to the directory. It is also possible to set up multiple users with their own passwords, so that family memebers can access their own files in the backup system, without easily access to others files.


Step 7: Verifying the Setup

  • Access from Windows: After setting up the share, ensure you can browse the /mnt/backup directory and write to it from Windows.
  • Permissions: If you’re unable to access the folder or get denied, make sure the Samba username/password is correct and that the permissions are set properly.
  • Cron Jobs: To verify automated backups, check if the backup script is running as expected by looking at the timestamp.

Troubleshooting

  • Slow Transfer Speed: If you experience slow transfers:
    • Ensure your Raspberry Pi is wired (via Ethernet) for better network performance.
    • Use NFS instead of Samba if speed is an issue.
    • Check if any software or processes are using up network bandwidth.
  • Samba Access Denied: If you’re denied access to the shared folder:
    • Double-check the Samba configuration (valid users and create mask).
    • Ensure you are using the correct credentials (the backupuser password).

Conclusion

You now have a fully functional local backup system on your Raspberry Pi running Ubuntu with an external NTFS-formatted hard drive. This setup enables you to back up data from your devices to the Raspberry Pi over your local network, using Samba for easy access from Windows and Linux systems. Plus, you can automate the backups using cron.

The only limitation I experienced with such a system is the low transfer speed. I only reached 4Mb/s for transfering files from my android phone and surface pro laptop to the Pi backup server. I was not able to increase it further. I think it is limited by the wifi speed.

Let me know if you have any questions, and happy backing up!

Leave a comment

Your email address will not be published. Required fields are marked *