Updated version (edited by Kamil):

http://pastebin.com/cf5Q3VhX

should have a daily backup, with some history. It should be bulletproof (--force), it should be logged (>> ...log), it should be compressed (| gzip), it should keep separate copies of each database, and it should automatically pick up any databases that are added.
 
=================================================
/root/backup-mysql.sh
-------------------------------------------------
#!/bin/bash
Host=localhost
BDir=/var/backups/mysql
Dump="/usr/bin/mysqldump --skip-extended-insert --force"
MySQL=/usr/bin/mysql
Today=$(date "+%a")
Databases=$(echo "SHOW DATABASES" | $MySQL -h $Host)
echo 'STARTING BACKUP'
for db in $Databases; do
    if [[ $db != 'Database' && $db != 'information_schema' ]]
    then
        date=`date`
        file="$BDir/$Host-$db-$Today.sql.gz"
        echo "Backing up '$db' from '$Host' on '$date' to: "
        echo "   $file"
        $Dump -h $Host $db | gzip > $file
    fi
done
rm $BDir/*mt4* # remove huge mt4 databases
echo 'BACKUP FINISHED'
 
=================================================
Shell:
-------------------------------------------------
chmod u+x /root/backup-mysql.sh
 
=================================================
 ~/.my.cnf:
-------------------------------------------------
[client]
user = "backup"
password = "SOMEPASS123"
host = "localhost"
 
=================================================
MySQL:
-------------------------------------------------
GRANT
  SELECT, SHOW VIEW, LOCK TABLES ON *.*
  TO backup@localhost
  IDENTIFIED BY 'SOMEPASS123';
FLUSH PRIVILEGES;
 
=================================================
/etc/crontab:
-------------------------------------------------
0 3 * * * root /root/backup-mysql.sh >> backup-mysql.log 2>> backup-mysql.log
 
=================================================
faq/support/mysql/mysql.txt · Last modified: 2019/04/10 08:51 by 127.0.0.1
Back to top
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0