Page 1 of 2

Help Creating a Backup Script

Posted: Tue Jan 09, 2007 5:24 pm
by Dirge of Cerberus
I need help creating a backup script. I need to know how to select every file, including subdirectory and etcetera, and FTP it to another server, where it would be stored by date, like 'SERVER_ROOT/Backup-01-09-07-6 15 PM/sitebackupfilesgohere'.

Posted: Tue Jan 09, 2007 5:31 pm
by aaronhall
Have you searched the manual for PHP's FTP functions or file/directory functions?

Posted: Tue Jan 09, 2007 6:48 pm
by Dirge of Cerberus
Hmm... Okay. I've cobbled together some of the code. I just need to know how to select all files to transfer.

Code: Select all

<?php

#FTP CONNECTION CODE

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

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to create the directory $dir
if (ftp_mkdir($conn_id, $dir)) {
 echo "successfully created $dir. Moving onto Backup...<br>\n";
} else {
echo "There was a problem while creating $dir. Exiting...\n";
exit();
}

# FTP File Transfer Code here...


// close the connection
ftp_close($conn_id);
?>

Posted: Tue Jan 09, 2007 7:42 pm
by aaronhall

Posted: Tue Jan 09, 2007 7:49 pm
by Ollie Saunders
read up on the linux rsync command. Using PHP is generally just reinventing the wheel

Posted: Tue Jan 09, 2007 9:25 pm
by Dirge of Cerberus
aaronhall:

scandir() isn't supported on my server.

Code: Select all

Fatal error: Call to undefined function: scandir() in /home/content/a/n/i/animestome/html/admin/scandirtest.php on line 3
ole:

I am not the root, so i doubt I can rsync.

Posted: Tue Jan 09, 2007 9:28 pm
by aaronhall
scandir() was introduced in PHP5. If you read the manual entry, there are some alternatives for PHP4 users.

Posted: Tue Jan 09, 2007 9:45 pm
by Dirge of Cerberus

Code: Select all

<?php

// FTP Connection info went here.
$ftpdir = date('l dS \of F Y h:i:s A');

echo $ftpdir;

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// 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...
$dir = "/..";
if ($handle = opendir('/..')) {
   while (false !== ($file = readdir($handle))) {
       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";
}
       }
   }
   closedir($handle);
}

// close the connection
ftp_close($conn_id);
?>
Why won't it transfer? I leave it there, and it does echo the directory, but it never creates a directory or transfer files.

Posted: Tue Jan 09, 2007 9:55 pm
by aaronhall
Are there any errors we should be aware of? Where is $ftp_server being set?

Posted: Tue Jan 09, 2007 9:56 pm
by Dirge of Cerberus
Can I PM you the entire script? It contains the FTP login and all....

Posted: Tue Jan 09, 2007 10:04 pm
by aaronhall
What's your budget?

Posted: Tue Jan 09, 2007 10:06 pm
by Dirge of Cerberus
Zero. T-T

Posted: Tue Jan 09, 2007 10:14 pm
by aaronhall
Post it here then, and remove the FTP login information

Posted: Tue Jan 09, 2007 10:17 pm
by Dirge of Cerberus

Code: Select all

<?php

$ftp_server = "host";
$ftp_user_name = "**********";
$ftp_user_pass = "******";
$ftpdir = date('l dS \of F Y h:i:s A');

echo $ftpdir;

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// 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...
$dir = "/..";
if ($handle = opendir('/..')) {
   while (false !== ($file = readdir($handle))) {
       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";
}
       }
   }
   closedir($handle);
}

// close the connection
ftp_close($conn_id);
?>

Posted: Tue Jan 09, 2007 10:20 pm
by aaronhall
What is the output of the script and what errors are being thrown, if any? What directory is opendir('/..') trying to reference?