Script for backing up server data to dropbox in LINUX

Source: Internet
Author: User
Tags curl
The code is as follows: Copy code

#! /Bin/bash
DROPBOX_USER = "Your Dropbox username"
DROPBOX_PASS = "Your Dropbox password"
DROPBOX_DIR = "Directory in your dropbox account to store the backups, e.g./backups"
BACKUP_SRC = "/home/var/www/var/git/etc/root"
BACKUP_DST = "/tmp"
MYSQL_SERVER = "127.0.0.1 & Prime;
MYSQL_USER = "root"
MYSQL_PASS = "Your MySQL password"

#
# Stop editing here.
NOW = $ (date + "% Y. % m. % d ")
DESTFILE = "$ BACKUP_DST/$ NOW. tgz"

#
# Upload a file to Dropbox.
#$1 = Source file
#$2 = Destination file.
Function dropboxUpload
{
#
# Code based on DropBox Uploader 0.6 from http://www.andreafabrizi.it /? Dropbox_uploader
LOGIN_URL = "https://www.dropbox.com/login"
HOME_URL = "https://www.dropbox.com/home"
UPLOAD_URL = "https://dl-web.dropbox.com/upload"
COOKIE_FILE = "/tmp/du_cookie _ $ RANDOM"
RESPONSE_FILE = "/tmp/du_resp _ $ RANDOM"

UPLOAD_FILE = $1
DEST_FOLDER = $2

# Login
Echo-ne "> Logging in ..."
Curl-s-I-c $ COOKIE_FILE-o $ RESPONSE_FILE-data "login_email = $ DROPBOX_USER & login_password = $ DROPBOX_PASS & t = $ TOKEN" "$ LOGIN_URL"
Grep "location:/home" $ RESPONSE_FILE>/dev/null

If [$? -Ne 0]; then
Echo-e "Failed !"
Rm-f "$ COOKIE_FILE" "$ RESPONSE_FILE"
Exit 1
Else
Echo-e "OK"
Fi

# Load home page
Echo-ne "> Loading Home ..."
Curl-s-I-B "$ COOKIE_FILE"-o "$ RESPONSE_FILE" "$ HOME_URL"

If [$? -Ne 0]; then
Echo-e "Failed !"
Rm-f "$ COOKIE_FILE" "$ RESPONSE_FILE"
Exit 1
Else
Echo-e "OK"
Fi

# Get token
TOKEN = $ (cat "$ RESPONSE_FILE" | tr-d 'n' | sed's /. * <form action = "https://dl-web.dropbox.com/upload?#^>] *> s * <input type =" hidden "name =" t "value =" ([a-z 0-9] *) ". */1 /')

# Upload file
Echo-ne "> Uploading '$ UPLOAD_FILE' to 'dropbox $ DEST_FOLDER /'..."
Curl-s-I-B $ COOKIE_FILE-o $ RESPONSE_FILE-F "plain = yes"-F "dest = $ DEST_FOLDER"-F "t = $ TOKEN"-F "file = @ $ UPLOAD_FILE "" $ UPLOAD_URL"
Grep "HTTP/1.1 302 FOUND" "$ RESPONSE_FILE">/dev/null

If [$? -Ne 0]; then
Echo-e "Failed !"
Rm-f "$ COOKIE_FILE" "$ RESPONSE_FILE"
Exit 1
Else
Echo-e "OK"
Rm-f "$ COOKIE_FILE" "$ RESPONSE_FILE"
Fi
}

# Backup files.
Mysqldump-u $ MYSQL_USER-h $ MYSQL_SERVER-p $ MYSQL_PASS-all-databases> "$ NOW-Databases. SQL"
Tar cfz "$ DESTFILE" $ BACKUP_SRC "$ NOW-Databases. SQL"

DropboxUpload "$ DESTFILE" "$ DROPBOX_DIR"

Rm-f '$ NOW-Databases. SQL' $ DESTFILE"

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.