以编程方式同步dropbox而不运行守护程序
我想’手动’强制Dropbox在特定时间同步(例如,使用cron定期,每天一次,作为更大的备份脚本的一部分)。 我的目标是仅在我控制的时间用单个“sync”命令调用替换dropbox守护进程。
查看Ubuntu上dropbox
命令的文档,我只看到启动/停止守护进程的方法,但不强制它同步。
是否有可以实现此目的的较低级别的api?
使用@ Rat2000的建议,我做到了这一点。 在一个终端,
while true; do dropbox status; sleep 1; done;
在另一个终端:
touch ~/Dropbox/test
第一个终端显示大致以下输出:
Idle Idle Idle ... ... Updating (1 file) Indexing 1 file... Updating (1 file) Indexing 1 file... ... ... Downloading file list... Downloading file list... ... ... Idle Idle
因此,我们可以使用expect
定义一个脚本,在受控时间执行一次性Dropbox同步,假设dameon在完成同步之前不会报告“空闲”。
编辑:
这个解决方案似乎对我有用:
#!/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)
备注 :
-
根据您的Dropbox版本,您可能需要更换
elif re.search("Idle", status):
同
elif re.search("Up to date", status):
-
为了减少对系统性能的影响,您可以尝试使用nice , ionice和nocache等实用程序,例如:
print subprocess.check_output(['nice', '-n10', 'ionice', '-c3', 'nocache', 'dropbox', 'start'])
用anacron
设置脚本
当然,我安排这个脚本通过anacron运行,如下所示:
1 10 dropbox_do_sync su myuser -p -c "python /home/myuser/scripts/dropbox_do_sync.py" >> /home/myuser/logs/anacron/dropbox_do_sync
这是一种非常简单的方法来实现这一目标:
我有一个缓慢的互联网连接,并且我不希望Dropbox在我工作的白天运行,但是希望它在我应该睡着的时候整夜运行。
我设置了这样的cron工作:
在终端类型crontab -e
添加以下行:
#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
使用user84207和Loony答案:
- 创建2个脚本:
startDropbox.sh:
dropbox start
stopDropbox.sh:
result=$(dropbox status) if [ "$result" = "Up to date" ]; then dropbox stop fi
- 使用crontab -e添加crontab
#每5分钟后才开始
* / 5 * * * * startDropbox.sh
#尽力停止每一分钟
* / 1 * * * * stopDropbox.sh