I needed a way to periodically backup old data from a mysql database and delete it from the mysql server (to improve speed/unclutter the tables)
My tables consist of 2 columns, one is a key the other is a string representing a date+time in " Y-m-d H:i:s " format:
your_table_1
id - tabletime
0 '2009-12-29 12:03:16'
1 '2009-12-29 12:13:12'
2 '2009-12-29 12:20:45'
... (and so on)
the php script:
<?php
// THIS SCRIPT IS USED TO BACKUP AND DELETE OLD DATA
// It should be called by task scheduler (on windows) or cron (on unix/linux) at set intervals (e.g. daily, every 3 days, weekly, ...)
// CONNECT TO THE MYSQL SERVER
$dbhost = 'localhost'; // change this to the ip if you're running the server on another PC
$dbuser = 'your_username_here';
$dbpass = 'your_secret_password_here';
//WARNING!: you can put these in a separate file so they are invisible but i only run my site locally so i don't really bother with the security yet
$connection = mysql_connect($dbhost, $dbuser, $dbpass)
or die('Error connecting to mysql');
// GENERAL SETTINGS
$backup_dir = "D:/backup/";
// till which date and time do you which to backup the data?
// delete all data that is older than : .. '$shixiaoriqi'
$shixiaoriqi = 100; // DAYS
$delete_unixtime = time() - $shixiaoriqi * 86400; // time() returns current time as a unix time stamp (= seconds since 1970) ::
// 86400 = 24 (hours) * 60 (minutes) * 60 (seconds)
$delete_time = date('Y-m-d H:i:s', $delete_unixtime); // convert the unixtimestamp to our date+time format
// date string - used in the name of the folder where the tables are stored
$datum = 'data_before_'.date('Y-m-d-H-i-s', $delete_unixtime);
// WARNING!: be careful with the name of this folder (not all signs are allowed and neither are spaces/blanks)
// CONNECT TO THE LABVIEW DATABASE
mysql_select_db('labview');
// 1. BACKUP THE DATA
// TABLE 1
$tableName = 'your_table_1';
if (file_exists($backup_dir.''.$datum)) {}//do nothing}
else { mkdir($backup_dir.''.$datum); } // create the directory
$backupFile = $backup_dir.''.$datum.'/'.$tableName.'.sql'; // NOTE: these files can be opened using MS Excel!
$query = "SELECT * INTO OUTFILE '{$backupFile}' FROM {$tableName} WHERE tabletime < '".$delete_time."'";
$result = mysql_query($query);
// ADD MORE TABLES HERE
// 2. DELETE THE DATA FROM THE MYSQL SERVER TABLES
// TABLE 1
$tableName = 'your_table_1';
$query = "DELETE FROM ".$tableName." WHERE tabletime < '".$delete_time."'";
$result = mysql_query($query);
// ADD MORE TABLES HERE
mysql_close($connection); // close the connection with the mysql server
?>
The task scheduler stuff:
1. create a new task
2. set trigger (daily, weekly, ...)
3. set action as follows:
3a. action = run program
3b. program/script = php.exe
3c. parameters = -f D:\backup\DCA_backup.php -> this is the location of your php script!
3d. working directory = D:\xampp\php\ -> this is the folder where your php.exe is located!
That's it.
Suggestions and remarks welcome!!
Comments
Have n't crate sql file
Folder has been created but sql file hasn't created