Code Monkey home page Code Monkey logo

filesync's Introduction

FileSync

  • FileSync is very basic implementation of .NET GoogleDriveAPI v3.
  • It uploads a single file to Google Drive cloud storage, using a Google service account.
  • It grants permission to this file to another Google Drive account.
  • It is shipped with a tiny command line application.
  • For the moment it is just an uploader, not a file syncing tool at all.

.NET Publish

Download latest release

Table Of Contents

Prerequisites

  • The released version is tested with Windows 10 and Ubuntu 20.04.
  • You will need to create a google service account. Read this.
  • A service account is a different account, it's not your own Google account.
  • Service accounts have their own 15 GB Drive free space.
  • It's not visible via a web interface or somewhere else (unless you get a list via API, which is not implemented in FileSync at the moment).

Command Line Usage

Global Options and Commands

Jaxx.FileSync.exe 
    -c ["PATH_TO_CERTFILE\key.p12"] 
    -s ["[email protected]"] 
    [COMMANDS] [COMMANDS_OPTIONS]
  • Global options are mandatory!

Example

Jaxx.FileSync.exe 
    -c "C:\Users\mike\privatekeys\mikeskey.p12" 
    -s "[email protected]"
    list

Global Options in Detail

  • -c ["PATH_TO_CERTFILE\key.p12"]
    • Provide the full path and filename of the private certificate p12 file you've downloaded from google api console after you've created the service account.
  • -s ["[email protected]"]
    • Provide the mail account you've created for the service account.
  • [COMMANDS] [COMMANDS_OPTIONS]
    • commands and subcommands as list, create file, delete object (as follows)

Upload a File

Jaxx.FileSync.exe 
      [GLOBAL_OPTIONS]
      create file
      -g ["[email protected]"] 
      -f ["UPLOAD_FILE"]
      -d ["root"]
  • All options are mandatory!
  • You have to use the -g option with your own drive space, otherwise you'd never ever have the chance to see your uploaded file again.

Example

Jaxx.FileSync.exe 
      -c "C:\Users\mike\privatekeys\mikeskey.p12"
      -s "[email protected]"
      create file
      -g "[email protected]" 
      -f "C:\Users\mike\Documents\list.txt"
      -d "root"

Options for Creating a File in Detail

  • [GLOBAL_OPTIONS]
    • see there
  • create
    • Simply calls the command to create (upload) an object.
  • file
    • Tells the create command to create (upload) a file object.
  • -g ["[email protected]"]
    • Grant permissions to the created object to another google drive user, so that he will be able to see the files in his own account's shared folder (grant it to yourself!)
  • -f ["UPLOAD_FILE"]
    • Full path to the file you'd like to upload.
  • -d ["root"]
    • The folder where the file should be created. The folder has to exist.
    • Pass "root" if the file should be created in the root folder.

Create a Folder

Starting with release 1.1.0 you can create folders in you drive storage.

Jaxx.FileSync.exe 
      [GLOBAL_OPTIONS]
      create folder
      -g ["[email protected]"] 
      -n ["NEW_FOLDER_NAME"]
      -p ["PARENTFOLDER"]
  • All options are mandatory!
  • If you want to create folders in the root path, pass 'root' as parent folder.
  • A parent folder has to exist if you want to create a child folder.
  • Be aware that folder names need to be unique in your store, due to my hasty implementation.

Example

Jaxx.FileSync.exe 
      -c "C:\Users\mike\privatekeys\mikeskey.p12"
      -s "[email protected]"
      create folder
      -g "[email protected]" 
      -n "SharedDocuments"
      -p "root"

Options for Creating a Folder in Detail

  • [GLOBAL_OPTIONS]
    • see there
  • create
    • Simply calls the command to create an object.
  • folder
    • Tells the create command to create a folder object.
  • -g ["[email protected]"]
    • Grant permissions to the created object to another google drive user, so that he will be able to see the object in his own account's shared folder (grant it to yourself!)
  • -n ["NEW_FOLDER_NAME"]
    • Name of the new folder
  • -p ["PARENTFOLDER"]
    • Name of the parent folder ('root' for root folder)

List Contents

Starting with release 1.2.0 you can list the content in your drive storage.

Jaxx.FileSync.exe [GLOBAL_OPTIONS] list

This will list all objects available in your storage:

[ObjectId] [mimeType] -> filename.ext, lastmodified

THE OBJECT ID IS THE LISTED ID WITHOUT THE SQUARE BRACKETS!

Example

Jaxx.FileSync.exe -c "C:\Users\mike\privatekeys\mikeskey.p12" -s "[email protected]" list

Delete Objects

Starting with release 1.2.0 you can delete single objects in your drive storage. To delete an object you'll need the object id you can gather with the list command.

Jaxx.FileSync.exe 
      -c "C:\Users\mike\privatekeys\mikeskey.p12" 
      -s "[email protected]" 
      delete object -i "7A-155DKmzQ-SGD9zync0cdK8LNz"

