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

Help Creating a Backup Script

Post 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'.
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 »

Have you searched the manual for PHP's FTP functions or file/directory functions?
Dirge of Cerberus
Forum Commoner
Posts: 38
Joined: Mon Nov 20, 2006 9:01 pm

Post 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);
?>
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 »

User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

read up on the linux rsync command. Using PHP is generally just reinventing the wheel
Dirge of Cerberus
Forum Commoner
Posts: 38
Joined: Mon Nov 20, 2006 9:01 pm

Post 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.
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 »

scandir() was introduced in PHP5. If you read the manual entry, there are some alternatives for PHP4 users.
Dirge of Cerberus
Forum Commoner
Posts: 38
Joined: Mon Nov 20, 2006 9:01 pm

Post 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.
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 »

Are there any errors we should be aware of? Where is $ftp_server being set?
Dirge of Cerberus
Forum Commoner
Posts: 38
Joined: Mon Nov 20, 2006 9:01 pm

Post by Dirge of Cerberus »

Can I PM you the entire script? It contains the FTP login and all....
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 »

What's your budget?
Dirge of Cerberus
Forum Commoner
Posts: 38
Joined: Mon Nov 20, 2006 9:01 pm

Post by Dirge of Cerberus »

Zero. T-T
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 »

Post it here then, and remove the FTP login information
Dirge of Cerberus
Forum Commoner
Posts: 38
Joined: Mon Nov 20, 2006 9:01 pm

Post 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);
?>
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 »

What is the output of the script and what errors are being thrown, if any? What directory is opendir('/..') trying to reference?
Last edited by aaronhall on Tue Jan 09, 2007 10:21 pm, edited 1 time in total.
Post Reply