这里我使用谷歌驱动器备份我的数据在驱动器上,为此我写了一个脚本,我想通过cronjob运行。但它需要access_token来访问谷歌驱动器。我在我的数据库中保留访问令牌并将其提供给备份文件,但该令牌未通过cronjob进行更新。但是当我在浏览器上运行它然后工作正常。我想通过没有更新的cronjob来更新令牌。
oauth2callback.php
<?php
require_once '../inc/google/vendor/autoload.php';
require_once '../classes/dbconnection.class.php';
$client = new Google_Client();
$client->setAuthConfigFile('client_secret.json');
$client->setAccessType("offline");
$client->setRedirectUri('https://******/controllers/oauth2callback.php');
$client->addScope(Google_Service_Drive::DRIVE);
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$token_data = $client->getAccessToken();
update_token($token_data);
$redirect_uri = 'https://******/controllers/db_backup.php?action=backup';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
function update_token($data){
$db = new dbconnection;
$conn = $db->connect('pf_central');
$update_token = $conn->query("UPDATE google_drive_token SET access_token = '{$data['access_token']}', token_type = '{$data['token_type']}', expires_in = '{$data['expires_in']}', created = '{$data['created']}' WHERE id = '1'");
}
?>
db_backup.php
<?php
date_default_timezone_set('Asia/Kolkata');
require_once '../classes/dbconnection.class.php';
$week = array(
'Sun' => '0',
'Mon' => '1',
'Tue' => '2',
'Wed' => '3',
'Thu' => '4',
'Fri' => '5',
'Sat' => '6'
);
$folder = array(
'Sun' => '0B7nanNCGzXwLbmx3emVQNFBLNlU',
'Mon' => '0B7nanNCGzXwLb0hDTWFLVmNza3c',
'Tue' => '0B7nanNCGzXwLaVFtRkVRcUU0NFE',
'Wed' => '0B7nanNCGzXwLdlFwQVZuOURSZjA',
'Thu' => '0B7nanNCGzXwLSUhBMnVQVWpVUzQ',
'Fri' => '0B7nanNCGzXwLUGJmOFJId19TVGs',
'Sat' => '0B7nanNCGzXwLcnhhV2tvMUdFNkk'
);
if($_GET['action'] == 'backup' || $_GET['action'] == 'Backup' || $_GET['action'] == 'BACKUP' || $_GET['action'] == 'backup#'){
require_once '../inc/google/vendor/autoload.php';
require_once '../classes/dumper.class.php';
$client = new Google_Client();
$client->setAuthConfigFile('client_secret.json');
$client->setAccessType("offline");
$client->addScope(Google_Service_Drive::DRIVE);
$db = new dbconnection;
$conn = $db->connect('pf_central');
$token_data = $conn->query("SELECT access_token,token_type,expires_in,created FROM google_drive_token WHERE id = '1'")->fetch_assoc();
//var_dump($token_data);
if (isset($token_data) && $token_data) {
$client->setAccessToken($token_data);
$drive_service = new Google_Service_Drive($client);
// $files_list = $drive_service->files->listFiles(array())->getFiles();
$timestamp = strtotime(date("Y-m-d"));
$day = date("D", $timestamp);
$backup_result = $conn->query("SELECT `cd`.`db_name`,`b`.`backup`,`b`.`id`,`b`.`file_id`,`b`.`file_name` FROM `client_db` AS `cd` INNER JOIN `backup` AS `b` ON `cd`.`id` = `b`.`client_id` WHERE `b`.`backup` <> '2' AND `day_code` = '{$week[$day]}' ");
if($backup_result->num_rows ){
while ($row = $backup_result->fetch_assoc()){
try {
if($row['backup'] == 0){
if(isset($row['file_id']) && !empty($row['file_id']))
deleteFile($drive_service,$row['file_id']);
$world_dumper = Shuttle_Dumper::create(array(
'host' => 'localhost',
'username' => 'Joeroot',
'password' => '*****',
'db_name' => $row['db_name'],
));
$zipFilename = $row['db_name'].'.sql.gz';
$world_dumper->dump($zipFilename); // if dump is not working then please check a folder perimission and change it to '777'
$update_backup = $conn->query("UPDATE `backup` SET `backup`= '1',`file_name`='{$zipFilename}' WHERE `id` = '{$row['id']}' ");
$file = new Google_Service_Drive_DriveFile(array(
'name' =>$zipFilename ,
'parents' => array($folder[$day])
));
$result = $drive_service->files->create($file, array(
'data' => file_get_contents($zipFilename),
'mimeType' => 'application/octet-stream',
'uploadType' => 'multipart'
));
if($result['id']){
$update_backup = $conn->query("UPDATE `backup` SET `backup`= '2',`file_id`='{$result['id']}',`file_name`='{$zipFilename}' WHERE `id` = '{$row['id']}' ");
unlink($zipFilename);
}
}
else if($row['backup'] == 1){
$file = new Google_Service_Drive_DriveFile(array(
'name' =>$row['file_name'] ,
'parents' => array($folder[$day])
));
$result = $drive_service->files->create($file, array(
'data' => file_get_contents($row['file_name']),
'mimeType' => 'application/octet-stream',
'uploadType' => 'multipart'
));
if($result['id']){
$update_backup = $conn->query("UPDATE `backup` SET `backup`= '2',`file_id`='{$result['id']}',`file_name`='{$row['file_name']}' WHERE `id` = '{$row['id']}' ");
unlink($row['file_name']);
}
}
} catch(Shuttle_Exception $e) {
//$update_backup = $conn->query("UPDATE `backup` SET `backup`= '0',`file_name`='{$zipFilename}' WHERE `id` = '{$row['id']}' ");
}
}
}
else
echo "No data to backup";
} else {
$redirect_uri = 'https://papa.fit/controllers/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
}
else if ($_GET['action'] == 'reSchedule' || $_GET['action'] == 'RESCHEDULE' || $_GET['action'] == 'reschedule' ) {
$db = new dbconnection;
$conn = $db->connect('pf_central');
$timestamp = strtotime(date("Y-m-d"));
$day = date("D", $timestamp);
$update_backup = $conn->query("UPDATE `backup` SET `backup`= '0' WHERE `day_code` = '{$week[$day]}' ");
if($update_backup)
echo "Resheduled databases for next backup";
}
function deleteFile($service, $fileId) {
try {
$service->files->delete($fileId);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
?>