Skip to content

Instantly share code, notes, and snippets.

@stefansl
Last active December 8, 2016 17:34
Show Gist options
  • Save stefansl/f08b0b87a4c7b382cce3822b7331f586 to your computer and use it in GitHub Desktop.
Save stefansl/f08b0b87a4c7b382cce3822b7331f586 to your computer and use it in GitHub Desktop.
Quick MySQL-Backup for one database on Linux-Shell
#!/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