Skip to content

Instantly share code, notes, and snippets.

@numb95
Created April 16, 2017 07:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numb95/dbb2fdcb0ae9d8cf6a208af9f2ac0888 to your computer and use it in GitHub Desktop.
Save numb95/dbb2fdcb0ae9d8cf6a208af9f2ac0888 to your computer and use it in GitHub Desktop.
#!/bin/sh
# init. the projects informations
PROJECT=PROJECT_NAME
FILE=$PROJECT.`jdate +%Y-%m-%d`.sql
DBSERVER=DATABASE_SERVER
DATABASE=DBNAME
USER=DBUSER
PASS=dbpassword
#in case you run this more than once a day, remove the previous version of the file
unalias rm 2> /dev/null
rm ${FILE} 2> /dev/null
rm ${FILE}.gz 2> /dev/null
#do the mysql database backup (dump)
# use this command for a database server on a separate host:
#mysqldump --opt --protocol=TCP --user=${USER} --password=${PASS} --host=${DBSERVER} ${DATABASE} > ${FILE}
# use this command for a database server on localhost. add other options if need be.
mysqldump --opt --user=${USER} --password=${PASS} ${DATABASE} > ${FILE}
#gzip the mysql database dump file
gzip $FILE
#show the user the result then move it to a destination then push it to it's git repo
echo "${FILE}.gz was created:"
ls -l ${FILE}.gz
mv ${FILE}.gz /root/backup/${PROJECT}/${DATABASE}/
git add ${FILE}.gz && git commit -m `jdate +%Y-%m-%d` && git push -u origin master
@eXtrem0us
Copy link

eXtrem0us commented Apr 16, 2017

It could be GZipped in one line,
Besides, I think you should insure about the successful process of sqldump and gzip and then echo:

mysqldump --opt --user=${USER} --password=${PASS} ${DATABASE} | gzip > $FILE.gz && echo "${FILE}.gz was created:" && ls -l ${FILE}.gz

Also, I think it would be better to use Gregorian date.

@numb95
Copy link
Author

numb95 commented May 30, 2018

@eXtrem0us i saw you comment after a year :)))
I think i was drunk when i wrote this script.
By the way thank you. i will change the script <3

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