Help Creating a Backup Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Dirge of Cerberus
Forum Commoner
Posts: 38
Joined: Mon Nov 20, 2006 9:01 pm

Post by Dirge of Cerberus »

It outputs the echo command, stalls, then stops.

Code: Select all

Tuesday 09th of January 2007 09:19:35 PM
Dirge of Cerberus
Forum Commoner
Posts: 38
Joined: Mon Nov 20, 2006 9:01 pm

Post by Dirge of Cerberus »

Oh, and '/..' makes it point to my site root. This script will be located under 'admin'
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

I have a feeling that opendir is returning false. Try double checking the directory reference.
Dirge of Cerberus
Forum Commoner
Posts: 38
Joined: Mon Nov 20, 2006 9:01 pm

Post by Dirge of Cerberus »

Hmm... Apparently, php5 is supported with the use of the *.php5 format...

I understand scandir() stores the data in an array, but how would I make it upload in order, say, upload '$file[1], then $file[2], and so on, while also uploading subdirectories and their files?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Dirge of Cerberus
Forum Commoner
Posts: 38
Joined: Mon Nov 20, 2006 9:01 pm

Post by Dirge of Cerberus »

Hmm.. Makes sense. I'll try it later and post the results.
Dirge of Cerberus
Forum Commoner
Posts: 38
Joined: Mon Nov 20, 2006 9:01 pm

Post by Dirge of Cerberus »

I don't know if it changed anything... I modified the script to notify of FTP connection and login.

Code: Select all

<?php
// FTP Info
$ftpdir = date('l dS \of F Y h:i:s A');

echo $ftpdir;

// set up basic connection
$conn_id = ftp_connect($ftp_server);
echo '<p>Connected, logging in...';
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
echo '<p>Logged in, creating directory....';
// try to create the directory $dir
if (ftp_mkdir($conn_id, $ftpdir)) {
 echo "successfully created the directory $ftpdir. Moving onto Backup...<br>\n";
} else {
echo "There was a problem while creating the directory $ftpdir. Exiting...\n";
exit();
}

# FTP File Transfer Code here...
$directory = '/..';
$file1 = scandir($directory);
foreach ($file1 as $file) {
       if ($file != "." && $file != "..") {
$localfile = $file;
$remote_file = "$ftpdir/$file";
// upload a file
if (ftp_put($conn_id, $remote_file, $localfile, FTP_ASCII)) {
 echo "Successfully uploaded <font color=green><b>$localfile</b></color><br>\n";
} else {
 echo "There was a problem while uploading <font color=red><b>$localfile</b></font><br>\n";
}
       }
       }

// close the connection
ftp_close($conn_id);
?>
It won't even connect.
User avatar
jyhm
Forum Contributor
Posts: 228
Joined: Tue Dec 19, 2006 10:08 pm
Location: Connecticut, USA
Contact:

Post by jyhm »

Sorry if I add confusion,

If you are trying to back-up local files and you are using Linux Unix or OSX.
Wouldn't it be better to write a shell script instead of php?

If you are using a bash terminal here is one tutorial as are many about bash:

http://docsrv.caldera.com:8457/en/AdvBashHowto/
Dirge of Cerberus
Forum Commoner
Posts: 38
Joined: Mon Nov 20, 2006 9:01 pm

Post by Dirge of Cerberus »

OK... I went to support, and it turns out I can't use outbound FTP... I made a new script, this one instead copying to a local folder.... But, it goes horribly wrong.

Code: Select all

<?php
$dir = date('l dS \of F Y h:i:s A');

echo "Folder name: ".$ftpdir."<p>";
$relativedir = "backup/$dir";
$foldir = "../$relativedir";
// try to create the directory $dir
if (mkdir($foldir, 0700)) {
 echo "Successfully created the directory $dir. Moving onto Backup...<br>\n";
} else {
echo "There was a problem while creating the directory $dir. Exiting...\n";
exit();
}

