Raspberry PI as a Backup Server (Like Apple's Time Machine using Rsync)

Started by branx86, December 03, 2015, 05:10:34 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

branx86



One (at minimum) USB external hard drive for simple network backups and file serving



Once you have gathered up the hardware, followed along with the Getting Started with Raspberry Pi tutorial to get up to speed (and are running Raspian) it's time to start setting up your Pi as a NAS.
Once you're at the command line the first thing you need to do is to add in support to Rasbian for NTFS-formatted disks. To do so type the following command: sudo apt-get install ntfs-3g 

Then do: sudo fdisk -l


The first disk /dev/mmcb1k0 is the SD card inside the Raspberry Pi that houses our installation of Raspbian. We're going to leave that one completely alone.
The second disk, /dev/sda is our first 1TB external hard drive. Make a note of the hard drive names.

Before we can mount the drives, we need to create a directory to mount the drives to. For the sake of simplicity we're going to simply make directory called USBHDD1 and USBHDD2 for each drive. First we have to make the drives. At the command line enter the following commands:  sudo mkdir /media/USBHDD1
After you've created the directory, it's time to mount the external drive to each location.
sudo mount -t auto /dev/sda1 /media/USBHDD1

Time to install Samba so we can access the storage from elsewhere on the network. At the command line enter: sudo apt-get install samba samba-common-bin

Make a backup copy of the Samba configuration file in case we need to revert to it. At the command line, type the following command line: sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.old

Do some basic editing in the Samba config file. Type the following at the command line:  sudo nano /etc/samba/smb.conf
You should see something like the following in your terminal window:


The workgroup identifier, by default workgroup = WORKGROUP. If you're using a different name for your home workgroup.
Turn on user authentication for our samba storage, otherwise anyone with general access to our network (like guest Wi-Fi users) will be able to walk right in. Scroll down in the Samba config file until you get to the section that reads:


Remove the # symbol from the security = user line (by highlighting it with the cursor and pressing delete) to enable username/password verification for the Samba shares.

add an entirely new section to the configuration file. Scroll all the way down to the very bottom of the file and enter the following text:
[Backup]
comment = Backup Folder
path = /media/USBHDD1/shares
valid users = @users
force group = users
create mask = 0660
directory mask = 0771
read only = no


Note: Whatever you put in the brackets in the top line is going to be the name of the folder as it appears on the network share. If you want another name other than "Backup" now is the time to edit it.

Press CTRL+X to exit, press Y when asked if you want to keep changes and overwrite the existing configuration file.
Then do: sudo /etc/init.d/samba restart

Add in a user that can access the Pi's samba shares.  You can make your username and password whatever you wish. To do so type the following commands:
sudo useradd backups -m -G users
sudo passwd backups



To make a legitimate Samba user. Enter the following command:  sudo smbpasswd -a backups
Now go to your windows computer and put in the Raspberry Pi address:

When prompted, enter the credentials you created in the previous steps.

Configure our Pi so that when it restarts it will automatically mount the external hard drives. To do so we need to fire up the nano editor and make a quick edit. At the command line type:  sudo nano /etc/fstab

Add a few quick entries. Within the nano editor add the following lines:   /dev/sda1 /media/USBHDD1 auto noatime 0 0

So far our Raspberry Pi NAS is hooked up to the network.
sudo apt-get install rsync

Once rsync is installed, it's time to set up a cron job to automate the process of copying files from the USBHDD1
At the command line enter the following command:  crontab -e

The command will open up your cron scheduling table in the nano text editor which should be rather familiar to you at this point in the tutorial.  Go ahead and scroll down to the bottom of the document and enter the following line:   

0 5 * * * rsync -av --delete /media/USBHDD1/shares /media/OtherHDD/shares/


This command specifies that every day at 5:00AM (the 0 5 part), every single day (* * *, wild cards in the year, month, day spots), we want rsync to compare the two directories, copying everything from HDD1 to HDD2 and deleting anything in the backup directory that no longer matches something in the primary directory—i.e. if we have a movie file on HDD1 we delete, we also want that file to be removed from the backup on the next synchronization.

If you wish to run the rsync immediately to get the data mirrored faster and make the initial cron job a little lighter on the system, go ahead and enter the same rsync command you put into the crontab at the command line like so:   rsync -av --delete /media/USBHDD1/shares /media/OtherHDD/shares/

That's it!