7

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?

  • 1
    I think that once the daemon starts, the sync occurs automatically. I am not sure about this. – Mihai Sep 25 '13 at 11:25
  • Maybe I can start the daemon, periodically check its status, and kill it once it reports it has done syncing? – erjoalgo Sep 25 '13 at 11:26
4

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
  • If it worked please mark your answer as the solution. – Mihai Sep 25 '13 at 11:42
  • As soon as I actually write the script and verify it works, I will. Thanks for your idea. – erjoalgo Sep 25 '13 at 11:58
  • Recent dropbox releases seem to report "Up to date" instead of "Idle". You might have to change that in your script if you are using an updated version. – Glutanimate Dec 17 '14 at 12:20
3

Here is a very simplstic way to accomplish this:

I have a slow internet connection, and don't want Dropbox running at all during the day when I am working, but want it running all night when I am supposed to be asleep.

I set up a cron job like this:

At a terminal type crontab -e

Add these lines:

#This line will stop Dropbox at 7 AM every morning:
 * 7 * * * dropbox stop
#This line will start dropbox at 10 PM every evening:
 * 22 * * * dropbox start
  • If you are asleep, then there is not much point to leaving dropbox running all night. It will do an initial sync, then remain idle for the rest of the time. The script that I wrote above, which starts dropbox, waits until it becomes idle after syncing, then shuts it down, is scheduled to run via anacron once per day. So we have a similar approach, except that it doesn't really make sense to keep dropbox alive if the content that you are syncing isn't expected to change. – erjoalgo Nov 12 '13 at 21:13
1

Using user84207 and Loony answers:

  • Create 2 scripts:

startDropbox.sh:

dropbox start

stopDropbox.sh:

result=$(dropbox status)
if [ "$result" = "Up to date" ];
then
   dropbox stop
fi
  • Add in crontab, with crontab -e

# starts only after each 5 minutes
*/5 * * * * startDropbox.sh
# try to stop every minute
*/1 * * * * stopDropbox.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.