Upload backups to Dropbox with crontab and dropboxd

Dropbox has an advantage - it syncs a new backup as soon as it appears in the system. The deal is to put new backup to your local Dropbox folder (with a cron script provided).

Before you proceed you have to know:

  1. You may use Dropbox to store files as much as your plan allows.
  2. You may connect up to 3 devices to Dropbox at free plan (for Android devices this limitation can be bypassed, by the way).
  3. When you will run dropbox to your linux server, then you need to synchronizethe only one folder. Or as Dropbox a two-way solution, it may populate your server with all data from the cloud. This can eat space on your server.
  4. You have to create an additional rsync script to sync between Discourse backup folder and local Dropbox backup folder. This requires double space for backups. Earlier Dropbox allowed symlinks but currently this feature is not available.
  5. To access your backups you don’t need root grants, but I run dropbox daemon as root in this guide (probably unsafe, welcome to improve)

First you need to setup backups in Discourse at /admin/site_settings/category/backups (my custom values in brackets)

maximum backups (3) - this manages how many files will retain in Dropbox
backup frequency (1) - everyday
backup time of day (3:30 UTC) - default
backup gzip compression level (6) - choose between compress speed (1) or less size (9), but in case of VPS your host provider my restart your server in case of long high CPU utilization

Second connect your server to Dropbox with shell commands (important: because I run the service as root, I am logged as root at this step - so you may try your regular user):

cd ~ && wget -O - "https://www.dropbox.com/download?plat=lnx.x86_64" | tar xzf -
# directory .dropbox-dist will be created
# run dropbox daemon
sh ~/.dropbox-dist/dropboxd

When you first use Dropbox at your server you will see a uniq weblink. You need to copy and paste it to web browser and follow the instructions. After that a directory Dropbox will be created in your home folder. Official Dropbox recommends to download Python script to manage your daemon. I don’t use it.

Commands to manage the daemon:

dropbox status
dropbox start
dropbox stop
Check your autostart script has been created (click to expand hidden text):
#cat /etc/systemd/system/dropbox.service

[Unit]
Description=Dropbox Service
After=network.target

[Service]
ExecStart=/bin/sh -c '/usr/local/bin/dropbox start'
ExecStop=/bin/sh -c '/usr/local/bin/dropbox stop'
PIDFile=/root/.dropbox/dropbox.pid
User=root
Group=root
Type=forking
Restart=on-failure
RestartSec=5
StartLimitInterval=60s
StartLimitBurst=3

[Install]
WantedBy=multi-user.target

Discourse provides backup files and it manages their number and the frequency.
Crontab rsync task copies new backup to local Dropbox folder.
After that Dropbox uploads new file to the cloud.
When next night Discourse removes old backup file, rsync removes it from Dropbox as well.

Third create dropbox folder for backup copies:

mkdir ~/Dropbox/backups

Fourth prepare folder for reverse-sync (unsync unnecessary folders)
For example, your Dropbox folder has subfolders and a file:

backups
books
stuff
logo.png

You need to exclude all except backups. Actually the command adds items to exclude list:

dropbox exclude add "Dropbox/books/"
dropbox exclude add "Dropbox/stuff/"
dropbox exclude add "Dropbox/logo.png"

If you suddenly added backups to exclude list, you can return it to synchronization, removing the item from the list:

dropbox exclude remove "Dropbox/backups/"

Current status of certain backup can be checked by the command:

dropbox filestatus Dropbox/backups/niti-2021-01-21-033726-v20201218000001.tar.gz

Fifth prepare script for local sync and clearing cache/root/rsync.dropbox:

#!/bin/sh
rsync -avh --stats --progress --delete /var/discourse/shared/standalone/backups/default/ /root/Dropbox/backups/
rm -rf /root/Dropbox/.dropbox.cache/

Sixth add command to crontab at 8AM local time: crontab -e

0 8 * * * /bin/bash /root/rsync.dropbox

There is a timewindow between 3AM UTC and my local 8AM - it should be enough to backup completion before it begin synchronizing with Dropbox.

P.S.
I did not check this configuration half a year or even more. Now I realized that the command dropbox status has returned:

You're using an old version of Dropbox. Please update to the latest version to continue using Dropbox.

The syncronization still works, therefore I will not upgrade it. Last time symlink support was lost.

7 Likes