Results 1 to 6 of 6

Thread: Backing up mySql databases

  1. #1
    Join Date
    Apr 2009
    Location
    Toronto, Canada
    Beans
    93
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Backing up mySql databases

    I'm using mySql to run a Wiki on my server. I would like to be able to back it up every once in a while in case anything happens to it. Could someone indicate where mySql server databases are saved? Furthermore, a script or utility for backing up/restoring backed up databases to mySql would greatly be appreciated.
    Thanks
    L'idée de communauté consiste d'offrande constante sans s'attendre à quoi que ce soit d'elle.
    The idea of community consists of constant giving without expectation of return.

  2. #2
    Join Date
    Jun 2008
    Location
    Tennessee
    Beans
    3,421

    Re: Backing up mySql databases

    With mysql, as with most RDBMS, you probably don't want to just copy the database files (they're in /var/lib/mysql, by the way, but please read on).

    For various reasons dealing with file consistency, what you want is to do a regular dump of the databases. You can use mysqldump for this (it comes with mysql).

    You could set up a local (ie. can only connect from localhost) mysql user just for backups with no database password, then the command would be something like this:

    Code:
    mysqldump -ubackup_user somedatabase > somedatabase.sql
    Then you can just backup the somedatabase.sql file. If the database is large, you might want to run it through gzip as well.

    You put this in a script and execute it regularly with cron.

    To restore, you 'd just do this:

    Code:
    mysql -uadminuser -p somedatabase < somedatabase.sql

    I should mention at this point that if you're using Webmin, the mysql module has a very easy interface for setting up automated database backups.

  3. #3
    Join Date
    Nov 2009
    Location
    Chennai, India
    Beans
    116
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Backing up mySql databases

    If you have shell access to your database server, you can backup the database using mysqldump. By default, the output of the command will dump the contents of the database in SQL statements to your console. This output can then be piped or redirected to any location you want. If you plan to backup your database, you can pipe the output to a sql file, which will contain the SQL statements to recreate and populate the database tables when you wish to restore your database.
    Code:
    mysqldump -u [username] -p [password] [databasename] > [backupfile.sql]
        o [username] - this is your database username
        o [password] - this is the password for your database
        o [databasename] - the name of your database
        o [backupfile.sql] - the file to which the backup should be written.
    To backup your database 'example' with the username 'admin' and password 'password' to a file example.sql, issue the command:
    Code:
    $ mysqldump -u admin -p password example > example.sql
    If you'd like restrict the backup to only certain tables of your database, you
    can also specify the tables you want to backup.
    Code:
    mysqldump -u [username] -p [password] [databasename] [table1 table2 ....]
        o [tables] - This is a list of tables to backup. Each table is separated by a space
    If you want to backup only table_1 & table_2 from the 'example' database,
    Code:
    mysqldump --add-drop-table -u admin -p password example table_1 table_2 > example.sql
    You can easily restore the mysqldump file by using the mysql command. This method is usually used to recreate or rebuild the database from scratch.
    Code:
    mysql -u [username] -p [password] [database_to_restore] < [backupfile]
    Here's how you would restore your example.sql file to the 'example' database.
    Code:
    mysql -u admin -p password example < example.sql
    You can use AutoMySQLBackup Script that can take periodic database backups.
    See: http://www.debianhelp.co.uk/mysqlscript.htm

    Hope this helps.
    Regards,
    Kiran Murari
    Open Thoughts | CSS OSS

  4. #4
    Join Date
    Mar 2005
    Location
    Italy
    Beans
    45

    Re: Backing up mySql databases

    Thanks for the guide

    Are there any parameter to add to keep export and import valid if you have InnoDB tables with Foreign Keys or myISAM tables with Triggers?

  5. #5
    Join Date
    Nov 2007
    Location
    Newry, Northern Ireland
    Beans
    1,258

    Re: Backing up mySql databases

    You could also download and run MySQL Workbench 5.2 OSS and connect to your MySQL servers and take backups that way, this is also good for browsing and editing tables and dbs using a GUI rather than CLI from the server itself.

    Just another option, not saying it is better or worse than any other.....
    Can't think of anything profound or witty.
    My Blog: http://gonzothegeek.blogspot.co.uk/

  6. #6
    Join Date
    Jan 2007
    Beans
    323

    Re: Backing up mySql databases

    Another solution...

    http://sourceforge.net/projects/automysqlbackup/

    Just put your settings in the file and add it to run in crontab. It keeps daily, weekly, and monthly backups for as many databases as you care to add.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •