DEV Community

loading...
Cover image for Backing up files with F# Scripts and MongoDB GridFS

Backing up files with F# Scripts and MongoDB GridFS

tunaxor profile image Angel D. Munoz ・2 min read

F# 5.0 has strong scripting capabilities thanks to the updated #r "nuget: " directive, this allows you to pull any NuGet dependency and start using it from your F# scripts right away. To show off this feature I'll propose the following use case

I want to backup files from a directory into a database and be able to restore them back to the directory I want

There are some ways we can do this from saving the data to a database and uploading to AWS/GCP/Azure/Dropbox in this case I wanted to showcase one of the Libraries I worked on previously called Mondocks which allows you to write MongoDB commands and execute them via the normal .NET MongoDB Driver.
I have a Raspberry PI on my LAN with a 1TB HDD and it has docker running so I can save my files there.

Let's start with the Backups, also I added some comments so you can read the source code and continue reading hereafter

that's all we need to start backing up either the current directory or a directory we specify with our arguments, that should look like the following Gif

Backup Files

Now Let's continue with the restores


The restoration process was hopefully as simple as the backup and should look like this
Restore Backup

And that's it! Hopefully, I showed you a bit of the F#'s scripting capabilities as well as how simple is to use Mondocks without sacrificing the MongoDB driver, since it's a side by side usage thing here's the project if you'd like to take a look

GitHub logo AngelMunoz / Mondocks

An alternative way to interact with MongoDB databases from F# that allows you to use mongo-idiomatic constructs

Mondocks

nuget Binder

dotnet add package Mondocks

This library is based on the mongodb extended json spec and mongodb manual reference

https://docs.mongodb.com/manual/reference/mongodb-extended-json/ > https://docs.mongodb.com/manual/reference/command/

This library provides a set of familiar tools if you work with mongo databases and can be a step into more F# goodies, it doesn't prevent you from using the usual MongoDB/.NET driver so you can use them side by side. It also can help you if you have a lot of flexible data inside your database as oposed to the usual strict schemas that F#/C# are used to from SQL tools, this provides a DSL that allow you to create MongoDB Commands (raw queries) leveraging the dynamism of anonymous records since they behave almost like javascript objects Writing commands should be almost painless these commands produce a JSON string that can be utilized directly on your application or even copy/pasted into the mongo shell Commands are kind…

Also, Shout out to Spectre.Console for the amazing console output.

Closing thoughts

Remember that F# is cross-platform, so if you want to run these scripts on Linux/MacOS as well you should be able to do so. F# is powerful yet it won't get in your way, it will most likely help you figure out how to do things nicely.

If you have further comments or doubts, please let me know down below or ping me on Twitter 😁

Discussion (0)

pic
Editor guide