Ubuntu – Programmatically sync dropbox without daemon running

crondropboxsync

I would like to 'manually' force dropbox to sync at certain times (eg at regular, daily intervals using cron, as part of a larger backup script). My goal is to substitute the dropbox daemon with single "sync" command invocations at only the times that I control.

Looking at the documentation for the dropbox command on Ubuntu, I only see a way to start/stop the daemon, but not to force it to sync.

Is there a lower level api available that can accomplish this?

Best Answer

Using @Rat2000's suggestion, I did this. In one terminal,

while true; do dropbox status; sleep 1; done;

In another terminal:

touch ~/Dropbox/test

The first terminal shows approximately the following output:

Idle
Idle
Idle
...
...
Updating (1 file)
Indexing 1 file...
Updating (1 file)
Indexing 1 file...
...
...
Downloading file list...
Downloading file list...
...
...
Idle
Idle

So we can define a script, using expect, to do a one-time dropbox sync at controlled times, assuming that the dameon does not report "Idle" until it has finished syncing.


Edit:

This solution seems to work for me:

#!/usr/bin/python
import subprocess, time, re

print "Starting dropbox daemon"
print subprocess.check_output(['dropbox', 'start'])

started_sync = False
conseq_idle = 20
while True:
    status = subprocess.check_output(['dropbox', 'status'])
    print status
    if re.search("Updating|Indexing|Downloading", status):
        started_sync = True
        conseq_idle = 20
    elif re.search("Idle", status):
        conseq_idle-=1
        if not conseq_idle:
            if started_sync:
                print "Daemon reports idle consecutively after having synced. Stopping"
                time.sleep(5)
            else:
                print "Daemon seems to have nothing to do. Exiting"
            subprocess.call(['dropbox', 'stop'])
            break
    time.sleep(1)

Notes:

  • Depending on your Dropbox release you might have to replace

    elif re.search("Idle", status):
    

    with

    elif re.search("Up to date", status):
    
  • To reduce the impact on system performance you can experiment with utilities such as nice, ionice, and nocache, e.g.:

    print subprocess.check_output(['nice', '-n10', 'ionice', '-c3', 'nocache', 'dropbox', 'start'])
    

Setting the script up with anacron

Of course, I schedule this script to run via anacron, like this:

1 10  dropbox_do_sync  su myuser -p -c "python /home/myuser/scripts/dropbox_do_sync.py" >> /home/myuser/logs/anacron/dropbox_do_sync
Related Question