Backup Website to Dropbox


/ Published in: Bash
Save to your folder(s)

Backup your website folder and mySQL Database to dropbox


Copy this code and paste it in your HTML
  1. #!/bin/bash
  2. #
  3. # Website Backup Script
  4. #
  5. # Copyright (c) 2011 Keiran "affix" Smith <[email protected]>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. #
  21.  
  22. # BASIC CONFIG
  23. FOLDER=""
  24. MYSQLDB=""
  25. MYSQLUSER=""
  26. MYSQLPASS=""
  27.  
  28. # DROPBOX CONFIG
  29. LOGIN_EMAIL=""
  30. LOGIN_PASSWD=""
  31. END_ON_UPLOAD_ERROR=1
  32. SKIP_LOADING_PAGE=1
  33.  
  34. ## DO NOT EDIT BELOW THIS LINE ##
  35.  
  36. LOGIN_URL="https://www.dropbox.com/login"
  37. HOME_URL="https://www.dropbox.com/home"
  38. UPLOAD_URL="https://dl-web.dropbox.com/upload"
  39. COOKIE_FILE="/tmp/du_cookie_$RANDOM"
  40. RESPONSE_FILE="/tmp/du_resp_$RANDOM"
  41. BIN_DEPS="curl sed grep tr pwd"
  42.  
  43. DATE=$(date '+%d-%m-%y')
  44.  
  45. function remove_temp_files
  46. {
  47. rm -rf $COOKIE_FILE
  48. rm -rf $RESPONSE_FILE
  49. }
  50.  
  51. function get_token
  52. {
  53. TOKEN=$(cat $1 | tr -d '\n' | sed 's/.*<form action="'$2'"[^>]*>\s*<input type="hidden" name="t" value="\([a-z 0-9]*\)".*/\1/')
  54. echo $TOKEN
  55. }
  56.  
  57. function dropbox_upload
  58. {
  59. UPLOAD_FILE=$1
  60. DEST_FOLDER=$2
  61.  
  62. 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"
  63. grep "HTTP/1.1 302 FOUND" "$RESPONSE_FILE" > /dev/null
  64.  
  65. if [ $? -ne 0 ]; then
  66. if [ $END_ON_UPLOAD_ERROR -eq 1 ]; then
  67. remove_temp_files
  68. exit 1
  69. fi
  70. fi
  71. }
  72.  
  73. function do_backup
  74. {
  75. mkdir /tmp/$DATE
  76. mkdir /tmp/$DATE/html
  77. mkdir /tmp/$DATE/mysql
  78. cp -R $FOLDER /tmp/$DATE/html
  79. mysqldump -u $MYSQLUSER -p$MYSQLPASS $MYSQLDB > /tmp/$DATE/mysql/$MYSQLDB-$DATE.sql
  80. tar -pczf /tmp/backup-$DATE.tar.gz /tmp/$DATE > /dev/null
  81. }
  82.  
  83. for i in $BIN_DEPS; do
  84. which $i > /dev/null
  85. if [ $? -ne 0 ]; then
  86. remove_temp_files
  87. exit 1
  88. fi
  89. done
  90.  
  91. #LOAD LOGIN PAGE
  92. if [ $SKIP_LOADING_LOGIN_PAGE -eq 0 ]; then
  93. curl -s -i -o "$RESPONSE_FILE" "$LOGIN_URL"
  94.  
  95. if [ $? -ne 0 ]; then
  96. remove_temp_files
  97. exit 1
  98. fi
  99.  
  100. #GET TOKEN
  101. TOKEN=$(get_token "$RESPONSE_FILE" "\/login")
  102. #echo -e " > Token = $TOKEN"
  103. if [ "$TOKEN" == "" ]; then
  104. remove_temp_files
  105. exit 1
  106. fi
  107. fi
  108.  
  109. #LOGIN
  110. curl -s -i -c $COOKIE_FILE -o $RESPONSE_FILE --data "login_email=$LOGIN_EMAIL&login_password=$LOGIN_PASSWD&t=$TOKEN" "$LOGIN_URL"
  111. grep "location: /home" $RESPONSE_FILE > /dev/null
  112.  
  113. if [ $? -ne 0 ]; then
  114. remove_temp_files
  115. exit 1
  116. fi
  117.  
  118. #LOAD HOME
  119. curl -s -i -b "$COOKIE_FILE" -o "$RESPONSE_FILE" "$HOME_URL"
  120.  
  121. if [ $? -ne 0 ]; then
  122. remove_temp_files
  123. exit 1
  124. fi
  125.  
  126. #GET TOKEN
  127. TOKEN=$(get_token "$RESPONSE_FILE" "https:\/\/dl-web.dropbox.com\/upload")
  128. #echo -e " > Token = $TOKEN"
  129. if [ "$TOKEN" == "" ]; then
  130. remove_temp_files
  131. exit 1
  132. fi
  133.  
  134. do_backup
  135. dropbox_upload "/tmp/backup-$DATE.tar.gz" "/"

URL: http://affix.me

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.