As in all calls before, pass the global options, followed by the command delete object the parameter -i and the object id. There is no differene between a file and a folder, they are all objects.

DELETE A FOLDER WILL DELETE ALL CONTENT IN THIS FOLDER, TOO! THERE WILL BE NO WARNING!

All options are mandatory!

Delete Objects by Age

Beeing a kind of backup tool, FileSync will run unattended for a while. To clean up old backups in the Drive storage you could use the "delete agedfiles" command (Release >= 1.2.0).

Jaxx.FileSync.exe 
      [GLOBAL_OPTIONS]
      delete agedfiles
      -a [AGE_IN_DAYS] 
      -f [PARENTFOLDER]
      -k [DAYS_TO_KEEP]
      -p 

Example

Jaxx.FileSync.exe -c "C:\Users\mike\privatekeys\mikeskey.p12" -s "[email protected]" 
    delete agedfiles -a 30 -f "mikesbackups" -k 10

Options for in Detail

  • [GLOBAL_OPTIONS]
    • see there (mandatory)
  • delete agedfiles
    • Calls the command
  • -a [AGE_IN_DAYS] (mandatory)
    • Files which are not modified since the passed count of days will be affected.
  • -f [PARENTFOLDER] (mandatory)
    • Just files in this folder will be affected.
  • -k [DAYS_TO_KEEP] (opitonal)
    • optional parameter, will prevent to delete this number of files, if they are the last ones which had have been modified - despite of having reached the given age. (see Scenario)
  • -p (optional)
    • Passing this optional parameter will enable the preview mode. Nothing will be deleted but you'll see what would be deleted.

Scenario

Mike runs a little MySQL database on a Windows OS. Each night it creates a dump of the database to backup data. The drawback: Until now this backups were stored on the local hard drive. What happens if this disk crashes?

After throwing away other ideas Mike decided to upload an extra backup to Google Drive. But he didn't want to install Google Drive app on his server for reasons:

  • Neither, he wants to log in with his own google account
  • Nor, he wants to create a google account for the server user
  • Nor, it would be guaranteed that the user is logged into the system all the time at all

He decided to create a google service account, authenticated by an email account and a private certification file (key.p12). To access his uloaded backups he grants his own google account the necessary permissions, so it will be visible in his own cloud drive in "shared files".

He created a little batch file which first creates the backup of the MySQL database and uploads the backuped file in a next script. The batch file is trigger via build in Windows Task Scheduler.

After a while his store grows and grows as more and more backups are uploaded. He decides, that he only wants to keep the backups from the last 30 days. But wait! What if the backups won't run for some days and Mike didn't notice this for any reason? So he decided, that he always want to keep the last 10 files.

Automation / Periodical Execution

As Mike in our scenario: Use the Windows Task Scheduler to automate FileSync.

Enhancements

Check the issues page on GitHub for further "nice to haves", feel free to create issues or to contribute.

Build and Run from Source

  • Microsoft Visual Studio 2019 Community Edition
  • .NET 5.0

To build & run, run dotnet restore and dotnet run.

Third Party

Released version is shipped with (and dotnet restore will restore the NuGet packages) the following third party libraries:

filesync's People

Contributors

viper3400 avatar

Watchers

 avatar  avatar

Forkers

xwyangjshb

filesync's Issues

Pass Metadata in Body When Creating a GoogleDrive File

In order to get #4 implemented at least the creation and modification time must be set, otherwise it seems to be empty in DriveApi.cs method UploadFile:

File body = new File();
body.Name = System.IO.Path.GetFileName(_uploadFile);
body.Description = "File uploaded by FileSync";
body.MimeType = GetMimeType(_uploadFile);
body.Parents = new List<string> { _parent };
body.WritersCanShare = true;

Add Housekeeping Features

Files with a certain file age should be deleted.

Jaxx.FileSync.exe [global] delete agedfiles -a | --age [30] -f | --folder [foldername]

  • #6 must be solved before this can take place

Taking the current timestamp and delete back for x days won't be good if a backup haven't run for y days without being noticed - this could probably mean loss of backups.

  • For better backup strategy maybe the last x files should be kept while all older ones were deleted.
  • Or we look at the latest modified days to determine the "agedfiles" and go back from their instead of taking the current time.

Remove Win32 dependency

In order to run in a alpine linux docker container the win32 dependency from Drive.Api has to be removed.

using Microsoft.Win32;

        // tries to figure out the mime type of the file.
        private static string GetMimeType(string fileName)
        {
            string mimeType = "application/unknown";
            string ext = System.IO.Path.GetExtension(fileName).ToLower();
            //TODO: Eliminate Win32 dependency
            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
            if (regKey != null && regKey.GetValue("Content Type") != null)
                mimeType = regKey.GetValue("Content Type").ToString();
            return mimeType;
        }

Maybe we can use https://github.com/hey-red/Mime

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.