Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andcam/58d5f3bae741d491d2fa to your computer and use it in GitHub Desktop.
Save andcam/58d5f3bae741d491d2fa to your computer and use it in GitHub Desktop.
update to remove newline chars
#!/bin/bash
#for file names
WDAY=`/bin/date +%w`
FDATE=`/bin/date +%Y%m%d`
BACKUPPATH="/tmp"
WEBDIR="/var/www"
SITES=`ls $WEBDIR`
# MySQL server details
MYSQL_USER="backup"
MYSQL_PASS="password"
MYSQL_PORT="3306"
MYSQL_SERVER="localhost"
# google drive
# config
BROWSER_AGENT="Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:13.0) Gecko/20100101 Firefox/13.0.1"
GOOGLE_USER="[email protected]"
# user or app specific password (if using 2-factor auth)
GOOGLE_PASSWORD="password"
GOOGLE_ACCOUNTTYPE="HOSTED" #gooApps = HOSTED , gmail=GOOGLE
FILES=""
# folder ID, can be found in URL when viewing drive.google.com
FOLDER_ID="7BycRryeHyj-bYkl2R1p8alpqYU1"
FOLDER="/folder:$FOLDER_ID/contents"
# default vars
BATCH_CURRENT_DIRECTORY=$(pwd)
BATCH_CMD_LINE_ARGS="$@"
BATCH_PROCESS_ID="$$"
BATCH_UNIX_USER_ID=$(id)
CURL_PATH="/usr/bin/curl"
CURL_OPTS="-S -k"
TMP_DIR="/tmp"
# google auth
LOGIN=$($CURL_PATH --data-urlencode Email=$GOOGLE_USER --data-urlencode Passwd=$GOOGLE_PASSWORD \
-d accountType=$GOOGLE_ACCOUNTTYPE -d service=writely \
-d source=cURL "https://www.google.com/accounts/ClientLogin")
TOKEN=$(echo "$LOGIN" | grep Auth | cut -d \= -f 2)
function upload {
find "$1" -type f -print0 | while IFS= read -r -d $'\0' FILE; do
FILENAME=$(basename $FILE)
UPLOAD_LINK=$($CURL_PATH $CURL_OPTS --request POST -H "Content-Length: 0" \
-H "Authorization: GoogleLogin auth=${TOKEN}" \
-H "GData-Version: 3.0" \
-H "Content-Type: $(file --mime-type --brief $FILE)" \
-H "Slug: $FILENAME" "https://docs.google.com/feeds/upload/create-session/default/private/full$FOLDER?convert=false" \
-D /dev/stdout | grep "Location:" | sed s/"Location: "// | tr -d '\r' )
$CURL_PATH $CURL_OPTS --request POST --data-binary "@$FILE" -H \
"Authorization: GoogleLogin auth=${TOKEN}" -H \
"GData-Version: 3.0" -H \
"Content-Type: $FILE_MIMETYPE" -H \
"Slug: $FILE" "$UPLOAD_LINK"
done
}
echo "## START BACK TO: $BACKUPPATH"
echo ""
echo "## START $WDAY: MYSQL ################################################"
# dump mysql and gzip it
FILES="$BACKUPPATH/mysql-$FDATE.tgz"
nice -n 19 mysqldump --all-databases -h ${MYSQL_SERVER} -P ${MYSQL_PORT} -u ${MYSQL_USER} -p${MYSQL_PASS} | gzip -1 > $FILES
# upload to google
upload $FILES
# delete it
rm -rf $FILES
echo "#### END: MYSQL ################################################"
echo "## START $WDAY: etc ################################################"
FILES="$BACKUPPATH/etc-$FDATE.tgz"
tar cfz $FILES /etc
# upload to google
upload $FILES
# delete it
rm -rf $FILES
echo "#### END: etc ################################################"
for SITE in $SITES
do
echo "## START $WDAY: $SITE ################################################"
FILES="$BACKUPPATH/$SITE-$FDATE.tgz"
# full backup
nice -n 19 tar -zcf $FILES $WEBDIR/$SITE
# upload to google
upload $FILES
# delete it
rm -rf $FILES
echo "#### END: $SITE ################################################"
done
@andcam
Copy link
Author

andcam commented Sep 29, 2014

note: does not diff, could be far more elegant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment