Return to Snippet

Revision: 50360
at August 21, 2011 03:18 by Affix


Updated Code
#!/bin/bash
#
# Website Backup Script
#
# Copyright (c) 2011 Keiran "affix" Smith <[email protected]>
#  
# 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.
#

# BASIC CONFIG
FOLDER=""
MYSQLDB=""
MYSQLUSER=""
MYSQLPASS=""

# DROPBOX CONFIG
LOGIN_EMAIL=""
LOGIN_PASSWD=""
END_ON_UPLOAD_ERROR=1
SKIP_LOADING_PAGE=1

## DO NOT EDIT BELOW THIS LINE ##

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"

DATE=$(date '+%d-%m-%y')

function remove_temp_files
{
	rm -rf $COOKIE_FILE
	rm -rf $RESPONSE_FILE
}

function get_token
{
    TOKEN=$(cat $1 | tr -d '\n' | sed 's/.*<form action="'$2'"[^>]*>\s*<input type="hidden" name="t" value="\([a-z 0-9]*\)".*/\1/')
    echo $TOKEN      
}

function dropbox_upload
{
    UPLOAD_FILE=$1
    DEST_FOLDER=$2
    
    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"
    grep "HTTP/1.1 302 FOUND" "$RESPONSE_FILE" > /dev/null

    if [ $? -ne 0 ]; then
        if [ $END_ON_UPLOAD_ERROR -eq 1 ]; then
            remove_temp_files
            exit 1
        fi
    fi
}

function do_backup
{
	mkdir /tmp/$DATE
	mkdir /tmp/$DATE/html
	mkdir /tmp/$DATE/mysql
	cp -R $FOLDER /tmp/$DATE/html
	mysqldump -u $MYSQLUSER -p$MYSQLPASS $MYSQLDB > /tmp/$DATE/mysql/$MYSQLDB-$DATE.sql
	tar -pczf /tmp/backup-$DATE.tar.gz /tmp/$DATE > /dev/null
}

for i in $BIN_DEPS; do
    which $i > /dev/null
    if [ $? -ne 0 ]; then
        remove_temp_files
        exit 1
    fi
done

#LOAD LOGIN PAGE
if [ $SKIP_LOADING_LOGIN_PAGE -eq 0 ]; then
    curl -s -i -o "$RESPONSE_FILE" "$LOGIN_URL"

    if [ $? -ne 0 ]; then
        remove_temp_files
        exit 1
    fi

    #GET TOKEN
    TOKEN=$(get_token "$RESPONSE_FILE" "\/login")
    #echo -e " > Token = $TOKEN"
    if [ "$TOKEN" == "" ]; then
        remove_temp_files
        exit 1
    fi
fi

#LOGIN
curl -s -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
    remove_temp_files
    exit 1
fi

#LOAD HOME
curl -s -i -b "$COOKIE_FILE" -o "$RESPONSE_FILE" "$HOME_URL"

if [ $? -ne 0 ]; then
    remove_temp_files
    exit 1
fi

#GET TOKEN
TOKEN=$(get_token "$RESPONSE_FILE" "https:\/\/dl-web.dropbox.com\/upload")
#echo -e " > Token = $TOKEN"
if [ "$TOKEN" == "" ]; then
    remove_temp_files
    exit 1
fi

do_backup
dropbox_upload "/tmp/backup-$DATE.tar.gz" "/"

Revision: 50359
at August 20, 2011 02:28 by Affix


Updated Code
#!/bin/bash
#
# Website Backup Script
#
# Copyright (c) 2011 Keiran "affix" Smith <[email protected]>
#  
# 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.
#

# BASIC CONFIG
FOLDER=""
MYSQLDB=""
MYSQLUSER=""
MYSQLPASS=""

# DROPBOX CONFIG
LOGIN_EMAIL=""
LOGIN_PASSWD=""
END_ON_UPLOAD_ERROR=1
SKIP_LOADING_PAGE=1

## DO NOT EDIT BELOW THIS LINE ##

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"

DATE=$(date '+%d-%m-%y')

function remove_temp_files
{
	rm -rf $COOKIE_FILE
	rm -rf $RESPONSE_FILE
}

function get_token
{
    TOKEN=$(cat $1 | tr -d '\n' | sed 's/.*<form action="'$2'"[^>]*>\s*<input type="hidden" name="t" value="\([a-z 0-9]*\)".*/\1/')
    echo $TOKEN      
}

function dropbox_upload
{
    UPLOAD_FILE=$1
    DEST_FOLDER=$2
    
    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"
    grep "HTTP/1.1 302 FOUND" "$RESPONSE_FILE" > /dev/null

    if [ $? -ne 0 ]; then
        if [ $END_ON_UPLOAD_ERROR -eq 1 ]; then
            remove_temp_files
            exit 1
        fi
    fi
}

function do_backup
{
	mkdir /tmp/$DATE
	mkdir /tmp/$DATE/html
	mkdir /tmp/$DATE/mysql
	cp -R $FOLDER /tmp/$DATE/html
	mysqldump -u $MYSQLUSER -p$MYSQLPASS $MYSQLDB > /tmp/$DATE/mysql/$MYSQLDB-$DATE.sql
	tar -pczf /tmp/backup-$DATE.tar.gz /tmp/$DATE > /dev/null
}

