Created
June 7, 2019 11:24
-
-
Save tomasz-nolberczak/718c3d023cae33ad0922bb5814d54088 to your computer and use it in GitHub Desktop.
Shell script to backup MySQL database using mysqldump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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