Today I want to demonstrate how you can program a backup tool using just a few lines of C# code.

Background

Back in the day when I used Microsoft Windows a lot for work, I used robocopy for my backups.

There is indeed rsync on UNIX- and Linux systems already, however, I would like to use a tool that is simple, platform-independent, and can be customized according to my needs.

Requirements

You need the latest version of .NET SDK 8.0 or later before you can compile and/or run the tool.

Features

The application mirrors a source directory to a target:

  • copies recursively
  • only copies files, if targets have different file sizes and/or timestamps
  • removes files and directories in target, which cannot be found in source anymore

Code

The code can be found at GitHub.

Check the repository out, switch to its root directory and execute

dotnet run -- /path/to/source/directory /path/to/target/directory

in your terminal.

If you want to compile it, execute

dotnet build --configuration Release

instead, which will produce an fb or fb.exe (Windows) executable file in bin/Release/net8.0 subfolder.

You can you copy this file to a folder, which is part of your PATH environment variable. This makes it possible to run the tool from everywhere:

fb /path/to/source/directory /path/to/target/directory

# or shorter from source directory:
# fb /path/to/target/directory

Logging

Each important step is logged to the console, including a final summary.

You can pipe this information to a file:

fb ./src ./dest > backup-logs.txt

Conclusion

As one of the next features, I will implement encrypting the target files and directories using a password.

Have fun trying it out!