Save daily backups of your WordPress database to file during development, to store them in GIT alongside with other developers databases.
First, define the environment by adding this to your wp-config.php:
1 |
define('WP_ENV', 'development'); // development, staged or production |
Second, paste this to your functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
// If the constant WP_ENV (from wp-config.php) is set to development if(WP_ENV == 'development'){ // If current user is administrator if(is_user_logged_in() && current_user_can('manage_options')){ // Set to 'd' to only save backup from the latest 30 days or so. This can be changed to houry or weekly or whatever. $backup_day = date_i18n('Ymd'); $user = wp_get_current_user(); $user_login = $user->user_login; // Include your username in the filename if you are using GIT along side with other developers $sql_filename = $user_login . '_' . $backup_day . '.sql'; // Get latest file update date $updated_db_file = get_option('updated_db_file_' . $user_login); // Set the backup path $backup_path = get_template_directory() . '/_db_backup/'; // Is the latest file update today? if not, continue if($updated_db_file != $backup_day){ $filename = $backup_path . 'index.php'; if(!file_exists($filename)){ // Check if path exist, otherwise, create index.php and .htacces and block acces $mk_path = $backup_path; mkdir($mk_path, 0700); $write_index = fopen($mk_path . 'index.php', 'w') or die('Kunde inte skapa index.php'); $txt = '<?php // Silence is golden'; fwrite($write_index, $txt); fclose($write_index); $write_htaccess = fopen($mk_path . '.htaccess', 'w') or die('Kunde inte skapa htaccess'); $htaccesstxt = 'deny from all'; fwrite($write_htaccess, $htaccesstxt); fclose($write_htaccess); } // Make sure this points to your mysqldump command exec('/Applications/AMPPS/mysql/bin/mysqldump --user=' . DB_USER . ' --password=' . DB_PASSWORD . ' --host=' . DB_HOST . ' ' . DB_NAME . ' > ' . $backup_path . $sql_filename . ' 2>&1'); // Update option value to set date to today update_option('updated_db_file_' . $user_login, $backup_day); } } } |