// 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);
}
}
}