for i in $BIN_DEPS; do
    which $i > /dev/null
    if [ $? -ne 0 ]; then
        remove_temp_files
        exit 1
    fi
done

# GET TO WORK!!!

#CHECK FILE/DIR
if [ ! -r "$UPLOAD_FILE" ]; then
    echo "FILE ERROR!"
    remove_temp_files
    exit 1
fi

#LOAD LOGIN PAGE
if [ $SKIP_LOADING_LOGIN_PAGE -eq 0 ]; then
    curl -s -i -o "$RESPONSE_FILE" "$LOGIN_URL"

    if [ $? -ne 0 ]; then
        remove_temp_files
        exit 1
    fi

    #GET TOKEN
    TOKEN=$(get_token "$RESPONSE_FILE" "\/login")
    #echo -e " > Token = $TOKEN"
    if [ "$TOKEN" == "" ]; then
        remove_temp_files
        exit 1
    fi
fi

#LOGIN
curl -s -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
    remove_temp_files
    exit 1
fi

#LOAD HOME
curl -s -i -b "$COOKIE_FILE" -o "$RESPONSE_FILE" "$HOME_URL"

if [ $? -ne 0 ]; then
    remove_temp_files
    exit 1
fi

#GET TOKEN
TOKEN=$(get_token "$RESPONSE_FILE" "https:\/\/dl-web.dropbox.com\/upload")
#echo -e " > Token = $TOKEN"
if [ "$TOKEN" == "" ]; then
    remove_temp_files
    exit 1
fi

do_backup
dropbox_upload "/tmp/backup-$DATE.tar.gz" "/"

Revision: 50358
at August 18, 2011 22:10 by Affix


Initial Code
#!/bin/bash
#
# Website Backup Script
#
# Copyright (c) 2011 Keiran "affix" Smith <[email protected]>
#  
# 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.
#

# BASIC CONFIG
FOLDER=""
MYSQLDB=""
MYSQLUSER=""
MYSQLPASS=""

# DROPBOX CONFIG
LOGIN_EMAIL=""
LOGIN_PASSWD=""
END_ON_UPLOAD_ERROR=1
SKIP_LOADING_PAGE=1

## DO NOT EDIT BELOW THIS LINE ##

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"

DATE=$(date '+%d-%m-%y')

function remove_temp_files
{
	rm -rf $COOKIE_FILE
	rm -rf $RESPONSE_FILE
}

function get_token
{
    TOKEN=$(cat $1 | tr -d '\n' | sed 's/.*<form action="'$2'"[^>]*>\s*<input type="hidden" name="t" value="\([a-z 0-9]*\)".*/\1/')
    echo $TOKEN      
}

function dropbox_upload
{
    UPLOAD_FILE=$1
    DEST_FOLDER=$2
    
    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"
    grep "HTTP/1.1 302 FOUND" "$RESPONSE_FILE" > /dev/null

    if [ $? -ne 0 ]; then
        if [ $END_ON_UPLOAD_ERROR -eq 1 ]; then
            remove_temp_files
            exit 1
        fi
    fi
}

function do_backup
{
	mkdir /tmp/$DATE
	mkdir /tmp/$DATE/html
	mkdir /tmp/$DATE/mysql
	cp -R $FOLDER /tmp/$DATE/html
	mysqldump -u affix -p$MYSQLPASS $MYSQLDB > /tmp/$DATE/mysql/$MYSQLDB-$DATE.sql
	tar -pczf /tmp/backup-$DATE.tar.gz /tmp/$DATE > /dev/null
}

for i in $BIN_DEPS; do
    which $i > /dev/null
    if [ $? -ne 0 ]; then
        remove_temp_files
        exit 1
    fi
done

# GET TO WORK!!!

#CHECK FILE/DIR
if [ ! -r "$UPLOAD_FILE" ]; then
    echo "FILE ERROR!"
    remove_temp_files
    exit 1
fi

#LOAD LOGIN PAGE
if [ $SKIP_LOADING_LOGIN_PAGE -eq 0 ]; then
    curl -s -i -o "$RESPONSE_FILE" "$LOGIN_URL"

    if [ $? -ne 0 ]; then
        remove_temp_files
        exit 1
    fi

    #GET TOKEN
    TOKEN=$(get_token "$RESPONSE_FILE" "\/login")
    #echo -e " > Token = $TOKEN"
    if [ "$TOKEN" == "" ]; then
        remove_temp_files
        exit 1
    fi
fi

#LOGIN
curl -s -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
    remove_temp_files
    exit 1
fi

#LOAD HOME
curl -s -i -b "$COOKIE_FILE" -o "$RESPONSE_FILE" "$HOME_URL"

if [ $? -ne 0 ]; then
    remove_temp_files
    exit 1
fi

#GET TOKEN
TOKEN=$(get_token "$RESPONSE_FILE" "https:\/\/dl-web.dropbox.com\/upload")
#echo -e " > Token = $TOKEN"
if [ "$TOKEN" == "" ]; then
    remove_temp_files
    exit 1
fi

do_backup
dropbox_upload "/tmp/backup-$DATE.tar.gz" "/"

Initial URL
http://affix.me

Initial Description
Backup your website folder and mySQL Database to dropbox

Initial Title
Backup Website to Dropbox

Initial Tags

                                

Initial Language
Bash