Hi everyone,
Since very long time, at work I need to repeat the same operations every time.
I've therefore decided to automate via script the following instructions, costs what it may:
[ol 1]
[li]ask ID from prompt and store it locally[/li]
[li]ssh to remote host[/li]
[li]enter docker container[/li]
[li]launch DML queries using the previously stored ID[/li]
[li]this returns one row with another ID from the column 'id' which I need to use in the next command[/li]
[li]store result of a DML including the new ID into a csv[/li]
[li]copy the csv into a different path[/li]
[li]exit the docker container[/li]
[li]change permission on that file[/li]
[li]exit the ssh[/li]
[li]secure copy of the file from the remote host to the initial/current folder[/li]
[/ol]
It might look kind of tricky but the only point I see as problematic (on which I'm actually stuck) are 2, 3 and 5.
Here is what I've done so far:
Any suggestion is welcome, feel free to change the logic and thank you in advance.
Since very long time, at work I need to repeat the same operations every time.
I've therefore decided to automate via script the following instructions, costs what it may:
[ol 1]
[li]ask ID from prompt and store it locally[/li]
[li]ssh to remote host[/li]
[li]enter docker container[/li]
[li]launch DML queries using the previously stored ID[/li]
[li]this returns one row with another ID from the column 'id' which I need to use in the next command[/li]
[li]store result of a DML including the new ID into a csv[/li]
[li]copy the csv into a different path[/li]
[li]exit the docker container[/li]
[li]change permission on that file[/li]
[li]exit the ssh[/li]
[li]secure copy of the file from the remote host to the initial/current folder[/li]
[/ol]
It might look kind of tricky but the only point I see as problematic (on which I'm actually stuck) are 2, 3 and 5.
Here is what I've done so far:
Code:
#!/bin/bash
read -p "Enter the ID you would like to process: " my_id
ssh 10.10.10.10
sudo docker exec -it psql-db /bin/sh
psql -U postgres -d MY-DATABASE -c "SELECT * FROM all_cases WHERE id='my_id'";
#get the ID from the 'id' column and save it as 'new_id'
Copy (Select one, two, three FROM table WHERE id='new_id') To '/tmp/'new_id'.csv' With CSV DELIMITER '|' HEADER;
cp /tmp/'new_id'.csv /var/tmp/
exit
sudo chown user:user /opt/tmp/'new_id'.csv
exit
scp 10.10.10.10:/opt/tmp/'new_id'.csv .
Any suggestion is welcome, feel free to change the logic and thank you in advance.