MySQL backup script

Slawek3 years ago

Backup script MySQL database

Example:

crontab -e
0 1 * * * /root/scripts/mysql-backup.sh >> /var/log/mysql-backup.log 2>&1

edit script, change options

bs.png

Mega Link

#!/bin/bash
#----------------------------------------
# OPTIONS
#----------------------------------------
USER='root'       # MySQL User
PASSWORD='your_password' # MySQL Password
DAYS_TO_KEEP=3    # 0 to keep forever
GZIP=1            # 1 = Compress
BACKUP_PATH='/root/mysql_backup/'
#----------------------------------------
echo /////////////////////////////////// >> /var/log/mysql-backup.log
echo "`date`" >> /var/log/mysql-backup.log
echo /////////////////////////////////// >> /var/log/mysql-backup.log
# Create the backup folder
if [ ! -d $BACKUP_PATH ]; then
  mkdir -p $BACKUP_PATH
fi

# Get list of database names
databases=`mysql -u $USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "|" | grep -v Database`

for db in $databases; do

  if [ $db = 'information_schema' ] || [ $db = 'performance_schema' ] || [ $db = 'mysql' ] || [ $db = 'sys' ]; then
    echo "Skipping database: $db"
    continue
  fi
  
  date=$(date -I)
  if [ "$GZIP" -eq 0 ] ; then
    echo "Backing up database: $db without compression"      
    mysqldump -u $USER -p$PASSWORD --databases $db > $BACKUP_PATH/$date-$db.sql
  else
    echo "Backing up database: $db with compression"
    mysqldump -u $USER -p$PASSWORD --databases $db | gzip -c > $BACKUP_PATH/$date-$db.gz
  fi
done

# Delete old backups
if [ "$DAYS_TO_KEEP" -gt 0 ] ; then
  echo "Deleting backups older than $DAYS_TO_KEEP days"
  find $BACKUP_PATH/* -mtime +$DAYS_TO_KEEP -exec rm {} \;
fi
Przemyslaw3 years ago

Hi, do I have to make file /var/log/mysql-backup.log? Because I have warning in email: E: File could not be read: /var/log/mysql-backup.log

regards,
Przemek

Slawek3 years ago

You can remove this lines

echo /////////////////////////////////// >> /var/log/mysql-backup.log
echo "`date`" >> /var/log/mysql-backup.log
echo /////////////////////////////////// >> /var/log/mysql-backup.log
Przemyslaw3 years ago

@Slawek Do I have to change crontab entry?

Slawek3 years ago

If you dont want log yes

Przemyslaw3 years ago

OK, Thanks.

Slawek3 years ago

did you run script as root ?

Przemyslaw3 years ago

Yes, I 'sudo su' and then 'crontab -e'.

Slawek3 years ago

do you have file /var/log/mysql-backup.log ?
maybe warning is not because script is not able create file
warning is read not write

Przemyslaw3 years ago

No I don't have that file.

Slawek3 years ago

try run manualy as root

chmod 755 mysql-backup.sh
./mysql-backup.sh

Przemyslaw3 years ago

No I see error:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Deleting backups older than 3 days
find: ‘/root/mysql_backup//*’: No such file or directory

I have similar error when try to install traccar on VPS. I have to use that command:
mysql -u root -p --execute not mysql -u root --execute like I tutorial.

Slawek3 years ago

you have to edit script and there where is user write your database user, there where is password write your database password

USER='root'
PASSWORD='your_password' <- here write your database password

if user and password is root

USER='root'
PASSWORD='root'

Przemyslaw3 years ago

Yes, I know. I change it.

Slawek3 years ago

I did chceck in my system case where password is wrong and !!!!

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Deleting backups older than 2 days

you use wrong credentials