Answer the question
In order to leave comments, you need to log in
Please help me fix bash script
Please help me fix the bash script that backups files and databases and sends them to the dropbox server. The error appears when the file is loaded. I guess he can't determine the path to the directory normally
. Here is the script itself:
#!/bin/bash
DROPBOX_USER="Your Dropbox username"
DROPBOX_PASS="Your Dropbox password"
DROPBOX_DIR="Directory in your dropbox account to store the backups, e.g. /backups"
BACKUP_SRC="/home /var/www /var/git /etc /root"
BACKUP_DST="/tmp"
MYSQL_SERVER="127.0.0.1"
MYSQL_USER="root"
MYSQL_PASS="Your MySQL password"
#
# Stop editing here.
NOW=$(date +"%Y.%m.%d")
DESTFILE="$BACKUP_DST/$NOW.tgz"
#
# Upload a file to Dropbox.
# $1 = Source file
# $2 = Destination file.
function dropboxUpload
{
#
# Code based on DropBox Uploader 0.6 from www.andreafabrizi.it/?dropbox_uploader
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"
UPLOAD_FILE=$1
DEST_FOLDER=$2
# Login
echo -ne " > Logging in..."
curl -s -i -c $COOKIE_FILE -o $RESPONSE_FILE --data "login_email=$DROPBOX_USER&login_password=$DROPBOX_PASS&t=$TOKEN" "$LOGIN_URL"
grep "location: /home" $RESPONSE_FILE > /dev/null
if [ $? -ne 0 ]; then
echo -e " Failed!"
rm -f "$COOKIE_FILE" "$RESPONSE_FILE"
exit 1
else
echo -e " OK"
fi
# Load home page
echo -ne " > Loading Home..."
curl -s -i -b "$COOKIE_FILE" -o "$RESPONSE_FILE" "$HOME_URL"
if [ $? -ne 0 ]; then
echo -e " Failed!"
rm -f "$COOKIE_FILE" "$RESPONSE_FILE"
exit 1
else
echo -e " OK"
fi
# Get token
TOKEN=$(cat "$RESPONSE_FILE" | tr -d '\n' | sed 's/.*<form action="https:\/\/dl-web.dropbox.com\/upload"[^>]*>\s*<input type="hidden" name="t" value="\([a-z 0-9]*\)".*/\1/')
# Upload file
echo -ne " > Uploading '$UPLOAD_FILE' to 'DROPBOX$DEST_FOLDER/'..."
curl -s -i -b $COOKIE_FILE -o $RESPONSE_FILE -F "plain=yes" -F "dest=$DEST_FOLDER" -F "t=$TOKEN" -F "[email protected]$UPLOAD_FILE" "$UPLOAD_URL"
grep "HTTP/1.1 302 FOUND" "$RESPONSE_FILE" > /dev/null
if [ $? -ne 0 ]; then
echo -e " Failed!"
rm -f "$COOKIE_FILE" "$RESPONSE_FILE"
exit 1
else
echo -e " OK"
rm -f "$COOKIE_FILE" "$RESPONSE_FILE"
fi
}
# Backup files.
mysqldump -u $MYSQL_USER -h $MYSQL_SERVER -p$MYSQL_PASS --all-databases > "$NOW-Databases.sql"
tar cfz "$DESTFILE" $BACKUP_SRC "$NOW-Databases.sql"
dropboxUpload "$DESTFILE" "$DROPBOX_DIR"
rm -f "$NOW-Databases.sql" "$DESTFILE"
Answer the question
In order to leave comments, you need to log in
DROPBOX_DIR="Directory in your dropbox account to store the backups, eg /backups"
Make the comments correct, otherwise I'll think it's a bug.
I don’t understand something where $RANDOM is defined or is there such a variable by default?
Greetings!
In the code, the “dog” in the parameter confuses "[email protected]$UPLOAD_FILE"
. Isn't that the problem?
By the way, if the target platform allows, then you can use the Dropbox Python client for the exchange , thereby saving on maintaining the script.
You write the results of curl to $RESPONSE_FILE:
curl -s -i -b $COOKIE_FILE -o $RESPONSE_FILE
And then check if "HTTP/1.1 302 FOUND" is there:
grep "HTTP/1.1 302 FOUND" "$RESPONSE_FILE" > /dev/null
I think the first step is to see what is actually in $RESPONSE_FILE after the script has run.
Obviously the reason is the file size. Dropbox itself reports that the limit for this method is 300 meters. This is confirmed by experiment with this script and "dd".
In this (well, or in a confusingly similar) script, an unpleasant feature was discovered - he, a meadow dog, safely sends backups to dropbox, but all this is not synchronized. Those. if you clean up irrelevant archives on the server, then in the dropbox it all just accumulates and eventually smells very bad. Who will say how to crush the viper and cure the disease?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question