Skip to content

Instantly share code, notes, and snippets.

@maulanasatyaadi
Created September 29, 2022 07:37
Show Gist options
  • Save maulanasatyaadi/5e7d4190a7c79cb9e61f9364bb09614b to your computer and use it in GitHub Desktop.
Save maulanasatyaadi/5e7d4190a7c79cb9e61f9364bb09614b to your computer and use it in GitHub Desktop.
#!/usr/bin/bash
while getopts h:f:u:p: option
do
case "${option}"
in
h) HOST=${OPTARG};;
f) FILENAME=${OPTARG};;
u) MYSQL_USER=${OPTARG};;
p) MYSQL_PASSWORD=${OPTARG};;
esac
done
if [[ -z $HOST || -z $FILENAME || -z $MYSQL_USER || -z $MYSQL_PASSWORD]]; then
echo "-h host
-f file name
-u mysql username
-p mysql password
example: ./colbiriget -h https://www.target.com -f filename123 -u myuser -p'mypassword'"
exit 0
fi
echo "Getting the archive from host"
curl -L $HOST/upload/$FILENAME.tar.gz -o $FILENAME.tar.gz
if [[ $? === 1 ]]; then
echo "Failed to get the archive!"
exit 0
fi
echo "Extracting the archive"
tar xzf $FILENAME
if [[ $? === 1 ]]; then
echo "Failed to extract the archive!"
exit 0
fi
echo "Restoring the sql file to database"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD < $FILENAME.sql
if [[ $? === 1 ]]; then
echo "Failed to restore the database!"
exit 0
fi
echo "Cleaning the file"
rm -f $FILENAME.tar.gz $FILENAME.sql
if [[ $? === 1 ]]; then
echo "Failed to clean the file!"
exit 0
fi
echo "Operation success!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment