Simple bash script to backup directory and MySQL database to Dropbox

Backup of important data or website is crucial if a system crash or hard drive failure occurs. There many software are available on internet to backup data. Some of those are paid or not easy to use, So I decided to create my own bash script to backup MySQL database and directories on Linux. This script also uploads backups to Dropbox account.


  1. Dropbox account (Create at
  2. curl installed on system

Get Your Dropbox API Access Token

  1. Go to
  2. Choose Dropbox API
  3. Choose the type of access Full Dropbox
  4. Enter unique name of your app
  5. Then click create app button
Dropbox create app

Then click Generated access token button. And copy your access token. This token will be used in script to upload file to Dropbox.

Dropbox Generated access token

Create script

Go to Linux command line (use ssh to connect remote machine). Create file and paste following script in it

FILENAME=$(date +backup_%d-%m-%Y.tar.gz);
cd /tmp;
tar czf file.tar.gz --absolute-names ${BACKUP_FILES};
export MYSQL_PWD=${DB_PASS};
mysqldump -u${DB_USER} --databases ${DATABASE} >database.sql
tar czf ${FILENAME} --remove-files database.sql file.tar.gz
curl -s --output -X POST \
    --header "Authorization: Bearer ${DROPBOX_KEY}" \
    --header "Dropbox-API-Arg: {\"path\": \"${DROPBOX}${FILENAME}\"}" \
    --header "Content-Type: application/octet-stream" \
    --data-binary @${FILENAME}

Replace Dir-To-Backup with a directory you want to backup you can add multiple directories separated by space always use absolute path. Replace Database-To-Backup with MySQL database you want to backup you can add multiple separated by space. Next enter MySQL database username and password in DB_USER and DB_PASS variables respectively. Finally replace Your-Dropbox-Access-Token with you access token generated in first step.

Make executable using following command.

chmod +x

Now your backup script is ready use. To test this script run this command


Then open your Dropbox backup folder to check if new backup exist.

Automate via crontab

You can run this script automatically via Linux crontab. To do this edit crontab with crontab -e command and paste one of the following line in it.

  • To run daily 0 0 * * * /
  • For Weekly 0 0 * * 0 /
  • To backup every 15 days 0 0 */15 * * /
  • For monthly 0 0 1 * * /

Replace / with absolute path of your backup script eg /home/sunny/

Liked It? Get Free updates in your Email

Delivered by feedburner

Leave a Reply

Your email address will not be published. Required fields are marked *