I’m going to save you a lot of time.
I researched this topic because it seemed like a cool project, and I sunk hours into configuring ownCloud (and writing a 3,000 word tutorial about it) until I realized…
I don’t need it! And you almost certainly don’t need it either.
If you want to create a private cloud server with your Pi, what is it you truly want?
You want to transfer files from your computer to private storage, and vice-versa, over an internet connection. This means you can backup your computer any time and access your “cloud storage” from anywhere.
The truth is, you only need a platform like ownCloud if you need to give other non-technical users access to your cloud storage. If your private cloud server is only for your personal use, there’s no need for a server at all.
The solution I came up with is incredibly simple and flexible. It’s pretty “bare bones” but I think a lot of people will enjoy it, and it can easily be expanded into a much more complex system.
How it works
Before we dive into the meat of the tutorial, here’s how it will work.
You’re going to use an external hard drive as your storage device. You’ll keep it plugged into your Pi at all times which is connected to the internet via Wifi or ethernet cable.
In practice, you’ll be able to run a single command to backup your computer to your HDD.
And it keeps getting better.
The benefits of this remote storage system
With this backup solution, you get all the benefits of using a HDD and a cloud service.
#1 Total privacy
You don’t have to worry about any companies spying on your data because it’s stored securely on a HDD in your home.
#2 The full convenience of the cloud
Cloud storage is great because you can access it from anywhere and you don’t have to get your HDD out of a drawer and plug it in. This solution also lets you access your files from anywhere over the internet.
#3 It’s much cheaper
Dropbox is $11.99/month for 2TB. You can buy a 2TB HDD on Amazon for $55. You’ll save $89 your first year and then $144/year after that.
#4 Fast transfer speeds
Cloud services like Dropbox only work over the internet, so large data transfers can take hours, even days to complete.
With this solution, any time you have a large transfer you can just plug the HDD into your computer and use the super fast USB 3.0 connection. You’re not tied down to cloud-only connections.
#5 The backup behavior is totally customizable
Because rysnc is such an incredibly flexible tool, you can customize many aspects of the syncing behavior like deleting files from the HDD that you’ve removed from your computer.
#6 You can schedule your backups at whatever interval you want
There many ways to schedule simple commands like this giving you total control over when and how often you run your backups.
#7 You can create your own data security plan
Are you worried you’ll lose both your laptop and HDD? You can buy a second HDD and swap them out occassionally keeping one stored outside your home. Or, get a second Pi and simultaneously backup to two “cloud storage” HDDs in different locations. There’s no limit to how advanced you can get with your data security.
#8 Encrypt everything
You can encrypt all the files on your computer and HDD, and also require a password to access files on the HDD even when using a USB connection.
Overall, it’s a killer solution for backing up your computer and getting remote access to your files. It’s more work than simply using Dropbox, but let’s be honest, this stuff is fun.
Now let’s get started with mounting the HDD.
Mount your HDD
The first step is to mount the hard drive, and the Pi makes this really easy.
When you connect your HDD to any of the USB ports, the Pi will automatically mount it for you. You’ll see a notice right away letting you know a removable medium has been inserted.
By default, the HDD will be mounted at /media/pi/HDD/.
If you want to mount the HDD to a different directory, you can follow the steps in the official documentation.
With the HDD mounted, you’re already prepared to backup your first files.
Please be careful! The
rysnc command is powerful and it’s totally possible to delete your files by accident. I highly recommend creating a “test” directory to practice with. Use test data every time you experiment with new code.
How to transfer files to your HDD
As mentioned already, we’ll be using the rsync command to transfer files to and from the HDD. All of these commands can be run locally from your machine.
The rsync function uses the following syntax:
rsync options source destination
Skipping the options for now, here’s an outline of how you would transfer a folder on your computer to the HDD mounted to the Pi.
rsync /Your/File/Path pi@YOUR.IP.ADDRESS:/media/pi/HDD
Besides updating the file paths, you’ll need to get your Pi’s IP address. You can do that by running the following command on your Pi:
Make sure to run that command from your Pi (not on your computer) either directly or via remote SSH.
There are a few options you should add as well to make it run faster and provide you with more useful feedback.
-a flag tells rsync to copy the files recursively which means it will copy not just the files in the directory, but also any directories and their files found within the directory. It also maintains file permissions and ownership making it a good option to include in all your rsync commands.
-v flag stands for “verbose” and will have rsync list all of the files transferred as well as how long each file took to transfer. This is good to include at first so you can make sure it’s working as expected.
-z flag tells rsync to compress the data transferred which helps improve performance. Note that this doesn’t mean the files end up in a compressed state on the other device.
-h flag tells rsync to make the output “human readable” which converts the bytes in the output message into Kb and Mb.
In addition, I’d recommend adding the
--progress flag the first few times you try out rsync. If you transfer any larger files, you think the command has frozen. You’ll get peace of mind and an idea of how fast transfers process by including
The finished command
With all of the options included, here’s what the finished command will look like:
rsync -avzh --progress /Your/File/Path pi@YOUR.IP.ADDRESS:/media/pi/HDD
Once you enter the command, you’ll enter your Pi’s password, and then the transfer will take place.
I was kind of amazed how well this worked on my first few tries. It’s an excellent way to transfer files quickly between devices. Since rsync checks the files for differences first, it will only transfer modified files which makes day-to-day maintenance after the initial backup quite fast.
Keep an exact replica of your backup directory
If you delete a file in your backup folder on your computer, it will still be kept on your HDD. If you’d like the HDD to maintain itself as an exact replica of your local directory, you can use the –delete flag to have rsync delete files on the HDD that no longer exist in the local directory.
rsync -avzh --delete /Your/File/Path pi@YOUR.IP.ADDRESS:/media/pi/HDD
Just be careful with this option when transferring files from the HDD as it will delete files on your computer.
How to transfer files from your HDD to your computer
Reversing the process to get files from your HDD is just as simple.
But before we get to the command, it’s important to understand exactly how the trailing slash works with rsync because it will likely effect how you write your reversed command.
This file path omits the trailing slash from the source and will transfer the contents of the “vacation” directory including the “vacation” directory itself into the “pics” directory.
rsync options /pics/vacation /HDD/pics
When you add the trailing slash, rsync will instead copy only contents of the “vacation” directory and not the directory itself into the “pics” directory.
rsync options /pics/vacation/ /HDD/pics
So, going with the same example code before, you would reverse the source and destination, and add a “/” after “HDD” so that the “HDD” folder itself isn’t copied over.
rsync -avzh --progress pi@YOUR.IP.ADDRESS:/media/pi/HDD/ /Your/File/Path
Now that you know how to move files to and from your HDD, you’re probably wondering how to schedule this process to work automatically for you.
This is where things get a bit complicated.
Can I schedule the backups?
The short answer: yes.
The long answer is, well, very long and specific to your platform. My needs are basic so I opted for a much simpler semi-automated solution which I’ll outline soon.
Since I haven’t used a fully automated system myself, I’m providing my reseach notes and some tips here instead.
You don’t want to schedule backups from your Pi
While the Raspberry Pi has built-in cron scheduling, you don’t want to schedule the backup from your Pi. Instead, you should schedule the task from your computer. There are two reasons for this.
First, you probably want to run your backup overnight, but if your computer is off or asleep, the cron job will fail.
Second, your computer will ask for a password when connecting via SSH to run the rsync command. You can generate an SSH key pair to allow auto-logins with ssh-keygen. However, I would not be comfortable generating a key pair where the Pi is the client with the private key and can get complete access to my laptop where I keep all my very important stuff.
On the contrary, I don’t mind generating a private key on my laptop to allow auto-logins to my Pi.
To generate the SSH keys so you can auto-login to your Pi without a password, I’d recommend following the steps on the official RPI site. You might want to do this for the convenience even if you don’t schedule your backups.
Now, to the matter of the sleeping laptop.
Scheduling when your computer is off
As mentioned already, a cron job run from the Pi when your computer is asleep will fail. Likewise, a cron job scheduled to run on your computer while it’s asleep will also fail.
Cron expects 100% uptime and simply isn’t the right tool for this task. There are better alternatives, but it depends on which OS you’re running.
You can probably come up with even more alternatives, but these stuck out to me as the best options.
Mac – cron has been deprecated in favor of a relatively new tool called launchd (here’s a good tutorial and more info here). It’s more complex which is a pain, but it’s extremely customizable. Unlike cron, tasks scheduled while your computer sleeps will run once it wakes.
Windows – the Task Scheduler added in Windows 10 will work well (detailed tutorial). Tasks scheduled to run when the system is turned off can start immediately upon wake.
Linux – Linux users will feel at home with anacron. Unlike cron, it was designed to work with desktops and laptops, and it will run missed tasks once your computer is powered on.
You can get creative here too. Depending on the tool used, you can schedule multiple backups per day and use triggers other than the time of day to launch them.
As a Mac user, I took a good look at launchd and thought, “Yea, I’ll do that later.” Instead I’m using a ridiculously simple solution that I’m calling semi-automated.
A simple semi-automed solution
If you don’t want to go through the hassle required to schedule your backups, it’s quick enough to just run the terminal command yourself. If it’s not critical to backup your new file changes every day then forgetting a day here and there won’t matter.
The only annoying part is that the command is so long and specific, you’ll want to save it somewhere instead of typing it from memory, and let’s be honest, copying & pasting it every day feels downright ugly.
A much nicer approach is to create an alias.
An alias is shorthand for a custom command. In this case, you can create an alias named “backup” to run the whole backup command.
First, open a fresh terminal window so that you’re in the home directory. Then run this command to open the .zhsrc file with the Nano editor
The file is likely empty, so you’ll be entering the alias into line 1. The syntax is as follows:
alias shorthand="full command here"
Here’s the completed version for an alias named “backup.”
alias backup="rsync -avzh --progress /Your/File/Path pi@YOUR.IP.ADDRESS:/media/pi/HDD"
Swap out the file path and IP address and you’re done. Hit ctrl+o to write out the file and then ctrl+x to close the Nano editor. Then restart the terminal and give it a try.
Now all you have to type into the terminal is “backup” and your computer will deliver any modified files to your hard drive. You can edit this command whenever you want if the folders or IP address change.
While schedule backups sound nice, it’s pretty darn easy to pop open the terminal and enter “backup” into it. Someday I’ll automate my backups, but for now this works for me.
At this point, you’ve got your remote storage working well, but this tutorial hasn’t fully lived up to its promise. There’s still no “cloud” to this cloud storage!
Here’s how you can run your new backup command over the internet.
How to backup and download files over the internet
You’ve been able to use your Raspberry Pi’s IP address so far because you’ve been on the same network. This doesn’t work from a remote network.
Creating and configuring your account is straightforward, but you can follow the steps in this tutorial to learn how to use Remote.it.
Once you’ve registered your Raspberry Pi in your Remote.it account, you can SSH into your Pi easily from remote networks. There’s a Connect button in the dashboard where you can select SSH to enable a new SSH connection from your computer.
Remote.it will give you a pre-written SSH command to connect to your Pi through their servers that looks like this:
ssh -l pi proxy22.rt3.io -p 31726
Your existing rsync command only needs a few adjustments to work with Remote.it.
Add “e” as an option which lets rsync know there will be an additional command. Then following “e,” include that command in quotes. In this case, the additional command is ssh and it includes a port number specified by Remote.it.
rsync -avzhe 'ssh -p 31726' /Your/File/Path email@example.com:/media/pi/HDD
The only downside here is that Remote.it doesn’t have persistent IPs which ruins our chance at making a
backup-remote alias. You’ll have to customize this command with a new port and IP each time you use it.
By the way, if you’re interested in getting a static IP address for accessing your Pi over the internet, I’ve seen people have success with both No-IP and ngrok. Let me know in the comments how they work for you if you try either one out.
Your own cloud storage
This cloud storage system is so simple thanks to rsync and the Raspberry Pi.
With the Pi, you’re basically adding internet access to the HDD so you can access it from anywhere. Using scheduling tools or an alias, running a backup is quick and easy.
Even better, you always have the option of plugging in if you need to complete a large data transfer.
And don’t forget, while we used the terminal exclusively in this tutorial, you can always get a remote desktop from VNC and browse your “cloud storage” using a GUI just like Google Drive or Dropbox.
I hope you learned a thing or two reading my method of creating a private cloud server with a Raspberry Pi.
If you’ve got questions or tips to share, drop a comment below.