I leave the program that I use to backup my database.
I use it in a bd in shared hosting. As you say you are in local, you can give more security to the script, saving the connection credentials in /root/.my.cnf
and executing the copy command in this way: copycmd=/usr/bin/mysqlhotcopy
(inside the script, I indicate it in the part that would go, in commentary). If you can not configure it that way, you will have a security warning, because it is not recommended to write bd passwords on the command line ... it is not serious, if a hacker is not listening to what you type on your machine:)
You can store this script in a file called for example: backup_db.sh
you can try it from the console and once it works you can program a cron that executes it as often as you indicate.
The script gives the possibility of saving several versions of copy (you can modify it in this variable numversions=4
), and adds the date to the file name of each copy.
#archivo: backup_db.sh
#nombre base de datos (pueden ser varias, separadas por espacio)
dblist="nombre-base-de-datos"
user="nombre-usuario"
pass="constraseña"
# Directorio para for backups
backupdir=/home/...
# Cantidad de versiones a mantener
numversions=4
# Path para el comando de copiado MySQL
# Si se ponen las credenciales en /root/.my.cnf usar el comando comentado en vez del otro
#copycmd=/usr/bin/mysqlhotcopy
copycmd="/usr/bin/mysqldump -u$user -p$pass --lock-tables --databases"
# Crear directorio si es necesario
mkdir -p "$backupdir"
if [ ! -d "$backupdir" ]; then
echo "Directorio inválido: $backupdir"
exit 1
fi
# Aquí comienza la copia
echo "Copiando bases de datos MySQL ..."
RC=0
for database in $dblist; do
echo
echo "Copiando $database ..."
mv "$backupdir/$database.gz" "$backupdir/$database.0.gz" 2> /dev/null
$copycmd $database | gzip > "$backupdir/$database.gz"
RC=$?
if [ $RC -gt 0 ]; then
continue;
fi
# Rollover the backup directories
rm -fr "$backupdir/$database.$numversions.gz" 2> /dev/null
i=$numversions
while [ $i -gt 0 ]; do
mv "$backupdir/$database.'expr $i - 1'.gz" "$backupdir/$database.$i.gz" 2> /dev/null
i='expr $i - 1'
done
done
if [ $RC -gt 0 ]; then
echo "MySQL Copia fallida!"
exit $RC
else
# Copia completada. Lista de versiones de backup!
ls -l "$backupdir"
echo "Copia MySQL completa!"
fi
exit 0