#!/bin/bash #你要修改的地方從這裡開始 MYSQL_USER= #mysql用戶名 MYSQL_PASS= #mysql密碼 MAIL_TO= #數據庫發送到的郵箱 FTP_USER= #ftp用戶名 FTP_PASS= #ftp密碼 FTP_IP= #ftp地址 FTP_backup= #ftp上存放備份文件的目錄,這個要自己得ftp上面建的 WEB_DATA= #要備份的網站數據 #DropBox Options LOGIN_EMAIL="" LOGIN_PASSWD="" DEST_FOLDER="/backup" #默認是Dropbox中的Backup目錄 #你要修改的地方從這裡結束 #定義數據庫的名字和舊數據庫的名字 DataBakName=Data_$(date +"%Y%m%d").tar.gz WebBakName=Web_$(date +%Y%m%d).tar.gz OldData=Data_$(date -d -5day +"%Y%m%d").tar.gz OldWeb=Web_$(date -d -5day +"%Y%m%d").tar.gz #刪除本地3天前的數據 rm -rf /home/backup/Data_$(date -d -3day +"%Y%m%d").tar.gz /home/backup/Web_$(date -d -3day +"%Y%m%d").tar.gz cd /home/backup #導出數據庫,一個數據庫一個壓縮文件 for db in `/usr/local/mysql/bin/mysql -u$MYSQL_USER -p$MYSQL_PASS -B -N -e 'SHOW DATABASES' | xargs`; do (/usr/local/mysql/bin/mysqldump -u$MYSQL_USER -p$MYSQL_PASS ${db} | gzip -9 - > ${db}.sql.gz) done #壓縮數據庫文件為一個文件 tar zcf /home/backup/$DataBakName /home/backup/*.sql.gz rm -rf /home/backup/*.sql.gz #發送數據庫到Email,如果數據庫壓縮後太大,請註釋這行 echo "主題:數據庫備份" | mutt -a /home/backup/$DataBakName -s "內容:數據庫備份" $MAIL_TO #壓縮網站數據 tar zcf /home/backup/$WebBakName $WEB_DATA ############################################################################################################################### #Set to 1 to enable DEBUG mode DEBUG=0 #Set to 1 to enable VERBOSE mode (-v option) VERBOSE=1 #If set to 1 the script terminate if an upload error occurs END_ON_UPLOAD_ERROR=0 #Set to 1 to skip the initial login page loading (Speed up the uploading process). #Set to 0 if you experience problems uploading the files. SKIP_LOADING_LOGIN_PAGE=1 #Don't edit these... 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" BIN_DEPS="curl sed grep tr pwd" VERSION="0.8.2" if [ $DEBUG -ne 0 ]; then set -x COOKIE_FILE="/tmp/du_cookie_debug" RESPONSE_FILE="/tmp/du_resp_debug" fi #Print verbose information depend on $VERBOSE variable function print { if [ $VERBOSE -eq 1 ]; then echo -ne "$1"; fi } #Remove temporary files function remove_temp_files { if [ $DEBUG -eq 0 ]; then rm -fr $COOKIE_FILE rm -fr $RESPONSE_FILE fi } #Extract token from the specified form # $1 = file path # $2 = form action function get_token { TOKEN=$(cat $1 | tr -d '\n' | sed 's/.*
]*>\s* Uploading '$UPLOAD_FILE' to 'DROPBOX$DEST_FOLDER'..." #Show the progress bar during the file upload if [ $VERBOSE -eq 1 ]; then CURL_PARAMETERS="--progress-bar" print "\n" else CURL_PARAMETERS="-s --show-error" fi curl $CURL_PARAMETERS -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 print " Failed!\n" if [ $END_ON_UPLOAD_ERROR -eq 1 ]; then remove_temp_files exit 1 fi else print " OK\n" fi } #Recursively upload a directory structure # $1 = remote destination folder function dropbox_upload_dir { for i in *; do if [ -f "$i" ]; then dropbox_upload "$i" "$1" fi if [ -d "$i" ]; then local OLD_PWD=$(pwd) cd "$i" dropbox_upload_dir "$1/$i" cd "$OLD_PWD" fi done } #Handles the keyboard interrupt (control-c) function ctrl_c { print "\n Bye ;)\n" remove_temp_files exit 1 } #Trap keyboard interrupt (control-c) trap ctrl_c SIGINT #CHECK DEPENDENCIES for i in $BIN_DEPS; do which $i > /dev/null if [ $? -ne 0 ]; then echo -e "Error: Required file could not be found: $i" remove_temp_files exit 1 fi done #USAGE function usage() { echo -e "Dropbox Uploader v$VERSION" echo -e "Usage: $0 [OPTIONS]..." echo -e "\nOptions:" echo -e "\t-u [USERNAME] (required if not hardcoded)" echo -e "\t-p [PASSWORD]" echo -e "\t-f [FILE/FOLDER] (required)" echo -e "\t-d [REMOTE_FOLDER] (default: /)" echo -e "\t-v Verbose mode" remove_temp_files } # File variables UPLOAD_FILE=/home/backup/$WebBakName UPLOAD_DATA=/home/backup/$DataBakName if [ "$LOGIN_EMAIL" == "" ]; then usage; exit 1; fi if [ "$DEST_FOLDER" == "" ]; then DEST_FOLDER="/" fi print "Dropbox Uploader v$VERSION\n" #CHECK FILE/DIR if [ ! -r "$UPLOAD_FILE" ]; then echo -e "Please specify a valid file or directory (-f)" remove_temp_files exit 1 fi #Prompt for password if [ "$LOGIN_PASSWD" == "" ]; then read -s -p "Password: " LOGIN_PASSWD echo fi #LOAD LOGIN PAGE if [ $SKIP_LOADING_LOGIN_PAGE -eq 0 ]; then print " > Loading Login Page..." curl -s --show-error -i -o "$RESPONSE_FILE" "$LOGIN_URL" if [ $? -ne 0 ]; then print " Failed!\n" remove_temp_files exit 1 else print " OK\n" fi #GET TOKEN TOKEN=$(get_token "$RESPONSE_FILE" "\/login") #echo -e " > Token = $TOKEN" if [ "$TOKEN" == "" ]; then print " Failed to get Authentication token!\n" remove_temp_files exit 1 fi fi #LOGIN print " > Logging in..." curl -s --show-error -i -c $COOKIE_FILE -o $RESPONSE_FILE --data "login_email=$LOGIN_EMAIL&login_password=$LOGIN_PASSWD&t=$TOKEN" "$LOGIN_URL" grep "location: /home" $RESPONSE_FILE > /dev/null if [ $? -ne 0 ]; then print " Failed!\n" remove_temp_files exit 1 else print " OK\n" fi #LOAD HOME print " > Loading Home..." curl -s --show-error -i -b "$COOKIE_FILE" -o "$RESPONSE_FILE" "$HOME_URL" if [ $? -ne 0 ]; then print " Failed!\n" remove_temp_files exit 1 else print " OK\n" fi #GET TOKEN TOKEN=$(get_token "$RESPONSE_FILE" "https:\/\/dl-web.dropbox.com\/upload") #echo -e " > Token = $TOKEN" if [ "$TOKEN" == "" ]; then print " Failed to get Upload token!\n" remove_temp_files exit 1 fi #If it's a single file... if [ -f "$UPLOAD_FILE" ]; then dropbox_upload "$UPLOAD_FILE" "$DEST_FOLDER" fi #If it's a dbfile file... if [ -f "$UPLOAD_DATA" ]; then dropbox_upload "$UPLOAD_DATA" "$DEST_FOLDER" fi #If it's a directory... if [ -d "$UPLOAD_FILE" ]; then OLD_PWD=$(pwd) cd "$UPLOAD_FILE" dropbox_upload_dir "$DEST_FOLDER" cd "$OLD_PWD" fi remove_temp_files ################################################################################################################################## #上傳到FTP空間,刪除FTP空間5天前的數據 ftp -v -n $FTP_IP << END user $FTP_USER $FTP_PASS type binary cd $FTP_backup delete $OldData delete $OldWeb put $DataBakName put $WebBakName bye END