0

I am trying to take backup of my website`s files and database and upload it to google drive from my website via Google drive Api.

Followed instructions as per these links - backup files to google drive using php https://www.matthewhipkin.co.uk/codelib/automatically-backing-up-your-web-server-files-to-googledrive-with-php/ and did the following -

  1. downloaded the backupgoogledrive folder from https://github.com/hippy2094/backuptogoogledrive and uploaded that in my public_html/backup folder and renamed backuptogoogle.php as backup/index.php

  2. I created the client ID, client secret and an authCode and put those in the setting.inc along with my MySQL username, password and database name. $homedir was put as /home/myuser/ and $sitedir was set to public_html/

  3. Then, I ran in SSH - php /home/user/backup/index.php

    This ran fine generating a json file and backup for files and database also was found in google drive.

  4. Then, I set up cron job as follows - 5 0 * * 6 /usr/local/bin/php /home/myusername/public_html/backup/index.php

    This should run the script at 00:05 on a saturday but it did NOT run or backup to google drive.

  5. After that, I changed the cron to run every 15 minutes. I noticed that the files and database backups are going to home/myuser folder instead of google drive and the error_log in that folder is giving this error every 15 minutes -

Fatal error: Uncaught exception 'Google_AuthException' with message 'Error fetching OAuth2 access token, message: 'invalid_grant'' in /home/myuser/public_html/backup/google-api-php-client/src/auth/Google_OAuth2.php:115

Stack trace:

#0 /home/myuser/public_html/backup/google-api-php-client/src/Google_Client.php(127): Google_OAuth2->authenticate(Array, '4/SCxgUoxjnbe_0...')

#1 /home/myuser/public_html/backup/index.php(38): Google_Client->authenticate('4/SCxgUoxjnbe_0...')

#2 {main} thrown in /home/myuser/public_html/backup/google-api-php-client/src/auth/Google_OAuth2.php on line 115`

Please note while registering at Google API at https://console.developers.google.com/apis I selected Other option in the Credentials Page and not Web Application because my aim is only to take backup for my site. Also, when I selected the Web Application option and generated the client ID and secret and tried to generate the authCode via url generated in SSH, it would give an error with the SSH generated url. So, the solutions for invalid grant which are mentioned in other posts do not work for me as I do not get option for setting call back url.

Please tell what I am doing wrong. Why I am able to run the script successfully manually and not via cron ? When I am running the script manually running in SSH. Only problem happens when cron runs.

Thanks in advance.

user20152015
  • 344
  • 2
  • 23

1 Answers1

0

Found the solutions as follows -

Put in the file index.php this code -

  chdir('/home/user/backup');

Then the cron backed - up the file to google drive properly. So, the problem was with the cron path.

user20152015
  • 344
  • 2
  • 23