OneDrive Hourly Refresh Utility For Syncing Large Quantites of Files

I am nearing the last day with my current job at FINEOS Corporation and transitioning into my new role at Benjamin in the coming weeks.

With my impending departure, I am making major efforts to offload >300GB worth of client data located in my FINEOS Office 365 OneDrive.

This data is from the various obligations worked in the past three years – so it covers quite a bit of consulting work done to date.

I’m doing this in an effort to make sure my team members are able to continue day-to-day operations uninterrupted.

Nobody likes to be left with zero documentation or dead file links – it just sucks when you’re on the receiving end of this sort of thing.

Something that surprised me (and prompted this whole effort) was learning 30 days after my departure, all files in my OneDrive will be deleted and not archived – so that wasn’t good to hear.

Manual Backup Steps – With this background information lets step into the sequence of events here that prompted the blog post and subsequent utility script.

  1. Set OneDrive to download all files from the cloud (untick OnDemand, forcing all files to download locally)
  2. Create Storage Directories on Team SharePoint sites and click “Sync with OneDrive” on those directories
  3. Move the downloaded files into their associated SharePoint Sync folders on my computer
  4. OneDrive Freaks out from >300GB of files, freezing up, and periodically needs a restart of the client due to indexing of >30,000 files

Automated Utility Script Sequence – So overall, I wrote a script that will automate Step 4 and restart OneDrive every hour:

  1. Start Script
  2. Enter Infinite While Loop
    1. Script attempts to Shut Down OneDrive
      1. Enter While Loop
      2. Detects how many instances of “OneDrive.exe” are running, if greater than or equal to 1 proceed to next step, if 0 break from while loop
      3. Initially will attempt to do “OneDrive.exe /shutdown” and sleep for 30 seconds
      4. Detects how many instances of “OneDrive.exe” are running, if greater than or equal to 1 proceed to next step, if 0 break from while loop
      5. After 30 seconds, will attempt to gracefully “taskkill” without the force flag – uses the term signal instead
      6. Detects how many instances of “OneDrive.exe” are running, if greater than or equal to 1 repeat loop, if 0 break from while loop
    2. Script attempts to Start Up OneDrive
    3. Script Sleeps for 1 hour before attempting to refresh OneDrive again
    4. Repeat Loop after Sleep timer ends

I was surprised to find out that there are no formal methods to send a periodic pause/resume command to OneDrive.exe so I had to really think out of the box on this one based on the limited information I was able to obtain on scripted usage of OneDrive.exe

#!/bin/bash

# Time in Hours before attempting to Restart OneDrive
ONEDRIVE_REFRESH_INTERVAL_IN_HOURS="1"

# Global Variable for Function Usage that tracks if OneDrive is running
GLOBAL_ONEDRIVE_RUNNING="TRUE"

function onedrive_CheckIfRunning()
{
    echo "[INFO] [DetectIfRunning] Checking if OneDrive.exe is running..."

    ONEDRIVE_PROCESS_COUNT=$(tasklist | grep -i "OneDrive.exe" | wc -l)

    echo "[INFO] [DetectIfRunning] ONEDRIVE_PROCESS_COUNT is $ONEDRIVE_PROCESS_COUNT"

    if [ "$ONEDRIVE_PROCESS_COUNT" -ge "1" ];
    then
        GLOBAL_ONEDRIVE_RUNNING="TRUE"
    else
        GLOBAL_ONEDRIVE_RUNNING="FALSE"
    fi
}

function onedrive_ShutdownSleepTimer()
{
    # Create some self documenting variables to make the code
    # more descriptive on how the multiplication occurs
    SECONDS_IN_A_MINUTE="60"
    MINUTES_IN_A_HOUR="60"

    ONEDRIVE_REFRESH_INTERVAL_IN_SECONDS=$(($MINUTES_IN_A_HOUR * $SECONDS_IN_A_MINUTE * $ONEDRIVE_REFRESH_INTERVAL_IN_HOURS))

    echo "[INFO] [WAIT] Sleeping for $ONEDRIVE_REFRESH_INTERVAL_IN_HOURS hours or $ONEDRIVE_REFRESH_INTERVAL_IN_SECONDS seconds before attempting to restart OneDrive..."
    sleep $ONEDRIVE_REFRESH_INTERVAL_IN_SECONDS
}

function onedrive_Start()
{
    echo "[INFO] [START] Starting OneDrive..."
    start "" "$LOCALAPPDATA/Microsoft/OneDrive/OneDrive.exe"
}

function onedrive_Shutdown()
{
    echo "[INFO] [STOP] Refreshing OneDrive (Shutting Down) ..."

    GLOBAL_ONEDRIVE_RUNNING="TRUE"

    onedrive_CheckIfRunning

    while [ $GLOBAL_ONEDRIVE_RUNNING == "TRUE" ]
    do
        echo "[INFO] [STOP] Attempting Proper Shutdown of OneDrive.exe ..."
        start "" "$LOCALAPPDATA/Microsoft/OneDrive/OneDrive.exe" /shutdown

        echo "[INFO] [STOP] Sleeping for 30 seconds to allow OneDrive.exe time to shutdown ..."
        sleep 30

        onedrive_CheckIfRunning

        if [ "$GLOBAL_ONEDRIVE_RUNNING" == "TRUE" ];
        then
            echo "[INFO] [STOP] OneDrive.exe detected as running still, attempting a graceful task closure..."
            taskkill //im "OneDrive.exe"
            
            echo "[INFO] [STOP] Sleeping for 30 seconds while waiting for it to exit..."
            sleep 30
            
            onedrive_CheckIfRunning
        else
            GLOBAL_ONEDRIVE_RUNNING="FALSE"
        fi
    done
}

function main()
{
    while [ "TRUE" == "TRUE" ]
    do
        echo "[INFO] [HowToCloseScript] OneDrive Refresh Program is running in Endless While Loop, Press Ctrl+C to Stop Script if Desired ..."
        onedrive_Shutdown
        onedrive_Start
        onedrive_ShutdownSleepTimer
    done
}

main

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s