您现在的位置是:网站首页> 编程资料编程资料
编写shell脚本将VPS上的数据备份到Dropbox网盘的方法_linux shell_
2023-05-26
570人已围观
简介 编写shell脚本将VPS上的数据备份到Dropbox网盘的方法_linux shell_
看到有人用dropbox备份网站数据,所以今天也试了一下,记得以前是一个python脚本,这是用的是bash 脚本,利用dropbox的api来上传下载的,很方便,脚本的地址是Dropbox-Uploader/dropbox_uploader.sh at master · andreafabrizi/Dropbox-Uploader · GitHub ,感谢作者分享这个脚本。
可以到git下载dropbox_uploader.sh,地址为:https://github.com/andreafabrizi/Dropbox-Uploader
或者也可以直接拷贝代码,保存为dropbox_uploader.sh,注意拷贝的时候最好是复制到文本编辑器里面,如notepad++之类的
#!/usr/bin/env bash # # Dropbox Uploader # # Copyright (C) 2010-2014 Andrea Fabrizi# # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # #Default configuration file CONFIG_FILE=~/.dropbox_uploader #Default chunk size in Mb for the upload process #It is recommended to increase this value only if you have enough free space on your /tmp partition #Lower values may increase the number of http requests CHUNK_SIZE=4 #Curl location #If not set, curl will be searched into the $PATH #CURL_BIN="/usr/bin/curl" #Default values TMP_DIR="/tmp" DEBUG=0 QUIET=0 SHOW_PROGRESSBAR=0 SKIP_EXISTING_FILES=0 ERROR_STATUS=0 #Don't edit these... API_REQUEST_TOKEN_URL="https://api.dropbox.com/1/oauth/request_token" API_USER_AUTH_URL="https://www2.dropbox.com/1/oauth/authorize" API_ACCESS_TOKEN_URL="https://api.dropbox.com/1/oauth/access_token" API_CHUNKED_UPLOAD_URL="https://api-content.dropbox.com/1/chunked_upload" API_CHUNKED_UPLOAD_COMMIT_URL="https://api-content.dropbox.com/1/commit_chunked_upload" API_UPLOAD_URL="https://api-content.dropbox.com/1/files_put" API_DOWNLOAD_URL="https://api-content.dropbox.com/1/files" API_DELETE_URL="https://api.dropbox.com/1/fileops/delete" API_MOVE_URL="https://api.dropbox.com/1/fileops/move" API_COPY_URL="https://api.dropbox.com/1/fileops/copy" API_METADATA_URL="https://api.dropbox.com/1/metadata" API_INFO_URL="https://api.dropbox.com/1/account/info" API_MKDIR_URL="https://api.dropbox.com/1/fileops/create_folder" API_SHARES_URL="https://api.dropbox.com/1/shares" APP_CREATE_URL="https://www2.dropbox.com/developers/apps" RESPONSE_FILE="$TMP_DIR/du_resp_$RANDOM" CHUNK_FILE="$TMP_DIR/du_chunk_$RANDOM" TEMP_FILE="$TMP_DIR/du_tmp_$RANDOM" BIN_DEPS="sed basename date grep stat dd mkdir" VERSION="0.14" umask 077 #Check the shell if [ -z "$BASH_VERSION" ]; then echo -e "Error: this script requires the BASH shell!" exit 1 fi shopt -s nullglob #Bash allows filename patterns which match no files to expand to a null string, rather than themselves shopt -s dotglob #Bash includes filenames beginning with a "." in the results of filename expansion #Look for optional config file parameter while getopts ":qpskdf:" opt; do case $opt in f) CONFIG_FILE=$OPTARG ;; d) DEBUG=1 ;; q) QUIET=1 ;; p) SHOW_PROGRESSBAR=1 ;; k) CURL_ACCEPT_CERTIFICATES="-k" ;; s) SKIP_EXISTING_FILES=1 ;; \?) echo "Invalid option: -$OPTARG" >&2 exit 1 ;; :) echo "Option -$OPTARG requires an argument." >&2 exit 1 ;; esac done if [[ $DEBUG != 0 ]]; then echo $VERSION set -x RESPONSE_FILE="$TMP_DIR/du_resp_debug" fi if [[ $CURL_BIN == "" ]]; then BIN_DEPS="$BIN_DEPS curl" CURL_BIN="curl" fi #Dependencies check which $BIN_DEPS > /dev/null if [[ $? != 0 ]]; then for i in $BIN_DEPS; do which $i > /dev/null || NOT_FOUND="$i $NOT_FOUND" done echo -e "Error: Required program could not be found: $NOT_FOUND" exit 1 fi #Check if readlink is installed and supports the -m option #It's not necessary, so no problem if it's not installed which readlink > /dev/null if [[ $? == 0 && $(readlink -m "//test" 2> /dev/null) == "/test" ]]; then HAVE_READLINK=1 else HAVE_READLINK=0 fi #Forcing to use the builtin printf, if it's present, because it's better #otherwise the external printf program will be used #Note that the external printf command can cause character encoding issues! builtin printf "" 2> /dev/null if [[ $? == 0 ]]; then PRINTF="builtin printf" PRINTF_OPT="-v o" else PRINTF=$(which printf) if [[ $? != 0 ]]; then echo -e "Error: Required program could not be found: printf" fi PRINTF_OPT="" fi #Print the message based on $QUIET variable function print { if [[ $QUIET == 0 ]]; then echo -ne "$1"; fi } #Returns unix timestamp function utime { echo $(date +%s) } #Remove temporary files function remove_temp_files { if [[ $DEBUG == 0 ]]; then rm -fr "$RESPONSE_FILE" rm -fr "$CHUNK_FILE" rm -fr "$TEMP_FILE" fi } #Returns the file size in bytes # generic GNU Linux: linux-gnu # windows cygwin: cygwin # raspberry pi: linux-gnueabihf # macosx: darwin10.0 # freebsd: FreeBSD # qnap: linux-gnueabi # iOS: darwin9 function file_size { #Some embedded linux devices if [[ $OSTYPE == "linux-gnueabi" || $OSTYPE == "linux-gnu" ]]; then stat -c "%s" "$1" return #Generic Unix elif [[ ${OSTYPE:0:5} == "linux" || $OSTYPE == "cygwin" || ${OSTYPE:0:7} == "solaris" ]]; then stat --format="%s" "$1" return #BSD, OSX and other OSs else stat -f "%z" "$1" return fi } #Usage function usage { echo -e "Dropbox Uploader v$VERSION" echo -e "Andrea Fabrizi - andrea.fabrizi@gmail.com\n" echo -e "Usage: $0 COMMAND [PARAMETERS]..." echo -e "\nCommands:" echo -e "\t upload " echo -e "\t download [LOCAL_FILE/DIR]" echo -e "\t delete " echo -e "\t move " echo -e "\t copy " echo -e "\t mkdir " echo -e "\t list [REMOTE_DIR]" echo -e "\t share " echo -e "\t info" echo -e "\t unlink" echo -e "\nOptional parameters:" echo -e "\t-f Load the configuration file from a specific file" echo -e "\t-s Skip already existing files when download/upload. Default: Overwrite" echo -e "\t-d Enable DEBUG mode" echo -e "\t-q Quiet mode. Don't show messages" echo -e "\t-p Show cURL progress meter" echo -e "\t-k Doesn't check for SSL certificates (insecure)" echo -en "\nFor more info and examples, please see the README file.\n\n" remove_temp_files exit 1 } #Check the curl exit code function check_http_response { CODE=$? #Checking curl exit code case $CODE in #OK 0) ;; #Proxy error 5) print "\nError: Couldn't resolve proxy. The given proxy host could not be resolved.\n" remove_temp_files exit 1 ;; #Missing CA certificates 60|58) print "\nError: cURL is not able to performs peer SSL certificate verification.\n" print "Please, install the default ca-certificates bundle.\n" print "To do this in a Debian/Ubuntu based system, try:\n" print " sudo apt-get install ca-certificates\n\n" print "If the problem persists, try to use the -k option (insecure).\n" remove_temp_files exit 1 ;; 6) print "\nError: Couldn't resolve host.\n" remove_temp_files exit 1 ;; 7) print "\nError: Couldn't connect to host.\n" remove_temp_files exit 1 ;; esac #Checking response file for generic errors if grep -q "HTTP/1.1 400" "$RESPONSE_FILE"; then ERROR_MSG=$(sed -n -e 's/{"error": "\([^"]*\)"}/\1/p' "$RESPONSE_FILE") case $ERROR_MSG in *access?attempt?failed?because?this?app?is?not?configured?to?have*) echo -e "\nError: The Permission type/Access level configured doesn't match the DropBox App settings!\nPlease run \"$0 unlink\" and try again." exit 1 ;; esac fi } #Urlencode function urlencode { local string="${1}" local strlen=${#string} local encoded="" for (( pos=0 ; pos /dev/null check_http_response #Even if the file/dir has been deleted from DropBox we receive a 200 OK response #So we must check if the file exists or if it has been deleted if grep -q "\"is_deleted\":" "$RESPONSE_FILE"; then local IS_DELETED=$(sed -n 's/.*"is_deleted":.\([^,]*\).*/\1/p' "$RESPONSE_FILE") else local IS_DELETED="false" fi #Exits... grep -q "^HTTP/1.1 200 OK" "$RESPONSE_FILE" if [[ $? == 0 && $IS_DELETED != "true" ]]; then local IS_DIR=$(sed -n 's/^\(.*\)\"contents":.\[.*/\1/p' "$RESPONSE_FILE") #It's a directory if [[ $IS_DIR != "" ]]; then echo "DIR" #It's a file else echo "FILE" fi #Doesn't exists else echo "ERR" fi } #Generic upload wrapper around db_upload_file and db_upload_dir functions #$1 = Local source file/dir #$2 = Remote destination file/dir function db_upload { local SRC=$(normalize_path "$1") local DST=$(normalize_path "$2") #Checking if the file/dir exists if [[ ! -e $SRC && ! -d $SRC ]]; then print " > No such file or directory: $SRC\n" ERROR_STATUS=1 return fi #Checking if the file/dir has read permissions if [[ ! -r $SRC ]]; then print " > Error reading file $SRC: permission denied\n" ERROR_STATUS=1 return fi #Checking if DST it's a folder or if it doesn' exists (in this case will be the destination name) TYPE=$(db_stat "$DST") if [[ $TYPE == "DIR" ]]; then local filename=$(basename "$SRC") DST="$DST/$filename" fi #It's a directory if [[ -d $SRC ]]; then db
相关内容
- 一波实用的Bash Shell整理_linux shell_
- 简介Linux中cp和mv搭配{,}在shell当中的用法_linux shell_
- Shell脚本实现的基于SVN的代码提交量统计工具_linux shell_
- Linux bash Shell中的变量类型详解_linux shell_
- Shell中if的基本语法和常见判断用法_linux shell_
- Shell脚本实现判断IP地址是否在一个ip段内代码分享_linux shell_
- php-fpm开机自动启动Shell脚本_linux shell_
- Shell脚本中引用、调用另一个脚本文件的2种方法_linux shell_
- Shell中判断字符串是否为数字的6种方法分享_linux shell_
- Linux Shell脚本查看NUMA信息_linux shell_
点击排行
本栏推荐
