goldensun 通过本文主要向大家介绍了shell脚本编程大全,shell脚本视频教程,shell脚本编程入门,shell脚本编程,linux shell脚本等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
看到有人用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 <andrea.fabrizi@gmail.com>
#
# 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 <LOCAL_FILE/DIR ...> <REMOTE_FILE/DIR>"
echo -e "\t download <REMOTE_FILE/DIR> [LOCAL_FILE/DIR]"
echo -e "\t delete <REMOTE_FILE/DIR>"
echo -e "\t move <REMOTE_FILE/DIR> <REMOTE_FILE/DIR>"
echo -e "\t copy <REMOTE_FILE/DIR> <REMOTE_FILE/DIR>"
echo -e "\t mkdir <REMOTE_DIR>"
echo -e "\t list [REMOTE_DIR]"
echo -e "\t share <REMOTE_FILE>"
echo -e "\t info"
echo -e "\t unlink"
echo -e "\nOptional parameters:"
echo -e "\t-f <FILENAME> 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,

