duanqia9034 2013-06-27 17:54
浏览 106
已采纳

innobackupex流tar并从php运行

I'm trying to create a backup script in php but can't make it work..

First I tried to run this from the command line:

innobackupex --user=root --password=xxx --databases="dbtest" /var/www_backup

It works and dumped all db files in the directory

Then I tried this from the command line:

innobackupex --user=root --password=xxx --databases="dbtest" --stream=tar ./ | gzip -c -1 > /var/www_backup/`date +%Y-%m-%d-%H-%M-%S`.tar.gz

It created a .tar.gz file, but when opening the file it only contains a backup-my.cnf..?! The filesize of backup-my.cnf is about 244 bytes, but the whole .tar.gz file is about 2mb!?? That doesn't make sense... something is wrong with the file..

Then I tried to run the first working command from the webserver

$syntax = 'innobackupex --user='.$mysql_user.' --password='.$mysql_pass.' --databases="'.$mysql_db.'" /var/www_backup';
exec($syntax, $output, $return);
echo "output
";
print_r($output);
echo "return
";
print_r($return);

No file is created but this is returned

output
Array
(
)
return
3

php already has access to the target directory

chown -R www-data /var/www_backup
  • 写回答

1条回答 默认 最新

  • duanbu1421 2013-06-30 17:44
    关注

    It created a .tar.gz file, but when opening the file it only contains a backup-my.cnf..?!

    You must use the -i option for tar to extract or view the contents of a tar file created by Percona XtraBackup. This is documented, and a reminder is output at the end of the innobackupex process:

    . . .
    130630 10:40:23  innobackupex: Connection to database server closed
    innobackupex: You must use -i (--ignore-zeros) option for extraction of the tar stream.
    130630 10:40:23  innobackupex: completed OK!
    

    Omitting the -i option results in the behavior you saw: only the first file in the tar archive is visible.


    Then I tried to run the first working command from the webserver... No file is created... PHP already has access to the target directory

    The innobackupex process must have read privileges on the data and log files under the MySQL datadir, in addition to write privilege to save the output of the backup.

    You should have seen errors like this:

    130630 10:36:20  InnoDB: Operating system error number 13 in a file operation.
    InnoDB: The error means mysqld does not have the access rights to
    InnoDB: the directory.
    InnoDB: File name ./ibdata1
    InnoDB: File operation call: 'open'.
    InnoDB: Error in opening ./ibdata1
    

    But it was probably lost because PHP's exec() call discards stderr output.


    Re your comment of 8/4:

    Okay I tried this myself with the most recent XtraBackup and most recent Percona Server.

    I need to test this from the shell so I can see error output. When you run it only from your PHP script, you aren't seeing the errors as they occur. Once you get it working, you should be able to change the gid to www-data and have it continue to work.

    I tested by changing the group ownership of all files to my own user's gid.

    $ cd /var/lib/mysql     # use your datadir if it is different
    $ chgrp -R billkarwin .
    

    No need to chmod anything on a default installation. But your file permissions are probably not right at this point, so you need to restore them to something that works:

    $ cd /var/lib/mysql     # use your datadir if it is different
    $ chmod -R 660 .
    $ chmod 770 . */.
    $ chmod 777 mysql.sock
    

    Then I can run innobackupex, but I have to specify the mysql credentials:

    $ innobackupex --user=root --password=XXXX /tmp
    
    130804 08:56:33  innobackupex: Connecting to MySQL server with DSN
    'dbi:mysql:;mysql_read_default_group=xtrabackup' as 'root'  (using password: YES).
    130804 08:56:33  innobackupex: Connected to MySQL server
    . . .
    130804 09:53:46  innobackupex: Connection to database server closed
    130804 09:53:46  innobackupex: completed OK!
    

    Note that you could also add an [xtrabackup] group to your user's own $HOME/.my.cnf so you don't have to put the credentials on the command-line.

    $ cat > ~/.my.cnf
    [xtrabackup]
    user=root
    password=XXXX
    ^D
    $ innobackupex /tmp
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路