In this tutorial I will show you how to synchronize any files with your Raspberry PI using OneDrive as the example. For this project I am just going to synchronize my Documents folder from OneDrive. You can also use this for many other types of drives eg. Google Drive or Dropbox and the process is very similar. Just choose a different number in step 6.
- First open a Terminal window sand enter curl -L https://raw.github.com/pageauc/rclone4pi/master/rclone-install.sh | bash
- Once this has finished you are ready to configure rclone
- Enter the command rclone config
- Enter n (for a new connection) and then press enter
- Enter a name for the connection (i’ll enter onedrive) and press enter
- Enter 25 for OneDrive and press enter (Yes the numbers have changed since the screenshot below. Please check carefully)
- Press Enter for client ID
- Press Enter for Client Secret
- Press n and enter for edit advanced config
- Enter y for auto config (if you are on the Pi Directly) or n if you are logged in remotely through SSH and then press enter
- Only follow if your Pi doesn’t have a Desktop installed (Headless) – you will need to install rclone on a machine that has a desktop and web browser. (For windows 10 see the section at the bottom)
- Then run the command rclone authorize onedrive
- You then login, when you get success paste the result from the terminal window into your remote terminal. Then skip to step 13.
- A browser window will now open, log in with your Microsoft Account and select yes to allow OneDrive access.
- You will then see “success” and you can close the web browser
- Now choose 1 for OneDrive Personal or Business
- Now select the OneDrive you would like to use, you will probably only have one OneDrive linked to your account. This will be 0
- Now select y for yes
- Now select y for yes again to save the connection
- Close the terminal window
- Using a web browser or phone app, In your OneDrive create a folder to sync; i’ll call mine Documents
- Now we have set up the connection, however we still have to synchronize the folder with the raspberry PI. To do this we will write a simple .sh script and set it to run at a set time.
- First open a terminal and test your sync command. rclone sync -v onedrive:Documents /home/pi/Documents/Download This command will sync the “onedrive” connection “Documents” folder to your raspberry pi. The contents will be downloaded to you documents/Download (you may need to create this folder) folder on the pi. If successful you will see the terminal start downloading each file to the folder.
- Now let’s make the script. For this method you need to login to the pi directly, or use nano in the terminal to create the script file. If you’re comfortable with a text editor you can enter this string into the text editor on the raspberry pi and save it as sync.sh rclone sync -v onedrive:Documents /home/pi/Documents/Download
- If you have used different names to me, replace onedrive with your connection name and replace Documents with your onedrive folder name. You can also change the path, this is where the folder is synced to.
- Put the file in the /home/pi/Documents on your Raspberry Pi
- Now make the file executable, in the terminal enter chmod +x /home/pi/Documents/sync.sh
- This file will call the connection and download any new files from OneDrive.
- Now let’s make the script automatic. Open a terminal window and run the command crontab -e
- Select 2 for nano
- Enter the following string – 0 * * * * /home/pi/Documents/sync.sh
- Save the file (usually Ctrl + O to save and Ctrl + X to exit)
- That’s it! Restart your Raspberry Pi and add files to your OneDrive
I use this to synchronize photos to my raspberry pi Photo frame project.
UPDATE: To Upload files to Onedrive
If you would like to Upload files to OneDrive from your Raspberry PI, simply swap the source and destination.
rclone sync -v /home/pi/Documents/download onedrive:Documents/Download
If you would like to upload and download, I would suggest using 2 different folders. Then in step 21 add both commands to the text file.
rclone sync -v /home/pi/Documents/upload onedrive:Documents/upload
rclone sync -v onedrive:Document/download /home/pi/Documents/download
UPDATE: To use a folder with spaces
If you would like to use a folder name with spaces, simply put Quotation marks around the path.
rclone sync -v /home/pi/Documents/download “onedrive:My Documents”
UPDATE: To run rclone on windows 10 for step 10
- You can run rclone on windows 10 by downloading it from the rclone website https://rclone.org/downloads/ (Intel/AMD – 64 Bit).
- Extract the files and then open CMD.
- Navigate to that folder using the cd command (mine is cd C:\Users\Jarrod\Downloads\rclone-v1.53.3-windows-amd64\rclone-v1.53.3-windows-amd64). This is the folder that contains the rclone.exe file
- From here you can run the rclone authorize onedrive command. A web browser will appear and you can sign in to authorise. Then copy the key from that CMD to your headless raspberry pi to authorise.
Hi Jarrod,
I tried to follow this to the letter with my Onedrive account.
I got to point 17 with no problems.
Point 18 – I already has a folder in my Onedrive created that I wanted to use (Documents)
Point 19 – Nothing to do here.
Point 20: rclone sync -v onedrive:Documents /home/pi/Documents
This gave the following error:
“2019/06/18 12:58:08 Failed to create file system for “onedrive:Documents”: didn’t find section in config file”
Any ideas what I didnt do, or whats wrong?
Thanks
Jon
Hey Jon,
so onedrive should be the name for your connection that you chose in step 5 “rclone sync -v onedrive:Documents /home/pi/Documents” so “”rclone sync -v (your drive name):Documents /home/pi/Documents””
Will this just download files or also upload new ones from the pi?
This will just download but you can change the line of code to do both. You can check the command on the rclone site.
The best way would probably be to have two commands as there isn’t one that does both. For example using sync and copy to both download and upload files respectively.
rclone sync -v onedrive:Documents /home/pi/Documents
rclone copy onedrive:Documents /home/pi/Documents
Not sure this works as a 2-way sync. Help says rclone sync deletes files in the destination if necessary to make destination match source, and it certainly seems to do this for me (fortunately I had created a Sync directory on pi, not used Documents). Doing a dry run -n gives a message
NOTICE: [file]: not deleted as –dry-run
This means there are no files for the rclone copy command to copy from the pi directory to OneDrive. I think Putting copy first just means nothing gets deleted
Is there a flag to do a non-destructive sync (or better still, a ‘proper’ 2-way sync?
Hey Luke,
Yes you’re right sync would just delete the files.
Rclone doesn’t have a true 2 way sync. My use case has always been a 1 way sync.
If you need to upload files to OneDrive and download files from OneDrive, the best option I can see would be to create two different folders in documents and OndeDrive (Uploads and downloads) and use a separate sync or copy command for each folder.
getting:
pi@raspberrypi:~/OneDrive $ sudo apt-get install gnome-schedule
Reading package lists… Done
Building dependency tree
Reading state information… Done
E: Unable to locate package gnome-schedule
It may be because you are in the /OneDrive folder. Have you tried doing it from a fresh terminal? Also try running “sudo apt update” before the command
Nope.
pi@raspberrypi:~ $ sudo apt update
Hit:1 http://linux.teamviewer.com/deb stable InRelease
Hit:2 http://archive.raspberrypi.org/debian buster InRelease
Hit:3 http://raspbian.raspberrypi.org/raspbian buster InRelease
Reading package lists… Done
Building dependency tree
Reading state information… Done
All packages are up to date.
pi@raspberrypi:~ $ sudo apt-get install gnome-schedule
Reading package lists… Done
Building dependency tree
Reading state information… Done
E: Unable to locate package gnome-schedule
pi@raspberrypi:~ $
Hi Gordon,
I’m not sure why it is not installing for you. I am able to install and locate it on my PI.
I would follow step 24 i instead. This is how to add the script to Cron without using the graphical gnome schedule.
Update: after some research it appears Raspian Buster has dropped gnome schedule has been dropped from its repository so you will need to use step 24.i
Hi
could it be, that in point
Enter the following string – 0 * * * * /home/pi/Documents/drive.sh
this should be sync.sh and not drive.sh
Hey, yes thank you. That is a typo, it should be sync.sh
Hello!
I am at setp 20 and it works. Any thought on How to get it to work with a foldaer that uses spaces? Hpw should I write that?
Thank you!
Hi, Thats Great!
I believe you would need to put quotation marks around the path EG. rclone sync -v /home/pi/Documents “onedrive:My Documents”
Hi
I dont find a way to Upload Data to Onedrive.
Is that even Possible?
This will just download but you might be able to change the line of code to upload. You can check the command on the rclone site.
You could try swapping the source and destination, i’m not sure if it would work.
rclone sync -v /home/pi/Documents onedrive:Documents
Thanks for this info, as I was following along I ran into an issue with step 10. I am running in headless and an not sure how to finish this step. I tried setting up rclone on my windows laptop but couldn’t figure out what to paste in 10.iii?
Any help would be great
Thanks
Hey Rob,
You can run rclone on windows 10 by downloading it from the rclone website https://rclone.org/downloads/ (Intel/AMD – 64 Bit). Extract the files and then open CMD. Navigate to that folder using the cd command (mine is cd C:\Users\Jarrod\Downloads\rclone-v1.53.3-windows-amd64\rclone-v1.53.3-windows-amd64). From here you can run the rclone authorize onedrive command. A web browser will appear and you can sign in to authorise. Then copy the key from that CMD to your headless raspberry pi to authorise.
Hi.
The -v flag should be a -i flag unless you would like all your files deleted 🙂
We followed the instructions and it deleted all the data from my project
Best regards
Hi,
Thanks for the feedback but that is the point of the tutorial. It is meant to make the destination identical to the source. If you follow the instructions and create an upload and/or download folder that are empty there won’t be an issue.
But yes if you would also like to leave any other files in the destination you can use -i.
hi, at step 11 i´m told that i can´t login with a personel account. is there a solution for this?
failure was the germna cloud. with global it is working. now i have the problem that the cronjob won´t start automatically. Couldn´t find the failure so far.
Hi Rudolf,
Have you restarted the PI after saving the cronjob?
Hi,
I found this is very informative and I appreciate the whole document.
I have a question regarding the sync.sh running.
In my application, I turn on the RPi every 30 minutes and take a picture and store it in a folder then turn off the RPi.
I need to run the sync.sh at those instances. I tried two options the RC.locale and contab -e.
The sync.sh worked well when I call it through the terminal using sh /home/pi/……… or run code using the python script os.system(“sh /home/…..”) code os.but when I try to automate using both options I mentioned it did not work.
What may be the reason and how to accomplish this task. I use the onedrive.
Hi Nipuna,
Thank you. The script will sync once at the point in time when it is run. RC.locale would only run the script once on startup.
Shutting down the Pi quickly will cause problems and the cron job won’t have time to run, my guide sets the script to run once an hour.
Try this string in step 28, it should run the script every minute. “* * * * * /home/pi/Documents/sync.sh”
Just make sure you leave the Pi on for at least 5 minutes after taking your photo.
Hi,
Thank you It worked after I did a little change, I add “sh” like this * * * * * sh /home/pi/Documents/sync.sh
appreciate your support a lot
Ah okay, Awesome glad I could help.
Hi!
I’m new with rclone and scripts.
Can I sync the whole OneDrive folder to my Raspberry?
Or can I add multiple commands to the script?
It would be good if I have a backup of everything on my OneDrive.
Hi Erpi,
I haven’t tried syncing the whole Onedrive, you could try but from what I’ve seen you usually have to give a specific folder.
You can add multiple commands to the script, yes that works well.
Hi!
Im using:
TIMESTAMP=$(date +”%F”)
rclone sync -v /webserver onedrive:webservern/$TIMESTAMP
and
TIMESTAMP=$(date +”%F”)
rclone copy /webserver onedrive:testar/$TIMESTAMP
in my folder i looks like this:
drwxrwxrwx 1 root root 0 Dec 10 17:07 axiell
drwxrwxrwx 1 root root 0 Jan 9 00:02 backup **
drwxrwxrwx 1 root root 0 Jan 6 21:15 budget **
drwxrwxrwx 1 root root 0 Dec 10 17:07 filup
drwxrwxrwx 1 root root 0 Jan 9 09:55 ‘Hans testar ifall denna mapp kopieras’
drwxrwxrwx 1 root root 0 Nov 1 16:37 Mertz2 **
drwxrwxrwx 1 root root 0 Dec 19 11:19 script **
those with ** are the ones it does sync/copy
and i have 7 folders inside my webserver folder on my pi, but it only sync/copy the 4 folders.
Any suggestions how to get all 7 folders?
Best regards
//Hans
Hey Hans,
I can’t say I’ve seen that issue before. I’m also not sure why you are using 2 commands to copy the same data into 2 different OneDrive folders.
The issue could be caused by many things. Did you try with the sync commands I have on my post, did that work properly?
Have you tried:
– running only one command and removing the timestamp variables
– check the permissions of the folders that aren’t syncing
– Depending on how much data there is in the folders, maybe something is timing out or crashing
Hi.
Thanxs for the reply, i only tried 2 commands for testing.
i followed your tutorial and it does sync but only same 4 folders as earlier written.
when i’m in the raspberry command i write, ls -al then i get this:
root@raspserver:/webserver# ls -al
total 4
drwxrwxrwx 1 root root 0 Jan 10 23:43 .
drwxr-xr-x 24 root root 4096 Jan 10 23:53 ..
drwxrwxrwx 1 root root 0 Dec 10 17:07 axiell
drwxrwxrwx 1 root root 0 Jan 9 00:02 backup
drwxrwxrwx 1 root root 0 Jan 6 21:15 budget
drwxrwxrwx 1 root root 0 Dec 10 17:07 filup
drwxrwxrwx 1 root root 0 Jan 9 09:55 ‘hans test’
drwxrwxrwx 1 root root 0 Jan 10 23:43 hasse2023
drwxrwxrwx 1 root root 0 Nov 1 16:37 Mertz2
drwxrwxrwx 1 root root 0 Jan 10 21:05 script
root@raspserver:/webserver#
somehow it counts 4 directory’s but it is now 8.
i don’t know what to do… i can’t see what’s wrong. i tried remove the folders and adding them again. but still same error
// Hans
Hi!
I found the problem, it was a password error. it is working now.
Thanxs
// Hans