Skip to content

Instantly share code, notes, and snippets.

@tomasz-nolberczak
Created June 7, 2019 11:24
Show Gist options
  • Save tomasz-nolberczak/718c3d023cae33ad0922bb5814d54088 to your computer and use it in GitHub Desktop.
Save tomasz-nolberczak/718c3d023cae33ad0922bb5814d54088 to your computer and use it in GitHub Desktop.
Shell script to backup MySQL database using mysqldump
#!/bin/bash
#########################################
# Shell script to backup MySQL database #
#########################################
# Variables
USER=""
PASSWORD=""
DATABASE=""
HOST=""
DESTINATION=""
DAYS=10
BASE_NAME="dump_$(date +"%d-%m-%Y")"
# Create directory if it doesn't exist
if [ ! -d $DESTINATION ]; then
mkdir -p $DESTINATION;
fi
# Dump database
mysqldump -u $USER -p"$PASSWORD" $DATABASE > $BASE_NAME.sql
# Create TAR GZ archive file and remove the original SQL
tar -czvf $BASE_NAME.tar.gz $BASE_NAME.sql
rm $BASE_NAME.sql
# Move file to directory
mv $BASE_NAME.tar.gz $DESTINATION
# Remove files that are older that $DAYS days
find $DESTINATION -mtime +$DAYS -exec rm -f {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment