26

I have a server and a domain name on GoDaddy.

I want to create a backup for my files to be uploaded on Google Drive

So that all my files and my database have their data on Google Drive.

I use PHP and MySQL for my database

After some research, I found "Automatically backing up your web server files to GoogleDrive with PHP" and did what he said.

I have downloaded the files google-api-php-client from the backuptogoogledrive repository.

And I have a client ID, client secret and an authCode

I edited the setting.inc and I put my own client ID, client secret and authCode. I also put my MySQL username, password and hostname.

In this page backuptogoogledrive it should create a .tar.gz folder and this folder should contain my website files. Then, this folder should upload it to my Google Drive and do the same thing for my database.

<?php
  set_time_limit(0);
  ini_set('memory_limit', '1024M'); 
  require_once("google-api-php-client/src/Google_Client.php");
  require_once("google-api-php-client/src/contrib/Google_DriveService.php");
  include("settings.inc.php");

  if($authCode == "") die("You need to run getauthcode.php first!\n\n");

  /* PREPARE FILES FOR UPLOAD */

  // Use the current date/time as unique identifier
  $uid = date("YmdHis");
  // Create tar.gz file
  shell_exec("cd ".$homedir." && tar cf - ".$sitedir." -C ".$homedir." | gzip -9 > ".$homedir.$fprefix.$uid.".tar.gz");
  // Dump datamabase
  shell_exec("mysqldump -u".$dbuser." -p".$dbpass." ".$dbname." > ".$homedir.$dprefix.$uid.".sql");
  shell_exec("gzip ".$homedir.$dprefix.$uid.".sql");

  /* SEND FILES TO GOOGLEDRIVE */

  $client = new Google_Client();
  // Get your credentials from the APIs Console
  $client->setClientId($clientId);
  $client->setClientSecret($clientSecret);
  $client->setRedirectUri($requestURI);
  $client->setScopes(array("https://www.googleapis.com/auth/drive"));
  $service = new Google_DriveService($client);  
  // Exchange authorisation code for access token
  if(!file_exists("token.json")) {
    // Save token for future use
    $accessToken = $client->authenticate($authCode);      
    file_put_contents("token.json",$accessToken);  
  }
  else $accessToken = file_get_contents("token.json");
  $client->setAccessToken($accessToken);  
  // Upload file to Google Drive  
  $file = new Google_DriveFile();
  $file->setTitle($fprefix.$uid.".tar.gz");
  $file->setDescription("Server backup file");
  $file->setMimeType("application/gzip");
  $data = file_get_contents($homedir.$fprefix.$uid.".tar.gz");
  $createdFile = $service->files->insert($file, array('data' => $data, 'mimeType' => "application/gzip",));
  // Process response here....
  print_r($createdFile);      
  // Upload database to Google Drive
  $file = new Google_DriveFile();
  $file->setTitle($dprefix.$uid.".sql.gz");
  $file->setDescription("Database backup file");
  $file->setMimeType("application/gzip");
  $data = file_get_contents($homedir.$dprefix.$uid.".sql.gz");
  $createdFile = $service->files->insert($file, array('data' => $data, 'mimeType' => "application/gzip",));
  // Process response here....
  print_r($createdFile);  

  /* CLEANUP */

  // Delete created files
  unlink($homedir.$fprefix.$uid.".tar.gz");
  unlink($homedir.$dprefix.$uid.".sql.gz");

?>

The problem now is that I have two folders for the database and there's no problem on it, and a second folder for the files. But this folder doesn't have any files on it.

How can I solve this problem?

enter image description here enter image description here

// User home directory (absolute)
  $homedir = "/home/mhmd2991/public_html/"; // If this doesn't work, you can provide the full path yourself
  // Site directory (relative)
  $sitedir = "public_html/"; 
Shadow
  • 33,525
  • 10
  • 51
  • 64
