Skip to content

Instantly share code, notes, and snippets.

@maulanasatyaadi
Last active September 27, 2022 15:11
Show Gist options
  • Save maulanasatyaadi/a11e0e13c6f10aa1d57a11e85d81c8e5 to your computer and use it in GitHub Desktop.
Save maulanasatyaadi/a11e0e13c6f10aa1d57a11e85d81c8e5 to your computer and use it in GitHub Desktop.
#!/bin/bash
while getopts u:p:h:d:t: option
do
case "${option}"
in
u) USER=${OPTARG};;
p) PASSWORD=${OPTARG};;
d) DATABASE=${OPTARG};;
h) HOST=${OPTARG};;
t) TEMPLATEID=${OPTARG};;
esac
done
if [[ -z $USER || -z $PASSWORD || -z $DATABASE || -z $HOST || -z $TEMPLATEID ]]; then
echo "Colibri importer
Help:
-u user
-p password
-d database
-h host
-t template id
usage example: ./colibriimport.sh -u mysquser -p'mypassword' -h localhost -d mydatabase -t mytemplateid"
exit 0
fi
/usr/bin/mysql -u $USER -p$PASSWORD -h $HOST -D $DATABASE -e "INSERT INTO colibri_export_status (templateId,status,created_at)
VALUES (\"${TEMPLATEID}\",\"Started\",NOW())
ON DUPLICATE KEY UPDATE status=VALUES(status), created_at=VALUES(created_at);"
if [[ $? == 1 ]]; then
echo -e "Connection rejected!"
exit 1
fi
mysqldump -u $USER -p$PASSWORD -h $HOST $DATABASE > "${TEMPLATEID}.sql"
if [[ $? == 1 ]]; then
echo -e "Connection rejected!\nFailed to dump database!"
exit 1
fi
tar czf ${TEMPLATEID}.tar.gz ${TEMPLATEID}.sql
if [[ $? == 1 ]]; then
echo -e "Failed to compress the file!"
exit 1
fi
/usr/bin/mysql -u $USER -p$PASSWORD -h $HOST -D $DATABASE -e "INSERT INTO colibri_export_status (templateId,status,created_at)
VALUES (\"${TEMPLATEID}\",\"Done\",NOW())
ON DUPLICATE KEY UPDATE status=VALUES(status), created_at=VALUES(created_at);"
if [[ $? == 1 ]]; then
echo -e "Connection rejected!"
exit 1
fi
echo "Operation success!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment