title: Accessing google drive by running cron job
body: <p>Here I am using google drive to backup my data on drive and for that I wrote a script which i want to run through cronjob. but it needs access_token to access google drive. I am keeping access token in my db and providing it to backup file but that token is not getting updated through cronjob . but when I am running it on browser then working fine. I want to update token through cronjob which didn't get updated.</p>
  
  <p><strong>oauth2callback.php</strong></p>
  
  <pre><code>&lt;?php
      require_once '../inc/google/vendor/autoload.php';
      require_once '../classes/dbconnection.class.php';
  
      $client = new Google_Client();
      $client-&gt;setAuthConfigFile('client_secret.json');
      $client-&gt;setAccessType("offline");
      $client-&gt;setRedirectUri('https://******/controllers/oauth2callback.php');
      $client-&gt;addScope(Google_Service_Drive::DRIVE);
  
      if (! isset($_GET['code'])) {
          $auth_url = $client-&gt;createAuthUrl();
          header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
      } else {
          $client-&gt;authenticate($_GET['code']);
          $token_data = $client-&gt;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-&gt;connect('pf_central'); 
  
          $update_token = $conn-&gt;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'");
  
      }
  
  ?&gt;
  </code></pre>
  
  <p><strong>db_backup.php</strong></p>
  
  <pre><code>&lt;?php
  date_default_timezone_set('Asia/Kolkata');
  require_once '../classes/dbconnection.class.php';
  
  
  $week = array(
                     'Sun' =&gt; '0',
                     'Mon' =&gt; '1',
                     'Tue' =&gt; '2',
                     'Wed' =&gt; '3',
                     'Thu' =&gt; '4',
                     'Fri' =&gt; '5',
                     'Sat' =&gt; '6'
                  );
  
  $folder = array(
                     'Sun' =&gt; '0B7nanNCGzXwLbmx3emVQNFBLNlU',
                     'Mon' =&gt; '0B7nanNCGzXwLb0hDTWFLVmNza3c',
                     'Tue' =&gt; '0B7nanNCGzXwLaVFtRkVRcUU0NFE',
                     'Wed' =&gt; '0B7nanNCGzXwLdlFwQVZuOURSZjA',
                     'Thu' =&gt; '0B7nanNCGzXwLSUhBMnVQVWpVUzQ',
                     'Fri' =&gt; '0B7nanNCGzXwLUGJmOFJId19TVGs',
                     'Sat' =&gt; '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-&gt;setAuthConfigFile('client_secret.json');
      $client-&gt;setAccessType("offline"); 
      $client-&gt;addScope(Google_Service_Drive::DRIVE);
  
      $db = new dbconnection;
      $conn = $db-&gt;connect('pf_central');
  
      $token_data = $conn-&gt;query("SELECT access_token,token_type,expires_in,created FROM google_drive_token WHERE id = '1'")-&gt;fetch_assoc();
  
      //var_dump($token_data);
  
      if (isset($token_data) &amp;&amp; $token_data) {
  
          $client-&gt;setAccessToken($token_data);
          $drive_service = new Google_Service_Drive($client);
         // $files_list = $drive_service-&gt;files-&gt;listFiles(array())-&gt;getFiles();
  
  
          $timestamp = strtotime(date("Y-m-d"));
          $day = date("D", $timestamp);
  
  
          $backup_result = $conn-&gt;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` &lt;&gt; '2' AND `day_code` = '{$week[$day]}' ");
  
          if($backup_result-&gt;num_rows ){
  
              while ($row = $backup_result-&gt;fetch_assoc()){
  
                  try {
                      if($row['backup'] == 0){ 
                          if(isset($row['file_id']) &amp;&amp; !empty($row['file_id']))
                              deleteFile($drive_service,$row['file_id']);
  
                          $world_dumper = Shuttle_Dumper::create(array(
                              'host' =&gt; 'localhost',
                              'username' =&gt; 'Joeroot',
                              'password' =&gt; '*****',
                              'db_name' =&gt; $row['db_name'],
                          ));
  
                          $zipFilename = $row['db_name'].'.sql.gz';
                          $world_dumper-&gt;dump($zipFilename); // if dump is not working then please check a folder perimission and change it to '777'
  
                          $update_backup = $conn-&gt;query("UPDATE `backup` SET `backup`= '1',`file_name`='{$zipFilename}' WHERE `id` = '{$row['id']}' ");
  
                           $file = new Google_Service_Drive_DriveFile(array(
                                    'name' =&gt;$zipFilename ,
                                    'parents' =&gt; array($folder[$day])
                                  ));
                           $result = $drive_service-&gt;files-&gt;create($file, array(
                                          'data' =&gt; file_get_contents($zipFilename),
                                          'mimeType' =&gt; 'application/octet-stream',
                                          'uploadType' =&gt; 'multipart'
                                      ));
  
                          if($result['id']){
                              $update_backup = $conn-&gt;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' =&gt;$row['file_name'] ,
                                    'parents' =&gt; array($folder[$day])
                                  ));
  
                          $result = $drive_service-&gt;files-&gt;create($file, array(
                                          'data' =&gt; file_get_contents($row['file_name']),
                                          'mimeType' =&gt; 'application/octet-stream',
                                          'uploadType' =&gt; 'multipart'
                                      ));
  
                          if($result['id']){
                              $update_backup = $conn-&gt;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-&gt;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-&gt;connect('pf_central');
  
      $timestamp = strtotime(date("Y-m-d"));
      $day = date("D", $timestamp);
  
      $update_backup = $conn-&gt;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-&gt;files-&gt;delete($fileId);
    } catch (Exception $e) {
      print "An error occurred: " . $e-&gt;getMessage();
    }
  }   
  
  ?&gt;
  </code></pre>