mohamad mohamad
  • 613
  • 9
  • 24
  • @DaImTo can you explain more – mohamad mohamad Oct 19 '17 at 12:37
  • Are you saying that it works for database folder backup, but not for files? – Don't Panic Oct 23 '17 at 08:16
  • @Don'tPanic yes that what i say – mohamad mohamad Oct 23 '17 at 08:25
  • I don't see any code which creates folders, only `.gz` files. Can you clarify? – Don't Panic Oct 23 '17 at 08:25
  • @Don'tPanic after i check the files i think the database work because it create a file not a folder can you please check the 2 images above – mohamad mohamad Oct 23 '17 at 08:28
  • What are the 2 filename screenshots you added? Is that what you see in Google Drive? If yes, it looks like everything works, right? – Don't Panic Oct 23 '17 at 08:38
  • @Don'tPanic when i download this files i open the sitebackup file using winzip i receive this message can not open file sitebackup.tar.gz it does not appear to be a valid archive if you downloaded this file try downloading the file again i click ok winzip open and i have empty file – mohamad mohamad Oct 23 '17 at 08:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157264/discussion-between-mohamad-mohamad-and-dont-panic). – mohamad mohamad Oct 23 '17 at 08:58
  • please check die("cd ".$homedir." && tar cf - ".$sitedir." -C ".$homedir." | gzip -9 > ".$homedir.$fprefix.$uid.".tar.gz") and run command on server to see output – Honarkhah Oct 24 '17 at 11:42
  • @mohamadmohamad what is your hosting plan on GoDaddy? – TRiNE Oct 30 '17 at 03:27
  • @TRiNE it's an Ultimate hosting – mohamad mohamad Oct 30 '17 at 06:45
  • @mohamadmohamad is it a dedicated server or website hosting? if it is dedicated server do you have ssh access? what is operation system then? – TRiNE Oct 30 '17 at 12:45
  • @TRiNE thank you for your reply now i have a backup .tar.gz the problem is i don't have this file in my google drive account can you help me on that ??!! – mohamad mohamad Nov 01 '17 at 06:42
  • Pure madness to give the data to Google like that when you are already paying for unlimited hosting space on GoDaddy in which you retain full file control. What would it hurt to make a directory on your web server and just copy all the files there so you have a backup that is ready to go on the server that needs the data? Unless you NEEEEEED cloud access of this data, its not a great solution, as more and more systems get eaten up by AWS, its like the lottery compounding on its payout for the day someone socials their way into the vault. – easleyfixed Aug 02 '22 at 20:48

5 Answers5

0

It seem the backup script is unable to find the directory where the site files are stored.

Quoting from the tutorial you followed to do the backup:

// User home directory (absolute)
$homedir = trim(shell_exec("cd ~ && pwd"))."/"; // If this doesn't work, you can provide the full path yourself
// Site directory (relative)
$sitedir = "www/";

First ensure that $sitedir is set properly with the relative path (from the home directory) to the site files directory.

It could be something different than www/, for example public_html/ for a website hosted on GoDaddy.

If the above is correct try to set $home variable manually with the absolute path of the home directory.


Update

$homedir is the home directory and $sitedir is the website root relative to $homedir

so looking at the code you posted it's quite likely there is a mistake, the two variable should be:

// User home directory (absolute)
$homedir = "/home/mhmd2991/"; // <-- FIXED HERE
// Site directory (relative)
$sitedir = "public_html/";

This assumes your website root directory is public_html and is located inside your home directory mhmd2991

Again make sure your website root directory is actually public_html and not www or html or anything else. Check it out using the terminal.

Paolo
  • 15,233
  • 27
  • 70
  • 91
  • thank you for your reply i did what you suggest i changed $sitedir to public_html/ and also i already did before $homedir i put on it the absolute path of the home directory but the problem is not solve it yet when i download the file of site backup is empty but for my database i have a file with my database data – mohamad mohamad Oct 24 '17 at 06:07
  • @mohamadmohamad hi, I tried the script and it works fine on my machine. I assume `$sitedir` is still NOT pointing to the website root folder. It would help us to help you if you could edit your question and post the directory tree structure – Paolo Oct 24 '17 at 08:10
  • can you please check above i put $sitedir and $homedir – mohamad mohamad Oct 24 '17 at 11:59
  • i'm sure that my website root directory is public_html/ after i change the $homedir i don't have any .gz file in my server for database or for website backup. Before i change the $homedir i have this message appear on my browser. This page isn’t working is currently unable to handle this request. HTTP ERROR 500 but after i change $homedir i don't receive this error and also the page load normaly without any error message – mohamad mohamad Oct 25 '17 at 06:23
  • @mohamadmohamad: Are you sure you have not forgotten some `;`? 2: Find in your provider info how to get access to `php_error.log` – bato3 Oct 25 '17 at 22:53
  • @mohamadmohamad HTTP ERROR 500 is usually synthom of a syntax error in your code. Check you don't have introduced errors/typos when you have edited the configuration as suggested in my answer – Paolo Oct 26 '17 at 11:16
  • @Paolo this error appear before your edit after i use your configuration i don't receive any error but also i don't have a .gz file – mohamad mohamad Oct 26 '17 at 11:47
