that1guy15
asked on
Incremental backups in Mysql
I am needing to backup one of my databases on a Cent 5 box running mysql. I would like to run a full backup weekly and and daily backups of the binary logs. I have never done this before so i just want to make sure im configuring everything correctly.
To enable binary logging i edit /etc/my.snf and add the following
log-bin=/var/log/mysql/bin ary/mydata base_log
binlog-do-db=mydatabase
Then restart mysqld
Then to run a weekly full i would do a mysqldmp and flush the logs
mysqldump --flush-logs mydatabse > mydatabasebkup.sql
Then on the daily incremental i would flush the logs and backup yesterdays log (this is were im unsure)
mysqladmin flush-logs
mv /var/log/mysql/binary/* /var/fileserver/backups/my database00 1.log
Does this look correct? my concerns are:
1)in the my.cnf file, binlog-do only creates logs for that database, correct?
2)on the daily if i flush the logs does that delete the old log files or does it leave them in the directory
Thanks for the imput
To enable binary logging i edit /etc/my.snf and add the following
log-bin=/var/log/mysql/bin
binlog-do-db=mydatabase
Then restart mysqld
Then to run a weekly full i would do a mysqldmp and flush the logs
mysqldump --flush-logs mydatabse > mydatabasebkup.sql
Then on the daily incremental i would flush the logs and backup yesterdays log (this is were im unsure)
mysqladmin flush-logs
mv /var/log/mysql/binary/* /var/fileserver/backups/my
Does this look correct? my concerns are:
1)in the my.cnf file, binlog-do only creates logs for that database, correct?
2)on the daily if i flush the logs does that delete the old log files or does it leave them in the directory
Thanks for the imput
FLUSH LOGS doesn't delete any binary log files unless you have the 'expire-log-days' variable set. http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#option_mysqld_expire_logs_days . Also see http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#option_mysqld_max_binlog_size .
If expire-log-days isn't set, it just closes the current log and starts a new one.
> mysqladmin flush-logs
> mv /var/log/mysql/binary/* /var/fileserver/backups/my database00 1.log
This is not so good. The brand new binary log file that is started by the flush-logs command is also moved out of the directory. Also, mysql maintains an index text file in the binary directory that contains the names of all binary log files, and it isn't a good idea to delete that file when mysql may be expecting it to be present.
A more solid approach involves maintaining an open mysql client session while concurrently manipulating the files.
1. In a mysql session, issue "LOCK TABLES READ". This waits for all writing queries to finish, then locks the database to prevent any writes from occuring until after either this client session ends or this client issues a "UNLOCK TABLES" command. You don't want to be in this state for very long.
2. Look at the binary/ directory and create a list of all the binary log files that are present.
3. In mysql, issue "FLUSH LOGS". This closes the most recent binary log and creates a new one. We're not interested in backing up the new one.
4. "UNLOCK TABLES" or exit the client session.
5. Use the list of binary logs you created in step 2. These files are all dormant now, and you can copy or move them or whatever.
Of course, this doesn't have to be done manually. A program or script that can use the mysql client api can do this as well.
Here's a similar way to do the full backup.
1. mysql: "LOCK TABLES READ"
2. shell: run the mysqldump command
3. mysql: "RESET MASTER". This deletes all binary log files present in the binary/ directory and resets the binary log serial number to 0.
4. mysql: "UNLOCK TABLES"
If expire-log-days isn't set, it just closes the current log and starts a new one.
> mysqladmin flush-logs
> mv /var/log/mysql/binary/* /var/fileserver/backups/my
This is not so good. The brand new binary log file that is started by the flush-logs command is also moved out of the directory. Also, mysql maintains an index text file in the binary directory that contains the names of all binary log files, and it isn't a good idea to delete that file when mysql may be expecting it to be present.
A more solid approach involves maintaining an open mysql client session while concurrently manipulating the files.
1. In a mysql session, issue "LOCK TABLES READ". This waits for all writing queries to finish, then locks the database to prevent any writes from occuring until after either this client session ends or this client issues a "UNLOCK TABLES" command. You don't want to be in this state for very long.
2. Look at the binary/ directory and create a list of all the binary log files that are present.
3. In mysql, issue "FLUSH LOGS". This closes the most recent binary log and creates a new one. We're not interested in backing up the new one.
4. "UNLOCK TABLES" or exit the client session.
5. Use the list of binary logs you created in step 2. These files are all dormant now, and you can copy or move them or whatever.
Of course, this doesn't have to be done manually. A program or script that can use the mysql client api can do this as well.
Here's a similar way to do the full backup.
1. mysql: "LOCK TABLES READ"
2. shell: run the mysqldump command
3. mysql: "RESET MASTER". This deletes all binary log files present in the binary/ directory and resets the binary log serial number to 0.
4. mysql: "UNLOCK TABLES"
ASKER
So can i set expire-log-days in the my.snf file or how do i configure it?
I'm not familiar with "my.snf", but I'm guessing that it's the same as the "my.cnf" or "my.ini" file.
It's supposed to be "expire-logs-days", not "expire-log-days" like I said before.
MySQL configuration options can be set on the command line or in the configuration file (like "my.cnf"). Command line options use hyphens '-' between words, and configuration file variables use underscores '_' beween words.
In the [mysqld] section of the config file, add a line that looks like:
expire_logs_days=14
Alternatively, if you manually start mysqld, add "--expire-logs-days=14" to the command line.
It's supposed to be "expire-logs-days", not "expire-log-days" like I said before.
MySQL configuration options can be set on the command line or in the configuration file (like "my.cnf"). Command line options use hyphens '-' between words, and configuration file variables use underscores '_' beween words.
In the [mysqld] section of the config file, add a line that looks like:
expire_logs_days=14
Alternatively, if you manually start mysqld, add "--expire-logs-days=14" to the command line.
ASKER
Yeah I meant my.cnf not snf. sorry typo.
Thanks
Thanks
I assumed it was just a typo, then you did it again. :)
ASKER
Ok so i got the binary logging enabled and the expire-logs-days set and the database seems to be back up and running fine.
So flush-logs will delete the binary logs after they are expired. is that correct?
So flush-logs will delete the binary logs after they are expired. is that correct?
Yep. Docs say "Possible removals happen at startup and at binary log rotation", and FLUSH LOGS causes a rotation. That's only if expire_logs_days is set to a nonzero value, though.
Rotation also happens when the current binary log file exceeds "max_binlog_size", if you have that set.
Rotation also happens when the current binary log file exceeds "max_binlog_size", if you have that set.
ASKER
So everything looks to be set up correctly but when i got here this morning i went to check to make sure a log file was created and there isnt a log file for my database. here is my my.cnf file what am i missing?
Thanks
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysq l.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
log-bin=/var/log/mysql/bin ary/bugs_l og
binlog-do-db=bugs
expire_logs_days=2
[mysql.server]
user=mysql
basedir=/var/lib
[mysqld_safe]
log-error=/var/log/mysqld. log
pid-file=/var/run/mysqld/m ysqld.pid
Thanks
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysq
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
log-bin=/var/log/mysql/bin
binlog-do-db=bugs
expire_logs_days=2
[mysql.server]
user=mysql
basedir=/var/lib
[mysqld_safe]
log-error=/var/log/mysqld.
pid-file=/var/run/mysqld/m
Have you restarted the mysqld server since you changed the config file?
ASKER
Yeah i restarted mysqld right after making the changes
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cool. looks like we are good. I needed to give mysql ownership of the /var/log/mysql/binary directory and rebooted mysqld and that solved it.
Thanks for all the Help
Thanks for all the Help
ASKER
THanks for the help
I've used mysqlhotcopy successfully for months. http://dev.mysql.com/doc/refman/5.0/en/mysqlhotcopy.html