Domanda

I'd like to create a backup script for files and folders on my server and I found this:

# Name of the buckup file
$filename = 'BACKUP_' . date('Y.m.d') . '.tar.gz';
# Target path where to save the file
$targetPath = '/path/to/backup/';
# Directory to backup
$dir = "/path/to/files/";  


# Execute the tar command and save file
system( "tar -pczf ".$targetPath ."".$filename." ".$dir );

Which works well, then I can just pass the URL to the script and the right headers and download the file.

My issues is that I'll be running this frequently and don't want to keep the backup on the server. So I'm trying to find a creative way to maybe create a temp file that gets removed. Issue is some of the files could be rather large and I have no way of knowing when the file has been downloaded completely.

È stato utile?

Soluzione

You may create backup files in /tmp, and than use:

# Name of the buckup file
$filename = 'BACKUP_' . date('Y.m.d') . '.tar.gz';
# Target path where to save the file
$targetPath = '/path/to/backup/';
# Directory to backup
$dir = "/path/to/files/";  
# Execute the tar command and save file
system( "tar -pczf ".$targetPath ."".$filename." ".$dir );

readfile("{$targetPath}{$filename}"); // outputs file contents to a browser
unlink("{$targetPath}{$filename}"); // removes file from disk

Read http://php.net/manual/ru/function.readfile.php for more info.

PS Also you may use ZipArchive class(build-in) to create your own archive more flexible.

Altri suggerimenti

Add the file name / path and created timestamp in the database, write a cron job to delete anything greater than the time you want to keep it. Have it run every hour, 30 minutes. However frequent you feel is necessary.

As far as whether it's been downloaded, create a download manager. /download/file/id, where id can be PK in that table, and delete only if it hasn't been downloaded yet, unless its been 48 hours, or something along those lines.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top