0

In GoDaddy, or any other platforms. The shared hosting generally do not provide shell access or do not allow ssh access. Many times, you will face issues running command in the shell, as shell executable command are banned by the hosting provider.

This helps them to provide resource to all the shared hosting users in a better way without hampering the isolation of one another.

You can still see options to turn on SSH nad other similar shell access options in different hosting platforms. Refer to the link below to activate SSH on GoDaddy https://in.godaddy.com/help/enable-ssh-for-my-linux-hosting-account-16102

-1

Dont you miss a hyphen in front of cf in your tar? Isnt it supposed to be tar -cf? You have tar cf. I dont think it recognises it as a command parameter, so there is no file created.

Tamali
  • 326
  • 2
  • 8
  • i did what you suggest shell_exec("cd ".$homedir." && tar -cf - ".$sitedir." -C ".$homedir." | gzip -9 > ".$homedir.$fprefix.$uid.".tar.gz"); but also i don't have any tar.gz file – mohamad mohamad Oct 30 '17 at 06:44
  • You can throw out the part with "gzip" if you use `-z` with the tar-command. Also i just noticed that the problem ist that there is no input data for tar. The use of tar is `tar [OPTIONS] input_data`. So yours should be something like: `shell_exec("cd" . $homedir . " && tar -czf " . $prefix.$uid . ".tar.gz input_file_or_directory");` – Tamali Oct 30 '17 at 10:02
  • The input_file_or_directory what should it be ?? – mohamad mohamad Oct 30 '17 at 11:13
  • In your case i guess it should be the directory in which you store your website-files. So something like: `shell_exec("cd" . $homedir . " && tar -czf " . $prefix.$uid . ".tar.gz public_html/");`. Do not forget the slash at the end of the directory or the containing data and the subfolders will not be included! – Tamali Oct 30 '17 at 11:19
  • i've tried like that shell_exec("cd" . $homedir . " && tar -czf " . $sitedir . ".$homedir.$fprefix.$uid.".tar.gz"); and like that shell_exec("cd" . $homedir . " && tar -czf " . $sitedir . ".$homedir.$fprefix.".tar.gz"); and like that shell_exec("cd" . $homedir . " && tar -czf " . $prefix.$uid . ".$homedir.$fprefix.".tar.gz"); and like that shell_exec("cd" . $homedir . " && tar -czf " . $prefix.$uid . ".$homedir.".tar.gz"); but i receive error HTTP ERROR 500 – mohamad mohamad Oct 30 '17 at 11:28
  • Well, the first name after `-czf` should be the filename of the file you want to create including the `.tar.gz`-ending. The second name ist the name of the directory. If your $homedir is still `/home/mhmd2991/` then the second name for tar should be `public_html/`. If your $homedir is `/home/mhmd2991/public_html/` then you are already inside the folder and the second name for tar should be `*`. So `shell_exec("cd " . $homedir . " && tar -czf " . $prefix.$uid . ".tar.gz public_html/");` should work for the first case. Otherwise use `*` instead of `public_html/`. – Tamali Oct 30 '17 at 11:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157799/discussion-between-mohamad-mohamad-and-tamali). – mohamad mohamad Oct 30 '17 at 11:51
  • @Tamali I tried the script on my mac (the part that archives the files) and works as expected. The TAR conmands parameters are correct – Paolo Nov 03 '17 at 21:09
-1

I was able to run this script successfully while using root access (available in VPS / dedicated servers). But in shared server, where root access is not available, this script is aborted by the server. Shared servers have restrictions on how much maximum time a script can run (usually less than one minute - but depends on the hosting)

Try to manually run the script in SSH and you will see that the script is aborted by the server which results in no files in the folder created.

user20152015
  • 344
  • 2
  • 23
-1

Most quality hosting sites allow users to archive their program files/directory/site. Similarly, if you are using databases, the database program usually has a link where you can archive and or export tables or an entire database. You will have to look for the instructions on how to do this as it varies between platforms and database systems, and the hosting site.

Once you have archived your files/directory/site and/or databases you can use an FTP program to export them to your computer. Likewise you can then copy them to another computer or the cloud.

My preference is to archive whatever I want to export into zip or tar files. For security reasons I also prefer to save my database files to an external drive or put them on a recordable DVD.

Phil R
  • 3
  • 3