LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2007, 09:31 PM   #1
adam_blackice
Member
 
Registered: Apr 2006
Location: /*Egypt */ //cairo
Distribution: Ubuntu 7.04 , SLED 10 , Fedora , RHEL 5
Posts: 312

Rep: Reputation: 32
helping in perl backup script


hello all ...,

i wrote this simple perl script to make a backup for the users of the system

the script is basically working by taking the users parameters for /etc/passwd file and pass it to

a Mysql Database ....,

PHP Code:
 #!/usr/bin/perl -w
use strict;
use 
DBI;
use 
diagnostics;
#step1  - create connection objection . 
my $dsn 'DBI:mysql:contacts';
my $user 'adam';
my $password 'secret';
my $conn DBI->connect($dsn,$user,$password) || die "Error connecting" DBI->errstr;

$file "/etc/passwd";

open (han1"$file") || die  "error opening file: $!";
 

my @newrecords = <han1>;

foreach (@
newrecords) {

@
columns split /:/;

my $username $columns[0];
my $x $columns[1];
my $userid $columns[2];
my $groupid $columns[3];
my $realname $columns[4];
my $homedir $columns[5];
my $shellpath $columns[6];

$conn->do("insert into users(username,x,userid,groupid,realname,homedir,shellpath) values('$username','$x','$userid','$groupid','$realname','$homedir','$shellpath')") || die "error preparing query" $conn->errstr;

it gives me lot of errors i want to know what is wrong any help will be appreciated

thanx for all
 
Old 06-17-2007, 10:50 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Sure. Just tell us specifically (more specifically than "lots of errors") what the situation is.
 
Old 06-18-2007, 02:22 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I agree with wjevans, also, you might want to do the following:

If you want to say or die... use 'or', not '||'. Perl best practice (it's to do with precedence if you really want to know).
If you are going to swallow the whole file into an array, close the file (and check the close()) before continuing.
Use explicit vars ie don't rely on implicit $_
chomp() the @newrecords array before using it, otherwise each rec ends in a newline (\n) and you don't want that.
you can assign all the named vars in the split() instead of using the intermediate @columns array
Use the DBI quote method ( http://search.cpan.org/~timb/DBI-1.56/DBI.pm#quote ) for string type vars during the insert.
 
Old 06-18-2007, 07:06 AM   #4
adam_blackice
Member
 
Registered: Apr 2006
Location: /*Egypt */ //cairo
Distribution: Ubuntu 7.04 , SLED 10 , Fedora , RHEL 5
Posts: 312

Original Poster
Rep: Reputation: 32
thanks for helping and this is the errors

Quote:
root@adam-laptop:/home/adam/Desktop# ./backup.pl
Global symbol "$file" requires explicit package name at ./backup.pl line 11.
Global symbol "$file" requires explicit package name at ./backup.pl line 13.
Global symbol "@columns" requires explicit package name at ./backup.pl line 20.
Global symbol "@columns" requires explicit package name at ./backup.pl line 22.
Global symbol "@columns" requires explicit package name at ./backup.pl line 23.
Global symbol "@columns" requires explicit package name at ./backup.pl line 24.
Global symbol "@columns" requires explicit package name at ./backup.pl line 25.
Global symbol "@columns" requires explicit package name at ./backup.pl line 26.
Global symbol "@columns" requires explicit package name at ./backup.pl line 27.
Global symbol "@columns" requires explicit package name at ./backup.pl line 28.
Execution of ./backup.pl aborted due to compilation errors (#1)
(F) You've said "use strict vars", which indicates that all variables
must either be lexically scoped (using "my"), declared beforehand using
"our", or explicitly qualified to say which package the global variable
is in (using "::").

Uncaught exception from user code:
Global symbol "$file" requires explicit package name at ./backup.pl line 11.
Global symbol "$file" requires explicit package name at ./backup.pl line 13.
Global symbol "@columns" requires explicit package name at ./backup.pl line 20.
Global symbol "@columns" requires explicit package name at ./backup.pl line 22.
Global symbol "@columns" requires explicit package name at ./backup.pl line 23.
Global symbol "@columns" requires explicit package name at ./backup.pl line 24.
Global symbol "@columns" requires explicit package name at ./backup.pl line 25.
Global symbol "@columns" requires explicit package name at ./backup.pl line 26.
Global symbol "@columns" requires explicit package name at ./backup.pl line 27.
Global symbol "@columns" requires explicit package name at ./backup.pl line 28.
Execution of ./backup.pl aborted due to compilation errors.
at ./backup.pl line 35
 
Old 06-18-2007, 02:00 PM   #5
1slipperyfish
LQ Newbie
 
Registered: May 2007
Location: wigan
Distribution: mandriva
Posts: 29

Rep: Reputation: 15
hello dbi is a bit above my perl experience as i'm still learning, however you missed a my from $file on line 13 and my @collumns on line 22, if i try to run your code i get
Code:
 root@localhost perl]# ./diagnoser.pl
Can't locate DBI.pm in @INC (@INC contains: /usr/lib/perl5/5.8.8/i386-linux /usr/lib/perl5/5.8.8 /usr/lib/perl5/site_perl/5.8.8/i386-linux /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5.8.7/i386-linux /usr/lib/perl5/vendor_perl/5.8.6 /usr/lib/perl5/vendor_perl .) at ./diagnoser.pl line 4.
BEGIN failed--compilation aborted at ./diagnoser.pl line 4.
sorry i couldn't be more help
paul
ps sorry i called it diagnoser.pl
 
Old 06-18-2007, 02:26 PM   #6
adam_blackice
Member
 
Registered: Apr 2006
Location: /*Egypt */ //cairo
Distribution: Ubuntu 7.04 , SLED 10 , Fedora , RHEL 5
Posts: 312

Original Poster
Rep: Reputation: 32
thanx for all

i have fixed my at the $file and the @columns but by the way i have anew error at preparing query

Quote:
DBD::mysql::db do failed: INSERT command denied to user 'adam'@'localhost' for table 'users' at ./backup.pl line 30, <han1> line 34.
Uncaught exception from user code:
error preparing queryINSERT command denied to user 'adam'@'localhost' for table 'users' at ./backup.pl line 30, <han1> line 34.
at ./backup.pl line 30
mmm i think that user adam is denied from logging on /etc/passwd file but this user is supposed to be in the database not on the system

any suggestion ?

and thaks again
 
Old 06-18-2007, 06:06 PM   #7
adam_blackice
Member
 
Registered: Apr 2006
Location: /*Egypt */ //cairo
Distribution: Ubuntu 7.04 , SLED 10 , Fedora , RHEL 5
Posts: 312

Original Poster
Rep: Reputation: 32
finally it worked

Quote:
#!/usr/bin/perl -w
use strict;
use DBI;
use diagnostics;
#step1 - create connection objection .
my $dsn = 'DBI:mysql:backupDB';
my $user = 'adam';
my $password = 'secret';
my $conn = DBI->connect($dsn,$user,$password) || die "Error connecting" . DBI->errstr;

my $file = "/etc/passwd";

open (han1, "$file") || die "error opening file: $!";


my @newrecords = <han1>;

foreach (@newrecords) {

my @columns = split /:/;

my $username = $columns[0];
my $x = $columns[1];
my $userid = $columns[2];
my $groupid = $columns[3];
my $realname = $columns[4];
my $homedir = $columns[5];
my $shellpath = $columns[6];

$conn->do("insert into users(username,x,userid,groupid,realname,homedir,shellpath) values('$username','$x','$userid','$groupid','$realname','$homedir','$shellpath')") || die "error preparing query" . $conn->errstr;
}


#the last question i want to know how can i make this operation updated i mean if i add new user for example how can i make the program to fetch the new records and add to the database


 
Old 06-19-2007, 02:20 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If you mean a new uer entry in /etc/passwd, you'll have to write a SELECT that checks each user in the file against the DB and inserts if it's not there, or possibly UPDATEs if it is.
Put all the SQL statements into a separate sub for each one.
I also refer you to my prev comment.
 
Old 06-20-2007, 05:27 PM   #9
adam_blackice
Member
 
Registered: Apr 2006
Location: /*Egypt */ //cairo
Distribution: Ubuntu 7.04 , SLED 10 , Fedora , RHEL 5
Posts: 312

Original Poster
Rep: Reputation: 32
thanx for your replaying but if you gently tell me how to do it exactly to be clearer to me & i will be thankful

Last edited by adam_blackice; 06-20-2007 at 05:30 PM.
 
Old 06-21-2007, 08:59 PM   #10
adam_blackice
Member
 
Registered: Apr 2006
Location: /*Egypt */ //cairo
Distribution: Ubuntu 7.04 , SLED 10 , Fedora , RHEL 5
Posts: 312

Original Poster
Rep: Reputation: 32
MMMMMmmmMMMMMMmmmmmmMMMMmmmMMMMmmMM

i think it will be an SQL task iam not well experienced with it .......,

if any body can help me to figure it out i will be Thankful
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
helping in cgi script that add users to the system adam_blackice Programming 14 04-16-2007 02:41 AM
Helping setting up a proper backup system scoops98 Linux - Software 5 06-22-2005 01:03 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
Helping Script for fresh instalations of Fedora Core 2 veggiemanuk Fedora 0 06-16-2004 07:47 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration