Home Linux & Systems Cybersecurity Cloud & DevOps Networks & Infrastructure SIEM & Monitoring DFIR & Threat Intel Development & Other All categories Projects About Tools

Bash script to backup MySQL

Leer en espanol
Bash script to backup MySQL

Table of contents

bash#!/bin/bash # DB Credentials user=»user» password=»password» host=”localhost” db_name=»bbdd» #defi ===

This time we are going to see a small script with which to make MySQL backups

bash
#!/bin/bash
# Credenciales BBDD

user=»usuario»

password=»contraseña»

host=»localhost»

db_name=»bbdd»
# definimos las variables del path y la fecha

backup_path=»/backup»

date=$(date +»%d-%b-%Y»)
# Establecemos los permisos predeterminados

umask 177
# Dump de la base de datos

mysqldump –user=$user –password=$password –host=$host $db_name > $backup_path/$db_name-$date.sql
#Comprimir backup
xz -9 $backup_path/$db_name-$date.sql
# Eliminar archivos de más de 30 días

find $backup_path/* -mtime +30 -exec rm {} \;

we add it in the crontab

bash
rokitoh@red-orbita:# cronta -l
0 0 * * * /usr/bin/backupmysq.sh

All the best

:wq!

Comments