Gossamer Forum
Home : General : Perl Programming :

copying a directory and subdirectories

Quote Reply
copying a directory and subdirectories
trying to write a subroutine to copy all the files in a directory, as well as the subdirectories and their files

Code:

use File::Copy::Recursive qw(dircopy );

my ($new_date) = &date_to_unix(&get_date);

my $newdirname = $new_date;
if ((-e "$backup_dir/$newdirname")) {
&html_admin_home("backup already exists for this date");
}
else {

my $src = "$db_data_path";
my $dest = "$backup_dir/$newdirname";

dircopy($src, $dest) or die "Can't dircopy: $db_data_path $!";
}


getting following error although ...data is the directory i want to copy
Can't dircopy: /var/home/delicia/delicia.com/cgi-bin/public/data No such file or directory

Last edited by:

delicia: May 21, 2018, 7:53 AM
Quote Reply
Re: [delicia] copying a directory and subdirectories In reply to
Simple question - but DOES it exist? Try:

Code:
my $src = "$db_data_path";
my $dest = "$backup_dir/$newdirname";

if (-d $src) {
print "$src exists ok...\n";
}

if (-d $desc) {
print "$dest exists ok...\n";
}

dircopy($src, $dest) or die "Can't dircopy: $db_data_path $!"; 1
.
..and then see what that outputs. BTW, you don't need to quote:

Code:
my $src = "$db_data_path";

It can just be:

Code:
my $src = $db_data_path;

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] copying a directory and subdirectories In reply to
ok i was getting src exists ok so the problem is destination. i'm sure it's a simple typo but i can't find it!!!
Code:
use File::Copy::Recursive qw(dircopy );

my ($msg,$src,$dest);

my ($new_date) = &date_to_unix(&get_date);

my ($newdirname) = "$db_data_path/$backup_dir/$new_date";

if ((-e "$newdirname")) {
&html_admin_home("backup already exists for this date");
}
else {
# not creating dir yet, just trying to get it to display the correct path!!!
# mkdir ("$db_data_path/$backup_dir/$newdirname",0777) or cgierr("$0: cannot create directory $backup_dir/$newdirname.\nReason: $!");
$dest = $newdirname;
$msg .= "path is $db_data_path";
$msg .= "<br>newdate is $new_date";
$msg .= "<br>dest is $dest";
$src = $db_data_path;


if (-d $src) {
$msg .= "<br>$src exists ok";
}

if (-d $dest) {
$msg .= "<br>$dest exists ok";
}

message:

path is /var/home/delicia/delicia.com/cgi-bin/public/data
newdate is 20180522
dest is backup/20180522
/var/home/delicia/delicia.com/cgi-bin/public/data exists ok

why isn't $dest including the $db_data_path?!?

$dest should be /var/home/delicia/delicia.com/cgi-bin/public/data/backup/20180522
Quote Reply
Re: [delicia] copying a directory and subdirectories In reply to
Hi,

Yeah that is a bit weird! Here is a tidied up bit of your code;

Code:
use File::Copy::Recursive qw(dircopy );

my ($msg,$src,$dest);

my ($new_date) = &date_to_unix(&get_date);

my $newdirname = "$db_data_path/$backup_dir/$new_date";

if (-e $newdirname) {
&html_admin_home("backup already exists for this date");
} else {
# not creating dir yet, just trying to get it to display the correct path!!!
# mkdir ("$db_data_path/$backup_dir/$newdirname",0777) or cgierr("$0: cannot create directory $backup_dir/$newdirname.\nReason: $!");
$dest = $newdirname;
$msg .= "path is $db_data_path";
$msg .= "<br>newdate is $new_date";
$msg .= "<br>dest is $dest";
$src = $db_data_path;


if (-d $src) {
$msg .= "<br>$src exists ok";
}

if (-d $dest) {
$msg .= "<br>$dest exists ok";
}

}

No need to wrap stuff like ((-e $file)) { Angelic

So as a test try adding:

Code:
print qq|
\$db_data_path => $db_data_path
\$backup_dir => $backup_dir
\$new_date => $new_date
JOINED: $newdirname|;

Just after:

Code:
my $newdirname = "$db_data_path/$backup_dir/$new_date";

And then see what gets outputted

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] copying a directory and subdirectories In reply to
i changed
Code:
my $newdirname = "$db_data_path/$backup_dir/$new_date";
to
Code:
my $newdirname = "/$backup_dir/$new_date";
then the
JOINED: $newdirname
is what i want except it has an extra slash at the beginning. even if i can figure out how to get rid of the extra slash, i don't know how to set $dest = JOINED: $newdirname
Quote Reply
Re: [delicia] copying a directory and subdirectories In reply to
Code:
my $newdirname = "$backup_dir/$new_date";

,...maybe? Angelic

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] copying a directory and subdirectories In reply to
i don't understand it, but that did fix the JOINED part. however, $dest is just backup/20180523. i need $dest to be same as JOINED
Quote Reply
Re: [delicia] copying a directory and subdirectories In reply to
What does it give you if you used my example code? I need to see the value of that ;)

