MySQL database dump and restore

Simple command line database dump
mysqldump -u[user_name] -p[user_password] [db_name] > [db_name.sql]

Shell script for periodic backups
#!/bin/sh
now=”$(date +’%Y%m%d’)”
filename=”db_backup_$now”.gz
backupfolder=”/exports/db-backup”
fullpathbackupfile=”$backupfolder/$filename”
logfile=”$backupfolder/”backup_log_”$(date +’%Y_%m’)”.log
echo -e “Database backup started at $(date +’%d-%m-%Y %H:%M:%S’)\n” >> “$logfile”
mysqldump -u[user_name] -p[user_password] [db_name] | gzip > “$fullpathbackupfile”
echo -e “Database backup finished at $(date +’%d-%m-%Y %H:%M:%S’)\n” >> “$logfile”
echo “*****************” >> “$logfile”
cat $logfile | mail -s “Database Backup from [server_name]” -r [from email address] [to email address]
rm $logfile
exit 0 

Restore database dump
mysqldump -u[user_name] -p[user_password] [db_name] < [db_name.sql]

Leave a Reply

Your email address will not be published. Required fields are marked *