$directory = '/..';
$file1 = scandir($directory);
foreach ($file1 as $file) {
       if ($file != "." && $file != "..") {
$localfile = $file;
$remote_file = "$relativedir/$file";
// upload a file
if (copy($localfile, $remotefile)) {
 echo "Successfully uploaded <font color=green><b>$localfile</b></color><br>\n";
} else {
 echo "There was a problem while uploading <font color=red><b>$localfile</b></font><br>\n";
}
       }
       }

?>

Code: Select all

Folder name:

Successfully created the directory Thursday 11th of January 2007 07:11:32 PM. Moving onto Backup...

Warning: copy(../Packages) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../Packages

Warning: copy(../__db.001) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../__db.001

Warning: copy(../__db.002) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../__db.002

Warning: copy(../__db.003) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../__db.003

Warning: copy(../bin) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../bin

Warning: copy(../boot) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../boot

Warning: copy(../cgi) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../cgi

Warning: copy(../cgi-bin) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../cgi-bin

Warning: copy(../cgi-gd) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../cgi-gd

Warning: copy(../cgi-pl) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../cgi-pl

Warning: copy(../dev) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../dev

Warning: copy(../etc) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../etc

Warning: copy(../home) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../home

Warning: copy(../initrd) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../initrd

Warning: copy(../lib) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../lib

Warning: copy(../mnt) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../mnt

Warning: copy(../opt) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../opt

Warning: copy(../proc) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../proc

Warning: copy(../root) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../root

Warning: copy(../sbin) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../sbin

Warning: copy(../tmp) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../tmp

Warning: copy(../tmpdata) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../tmpdata

Warning: copy(../usr) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../usr

Warning: copy(../var) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../var

Warning: copy(../web) [function.copy]: failed to open stream: No such file or directory in /home/content/a/n/i/animestome/html/admin/backup.php5 on line 22
There was a problem while uploading ../web
Dirge of Cerberus
Forum Commoner
Posts: 38
Joined: Mon Nov 20, 2006 9:01 pm

Post by Dirge of Cerberus »

Nevermind, code done.

backup.php5:

Code: Select all

<?php

set_time_limit(60000000);

$dir = date('l dS \of F Y h:i:s A');

echo "Folder name: ".$dir."<p>";
$relativedir = "backup/$dir";
$foldir = "../$relativedir";
// try to create the directory $dir
if (mkdir($foldir, 0700)) {
 echo "Successfully created the directory $dir. Moving onto Backup...<br>\n";
} else {
echo "There was a problem while creating the directory $dir. Exiting...\n";
exit();
}



include('copyr.php5');





$directory = '../';
$file1 = scandir($directory);
foreach ($file1 as $file) {
       if ($file != "." && $file != ".." && $file != "CORRUPT DIR" && $file != "backup") {
$localfile = "../$file";
$remote_file = "$foldir/$file";
// upload a file
if (copyr($localfile, $remote_file)) {
 echo "Successfully uploaded <font color=green><b>$localfile</b></font><br>\n";
} else {
 echo "There was a problem while uploading <font color=red><b>$localfile</b></font><br>\n";
}
       }
       }

?>
copyr.php5

Code: Select all

<?PHP
      function copyr($source, $dest){
      // Simple copy for a file
      if (is_file($source)) {
      $c = copy($source, $dest);
      chmod($dest, 0777);
      return $c;
      }
       
      // Make destination directory
      if (!is_dir($dest)) {
      $oldumask = umask(0);
      mkdir($dest, 0777);
      umask($oldumask);
      }
       
      // Loop through the folder
      $dir = dir($source);
      while (false !== $entry = $dir->read()) {
      // Skip pointers
      if ($entry == '.' || $entry == '..') {
      continue;
      }
       
      // Deep copy directories
      if ($dest !== "$source/$entry") {
      copyr("$source/$entry", "$dest/$entry");
      }
      }
      
      // Clean up
      $dir->close();
      return true;
      }
?>
Post Reply