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.

Prerequisite

  1. Dropbox account (Create at https://www.dropbox.com/)
  2. curl installed on system

Get Your Dropbox API Access Token

  1. Go to https://www.dropbox.com/developers/apps/create
  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 backup.sh script

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

#!/bin/bash
BACKUP_FILES='Dir-To-Backup';
DATABASE='Database-To-Backup';
DB_USER='Database-Username';
DB_PASS='Database-Password';
DROPBOX='/backup/';
DROPBOX_KEY='Your-Dropbox-Access-Token';
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 https://content.dropboxapi.com/2/files/upload \
    --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 backup.sh executable using following command.

chmod +x backup.sh

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

./backup.sh

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

Automate backup.sh 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 * * * /Path-to-Backup.sh
  • For Weekly 0 0 * * 0 /Path-to-Backup.sh
  • To backup every 15 days 0 0 */15 * * /Path-to-Backup.sh
  • For monthly 0 0 1 * * /Path-to-Backup.sh

Replace /Path-to-Backup.sh with absolute path of your backup script eg /home/sunny/backup.sh.


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 *