Last active
December 8, 2016 17:34
-
-
Save stefansl/f08b0b87a4c7b382cce3822b7331f586 to your computer and use it in GitHub Desktop.
Quick MySQL-Backup for one database on Linux-Shell
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 | |
# Database credentials | |
DBUSER="" | |
DBPASSWORD="" | |
DBHOST="localhost" | |
DBNAME="" | |
USER="" # fe. myuser.mygroup | |
# Other options | |
BACKUP_PATH="./backup" | |
DATE=$(date +"%d-%b-%Y") | |
# Check if backup path exists | |
mkdir -p ${BACKUP_PATH} | |
# Set default file permissions | |
umask 177 | |
# Dump database into SQL file | |
mysqldump --user=${DBUSER} --password=${DBPASSWORD} --host=${DBHOST} ${DBNAME} >${BACKUP_PATH}/${DBNAME}-${DATE}.sql | |
# Delete files older than 30 days | |
find ${BACKUP_PATH}/* -mtime +30 -exec rm {} \; | |
# Set user rights | |
if [ ! -z "${USER}" ]; then | |
chown -R ${USER} ${BACKUP_PATH} | |
fi | |
# Show output | |
ls -la ${BACKUP_PATH} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment