hummty_dummty has asked for the wisdom of the Perl Monks concerning the following question:

iwant to make backup for the users on the system in linux enviroment


hello all Monks
i want to backup users on my linux system i write a acode that takes the users arguments from /etc/passwd files and pass it to a mysql Database

here is my code it gave me lot of errors any body helps me in solving this problem please

#!/usr/bin/perl -w use strict; use DBI; #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,homedi +r,shellpath) values('$username','$x','$userid','$groupid','$realname' +,'$homedir','$shellpath')") || die "error preparing query" . $conn->e +rrstr; }

Regards to All MONKS

Replies are listed 'Best First'.
Re: backup for the users on the system in linux enviroment
by ysth (Canon) on Jun 18, 2007 at 00:04 UTC
    You want to say "split /:/". Or use getpwent or User::pwent.

    It also would be good idea to prepare an insert statement with "values (?,?,?...)" and provide the actual values as parameters to execute().

      when i tried to use
      @columns = split /:/;

      i got the following errors
      Global symbol "$file" requires explicit package name at ./backup.pl li +ne 9. Global symbol "$file" requires explicit package name at ./backup.pl li +ne 11. Global symbol "@columns" requires explicit package name at ./backup.pl + line 18. Global symbol "@columns" requires explicit package name at ./backup.pl + line 20. Global symbol "@columns" requires explicit package name at ./backup.pl + line 21. 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. Execution of ./backup.pl aborted due to compilation errors.

      you said "It also would be good idea to prepare an insert statement with "values (?,?,?...)" and provide the actual values as parameters to execute()."


      you mean i will change values in values('$username','$x','$userid','$groupid','$realname','$homedir','$shellpath')")
      to values(?,?,?,.....) ?
      thanx for your help
        Global symbol "$file" requires explicit package name at ./backup.pl li +ne 9.
        Put a use diagnostics; line at the beginning of your code, and you'll get a verbose explanation of this (or any other) message. Or look for the message in perldiag.
        you mean i will change values in values('$username','$x','$userid','$groupid','$realname','$homedir','$shellpath')") to values(?,?,?,.....) ?
        I mean do something like (example taken from biograd's scratchpad):
        # before your loop $sth_achroms = $dbh -> prepare( " INSERT INTO achroms (type, ac_id, length, chr_num, chr_strt, chr_end, bac_strt, bac_end, orient) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?) "); # later, inside your loop $sth_achroms -> execute( $achrom[0],$achrom[1],$achrom[2], $achrom[3], $achrom[4], $achrom[5], $achrom[6], $achrom[7], $achrom[8]);
        as far as those error messages go - you're using strict mode, but you're not declaring:
        my $file; my @columns;