Code:
print qq|
\$db_data_path => $db_data_path
\$backup_dir => $backup_dir
\$new_date => $new_date
JOINED: $newdirname|;

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] copying a directory and subdirectories In reply to
Quote:
$db_data_path => /var/home/delicia/delicia.com/cgi-bin/public/data
$backup_dir => /var/home/delicia/delicia.com/cgi-bin/public/data/backup
$new_date => 20180523

JOINED: /var/home/delicia/delicia.com/cgi-bin/public/data/backup/20180523

the joined line is perfect. i want to test if the 20180523 directory exists. if not, create it. then copy everything from $db_data_path to it.

although looking at my logic i think that's wrong maybe redundant. i don't want to copy the $backup_dir. so maybe i need to join $db_script_path instead of $db_data_path. $db_script_path is same as data_path except it doesn't have /data at the end. i just looked at my cfg and i have $backup_dir = $db_data_path . "/backup"

in other words, i could put my backup directory off script path instead of off data path OR i could leave it off data path but exclude it when i do the dircopy. i will defer to your recommendation! thanks

Last edited by:

Andy: May 23, 2018, 7:18 AM
Quote Reply
Re: [delicia] copying a directory and subdirectories In reply to
Hi,

I'm a bit confused as to why you are wanting to put the backup folder INSIDE the data folder? this is going to end up recursivly copying all the past backups. ie you could end up with:

/data/backup/12345/
/data/backup/backup/12345/
/data/backup/backup/12345/backup/12345/
etc (and all the past backups as well)

I would move the backup to a totally different folder - say:

DATA: /var/home/delicia/delicia.com/cgi-bin/public/data
BACKUP: /var/home/delicia/delicia.com/cgi-bin/public/backup

This is how I would do it:

Code:
mkdir("$db_data_path/backup") if !-d "$db_data_path/backup";
my $src = $db_data_path;
my $dest = "$db_data_path/backup/$new_date";

dircopy($src, $dest) or die "Can't dircopy: $db_data_path to $dest $!";

On a seperate note - I would make sure the backup folder is CHMOD 0666 so that it can't be publicly read (OR put a .htaccess protection in it, so prying eyes can't read the contents)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] copying a directory and subdirectories In reply to
it's working perfectly! thank you so much. everything is off cgi-bin so i don't think it can be seen unless logged in through script.

Code:
sub admin_backup_table {
# -------------------------------
# copy everything from $data_path

use File::Copy::Recursive qw(dircopy );

my ($msg,$src,$dest);

my ($new_date) = &date_to_unix(&get_date);

my $dest = "$backup_dir/$new_date";

if (-e $dest) {
&html_admin_home("backup already exists for this date $new_date");
}
else {

mkdir ("$dest",0777) or cgierr("$0: cannot create directory $dest.\nReason: $!");

$src = $db_data_path;

dircopy($src, $dest) or die "Can't dircopy: $db_data_path $!";

$msg .= "<BR>Backup Successful";

}
&sis_logging("Backup made $new_date") if ($auth_logging);
&html_admin_home($msg);

}
Quote Reply
Re: [delicia] copying a directory and subdirectories In reply to
Cool :)

Regarding it not being accessible from the browser - I would still give it a go :) (just find a file, and type the URL in as if it was a normal URL - if you get a forbidden error, you are good Cool)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] copying a directory and subdirectories In reply to
yes, i tried it and couldn't get in. so all is well. my brain is tired now. i will work on restoring files later!
Quote Reply
Re: [delicia] copying a directory and subdirectories In reply to
Angelic