Skip to content

Instantly share code, notes, and snippets.

@web-hat
Last active March 27, 2017 18:27
Show Gist options
  • Save web-hat/ba4529bf68b8264cf5b6f6079f7f9d8d to your computer and use it in GitHub Desktop.
Save web-hat/ba4529bf68b8264cf5b6f6079f7f9d8d to your computer and use it in GitHub Desktop.
Shell Script To Backup MySQL Database
#!/bin/sh
# Step 1: set up all the variables
###Set the file path where you want to store the backup file and set the name of the file.
FILE=/path/to/your/backup_dir/my_db_file.sql.$(date +'%Y%m%d')
###Database Details:
DBSERVER=db_host
DATABASE=db_name
USER=db_user
PASS=db_password
# Step 2: If you are running this script more then one time then delete the previous copy of db file.
rm -f "$FILE" "$FILE.gz"
# Step 3: Take a MySQL backup.
mysqldump --opt --user=${USER} --password=${PASS} ${DATABASE} > ${FILE}
# Step 4: gzip/compress the MySQL database dump file.
gzip $FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment