Skip to content

Instantly share code, notes, and snippets.

@stefansl

stefansl/backupdb.sh

Last active Dec 8, 2016
Embed
What would you like to do?
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