0
votes

I am using the following function to create a backup of my MySQL Database. This does work great and it generate a .sql file containing everything. The file itself has lately been around 36 Megabytes. I was hoping to compress the file into a zip to help save some disk space. I can create the .zip and it appears to work and creates the .zip file that's around 4.5 Megabytes. However, I tried downloading the zip file and opening it on Windows 10 and I got:

This folder is empty.

When I try to unpack the zip, even though it's empty I get:

Windows cannot complete the extraction.
The compressed (zipped) folder {filepath} is invalid.

I used echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; and it gives me:

The zip archive contains 1 files with a status of 0

Opening the zipfile with ZipArchive and var dumping the result gave me:

object(ZipArchive)[15]
  public 'status' => int 0
  public 'statusSys' => int 0
  public 'numFiles' => int 1
  public 'filename' => string '/backups/mysql/2021-01-01-0258-mysql_backup_1609491536.zip' (length=91)
  public 'comment' => string '' (length=0)

What am I doing that is corrupting the file that I cannot unzip or see the files on Windows? PHP seems to be able to tell the file is there.

        function backup_tables($savepath,$tables = '*',$zip=true) {
            $savepath = rtrim($savepath,'/');
            $filepath = $savepath.'/';
            $name = date("Y-m-d-hi").'-mysql_backup_'.time();
            $fileName = $filepath.$name.'.sql';
            $zipName = $filepath.$name.'.zip';
            //$this->select("SET NAMES 'utf8'");
            
            exec('mysqldump --user='.escapeshellarg($this->user).' --password='.escapeshellarg($this->pass).' --host='.escapeshellarg($this->host).' '.escapeshellarg($this->db).' > '.escapeshellarg($fileName).'');
            if(file_exists($fileName)) {
            //if(fclose($handle)){
                if($zip==true) {
                    $zip = new ZipArchive();
                    if($zip->open($zipName,ZIPARCHIVE::CREATE) !== true) {
                        return false;
                    }
                    //add the files
                    
                    $zip->addFile($fileName);
                    //debug
                    //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
                    //close the zip -- done!
                    $zip->close();
                    if(file_exists($zipName)) {
                        //unlink($fileName);
                    }
                    
                    
                    $za = new ZipArchive();
                    $za->open($zipName);
                    var_dump($za);
                    
                    /* All good for Zip but return nothing. */
                    return true;
                }
                else {
                    /* All good for .sql file but return nothing. */
                    return true;
                }
                exit; 
            }
            else {
                echo 'SQL file does not exists at: '.$fileName;
            }
        }

Update

As pointed out in the answers I had the wrong variable name when opening the file. I now get this on the var_dump($za).

 object(ZipArchive)[15]
    public 'status' => int 0
    public 'statusSys' => int 0
    public 'numFiles' => int 1
    public 'filename' => string '.../backups/mysql/2021-01-01-0313-mysql_backup_1609492418.zip' (length=91)
    public 'comment' => string '' (length=0)

It now says there is one file in it; however it is still corrupt on windows.

1

1 Answers

0
votes

You open $fileName in the check, but you'll have to open $zipName...