17 Jun How to sync any folder on your PC with OneDrive
At the beginning of the year, I decided upon using [OneDrive](https://onedrive.live.com/) as my cloud backup solution. OneDrive is more of a cloud *storage* provider, but it can also be used for backups with some simple tweaks! I was originally deciding between [Backblaze](https://www.backblaze.com/), [Dropbox](https://dropbox.com), and [Drive](https://drive.google.com). I wasn't too happy with pricing for the latter two. Backblaze seemed interesting with its "just-backup-everything" approach, but I later chose OneDrive to also get Office 365 for free.
One of the problems I immediately ran into was the fact that OneDrive is a storage provider and not a backup provider. OneDrive installs itself into the user folder in Windows. From there, you can just drag and drop any files you want into the OneDrive folder, and it'll automatically be synced with your account. This is simple and seems to work well in most use cases. However, for people who already have their important pictures and files and documents in a preexisting folder hierarchy, this doesn't work well. In addition, if you're like me and have a small `C:\` drive for Windows and one or more large HDDs for storing files, you'll know how nearly impossible it is to just move everything into the OneDrive folder—especially considering that OneDrive is 1TB of storage space!
Enter symbolic links. The symlink that most people are familiar with are desktop shortcuts. These soft links point to files at a remote location. For example, a shortcut to Firefox on your desktop actually opens `C:\Program Files\Mozilla Firefox\firefox.exe`.
![Firefox shortcut](/_static/stuff/2020-06-17-firefox_shortcut.png)
The symlink that is relevant to our OneDrive discussion is called a [junction](https://docs.microsoft.com/en-us/windows/win32/fileio/hard-links-and-junctions). This creates a sort of "copy" of the folder, which is actually just an alias of the remote location. For example, if you have a folder that is `D:\this\folder\is\nested\so\deeply\and\it\is\annoying`, you can create a junction to it from `C:\thatfolder`. You can then browse `thatfolder` the same as you would `annoying`, except the files take up space on the `D:\` drive. The command to create that junction would be:
```
mklink /J "C:\thatfolder" "D:\this\folder\is\nested\so\deeply\and\it\is\annoying"
```
We now see that for OneDrive we can easily preserve our existing folder structures at their respective original locations while also backing up their contents to OneDrive *simply by using a junction*!
```
mklink /J "%OneDrive%\Pictures" "D:\Media\Pictures"
```
And voilĂ ! It appears to be backing up. Alas, if only things were this easy. The next problem you'll probably encounter is that it doesn't work. Or rather, it only works once. OneDrive does not appear to observe changes to junctions when syncing. There is, however, a simple way around this. In the process of finding out why this happened, I created a file in my OneDrive root. After a few minutes, it started syncing changes from junctions. After some more digging, I realized that the process of **creating and deleting a real file** in OneDrive triggers the sync process to detect all changes. From there, it was a simple matter of creating a batch script and running it from the Task Scheduler.
```
echo hi > %OneDrive%\sync
del %OneDrive%\sync
```
Each time this batch script is run, it creates the `sync` file and immediately deletes it. Trigger this from whatever event you'd like, and now you have a OneDrive that can sync any folder on your computer!
Comments