root/docs/usc/nemo/usr2/home/jcothran/cc/process_copy.pl

Revision 128 (checked in by jcothran, 3 years ago)

--

Line 
1 #!/usr/bin/perl
2
3 # This script will create copy files from a source db, copy files to a target db or delete files from a source db - usually processed in that step order
4
5 use strict;
6 use XML::XPath;
7
8 #usage: perl process_copy.pl environment_copy.xml <create,copy,delete>  2007_01_01_00-05 2007_02_01_00-05
9 my ($env_path,$mode,$time_start,$time_stop) = @ARGV;
10
11 my $time_start_full = substr($time_start,0,4).'-'.substr($time_start,5,2).'-'.substr($time_start,8,2).' '.substr($time_start,11,2).':00:00'.substr($time_start,13,3);
12 my $time_stop_full = substr($time_stop,0,4).'-'.substr($time_stop,5,2).'-'.substr($time_stop,8,2).' '.substr($time_stop,11,2).':00:00'.substr($time_stop,13,3);
13
14 #print $time_start_full."\n";
15
16 my $xp_env = XML::XPath->new(filename => $env_path);
17
18 my $db_source_host = $xp_env->findvalue('//db_source/host');
19 my $db_source_name = $xp_env->findvalue('//db_source/db_name');
20 my $db_source_username = $xp_env->findvalue('//db_source/username');
21 my $db_source_password = $xp_env->findvalue('//db_source/password');
22
23 my $db_target_host = $xp_env->findvalue('//db_target/host');
24 my $db_target_name = $xp_env->findvalue('//db_target/db_name');
25 my $db_target_username = $xp_env->findvalue('//db_target/username');
26 my $db_target_password = $xp_env->findvalue('//db_target/password');
27
28 #must use absolute psql path for successful server automation(will work without as user)
29 my $path_psql = $xp_env->findvalue('//path/path_psql');
30
31 my $path_tables = $xp_env->findvalue('//path/path_tables');
32 my $xp_tables = XML::XPath->new(filename => $path_tables);
33
34 my $dir_tmp_cpy_local = $xp_env->findvalue('//path/dir_tmp_cpy_local');
35 my $dir_tmp_cpy_host = $xp_env->findvalue('//path/dir_tmp_cpy_host');
36 my $dir_cpy = $xp_env->findvalue('//path/dir_cpy');
37
38 my ($this_table,$these_columns,$filename);
39
40 ##################################################################
41 if ($mode eq 'create') {
42 foreach my $element ($xp_tables->findnodes('//tableList/table')) {
43
44         $this_table = $element->findnodes('name');
45         $this_table = $this_table->string_value();
46
47         $these_columns = $element->findnodes('columns');
48         $these_columns = $these_columns->string_value();
49
50         $filename = "$this_table:$time_start:$time_stop.csv";
51
52         #would like to use 'with csv header' in the copy commands below but need to be using postgresql version 8.2 or greater for this
53         `$path_psql -U $db_source_username -d $db_source_name -h $db_source_host -c "create temp table $this_table\_dump as select $these_columns from $this_table where row_entry_date >= '$time_start_full' and row_entry_date < '$time_stop_full'; copy $this_table\_dump to stdout with csv;" > $dir_tmp_cpy_local$filename`;
54
55         #if file is empty then remove
56         if (-z "$dir_tmp_cpy_local$filename") { `rm $dir_tmp_cpy_local$filename`; }     
57
58 }
59
60 }
61
62 ##################################################################
63 if ($mode eq 'copy') {
64 foreach my $element ($xp_tables->findnodes('//tableList/table')) {
65
66         $this_table = $element->findnodes('name');
67         $this_table = $this_table->string_value();
68
69         $these_columns = $element->findnodes('columns');
70         $these_columns = $these_columns->string_value();
71
72         $filename = "$this_table:$time_start:$time_stop.csv";
73
74         #if copy file exists
75         if (-e "$dir_tmp_cpy_host$filename") {
76         #run copy file against target table to populate
77         `$path_psql -U $db_target_username -d $db_target_name -h $db_target_host -c "copy $this_table($these_columns) from '$dir_tmp_cpy_host$filename' with csv"`;
78
79         #just .gz instead of .tar.gz since testing indicated just .gz smaller
80         `cd $dir_tmp_cpy_local; zip $filename.gz $filename; rm $filename; mv $filename.gz $dir_cpy`;
81         }
82 }
83
84 }
85
86 ##################################################################
87 if ($mode eq 'delete') {
88 my @tableList = ();
89 foreach my $element ($xp_tables->findnodes('//tableList/table/name')) {
90         $this_table = $element->string_value();
91         push(@tableList,$this_table);
92 }
93 @tableList = reverse(@tableList);
94
95 foreach $this_table (@tableList) {
96
97         `$path_psql -U $db_source_username -d $db_source_name -h $db_source_host -c "delete from $this_table where row_entry_date >= '$time_start_full' and row_entry_date < '$time_stop_full'"`;
98 }
99
100 }
101                
102 exit 0;
103
Note: See TracBrowser for help on using the browser.