• 2013 - Ultralight vliegtuig
    2013 - Ultralight vliegtuig
  • 2016 - Ondergaande zon op de Nijl
    2016 - Ondergaande zon op de Nijl
  • 2015 - Atlantische oceaan
    2015 - Atlantische oceaan
  • 2015 - Kanoën in France
    2015 - Kanoën in France
  • 2017 - Op de MTB bij holterberg
    2017 - Op de MTB bij holterberg

Hoe maak je een backup van MySQL databases...

Tja, even zoeken, aangezien ik niet in een hele zware omgeving zit, mag de database wel even ' vastgezet' worden.
Ik heb een scriptje gemaakt die ik hieronder wel wil delen...

   

Dit scriptje heet 'mysqlbackup.sh', en heb ik in de crontab gezet om elke dag een keer te runnen..
Ik heb in het begin een aantal variabelen  gezet, zodat je het eenvoudig aan je wensen kunnen aanpassen.

#!/bin/sh

####################################################
# Name:    mysqlbackup.sh
#
# Author:  Gerrit Doornenball (NL)
#
# Purpose: Perform backup of full MySQL environment
#
# Usage:   Create a file 'dbcfjhtwth' with the password in de home directory
#          of root to read the password. (for security reasons i will not put
#          te password in this script.)
#          Put this script in your crontab to run at regular times, eg:
#            0 23 * * * root /usr/local/bin/mysqlbackup.sh &> /dev/null
#            to run as root every day at 23:00.
#
# Change control:
#   1. 28/12/2007  - Created
#   2. 16/01/2008  - resolved issue that older files did not delete..

# Variables
BackupPath=/srv/mysql
CDate=`date +%Y%m%d`
user=root
password=`cat ~/dbcfjhtwth`
File=mysqlbackup-$CDate

# Check if Backuppath exists
if test -d $BackupPath
then
echo " Backup map exists"
else
   echo " Backup map does not exist, creating.."
   mkdir $BackupPath
      if test -d $BackupPath
      then
        echo " Backupmap created"
      else
        echo " Creation of backupmap FAILED"
        exit
      fi
fi

#Create backup
echo "  Creating Backup File... "
/usr/bin/mysqldump --user=$user --password=$password --lock-all-tables --all-databases > $BackupPath/$File.sql

# Archive files
echo "  Archiving $File ..."
# tar zcvf $BackupPath/$File.tgz $BackupPath/$File.sql
zip $BackupPath/$File.zip $BackupPath/$File.sql
rm $BackupPath/$File.sql

# Delete files older than 7 days
echo "  Deleting files older than 7 days ..."
for file in $(find $BackupPath -type f -mtime +7 );do
rm -vf "$file"
done

exit 0

 

You have no rights to post comments