Thursday, September 27, 2007

Infinitive possibilities with UNIX pipes



The UNIX pipes are brilliant :)

I have a "Not enough space" message on my Linux production boxes sometimes and I need to archive or dump databases on-line frequently. So, I've created one archiver host (simple Linux with huge RAID HDDs) for all of my machines.

With commands like these the backup process doesn't consume the local storage, the archive file is created remotely:
(use it on the to_be_archived machine!)

$ tar -c ./ | ssh root@backupmachine "cat | gzip > archive.tar.gz"

or
$ tar -cz ./ | ssh root@backupmachine "cat > archive.tar.gz"

It's better than scp, because it stores the ACLs too.

You can use it for database backup:
$ /usr/local/postgresql/bin/pg_dump -t table -U user db | ssh user@backupmachine "cat | gzip > archive.gz"

or
$ /usr/local/postgresql/bin/pg_dump -t table -U user db | gzip | ssh user@backupmachine "cat > archive.gz"


With a script, like this you can on-line dump your Oracle database tables:
*temporal pipe file is created

#!/bin/bash
export ORACLE_SID=
user=dbuser
echo "rm -f pipe
/usr/sbin/mknod pipe p
ssh user@backupmachine "gzip > oraclebackup.gz" < pipe &
exp userid=${user} parfile=backup.par
" > oraclebackup.sh

echo "file=pipe
log=${usr}_arch_.log
compress=N
tables=(
TABLE1,
TABLE2,
TABLE3,
TABLE4
)
buffer=3000000" > backup.par
sh oraclebackup.sh

rm oraclebackup.sh
rm backup.par


It's a simplier example, to archive specific (with find) files remotely:

#!/bin/bash

mknod pipe p
ssh user@backupmachine "gzip > pipe.tar.gz" < pipe &
find -name *bash* | xargs tar cvf pipe

rm pipe


Feel free to comment if you have better methods (because it's sure these are not the bests)

4 comments:

Anonymous said...

Hi!
Could you please show an example how to export oracle (exp) , pipe in the same time, gzip and send tz file over network to other server.
ssh keys are exchaged.

THX

Kurta László said...

The script for it is in the original post:

Create a file (for example backup.sh) with this content (row numbers for explanations below):


1. #!/bin/bash
2. export ORACLE_SID=yoursid
3. user=yourdbuser
4. echo "rm -f pipe
5. /usr/sbin/mknod pipe p
6. ssh user@backupmachine 'gzip > oraclebackup.gz' < pipe &
7. exp userid=${user} parfile=backup.par
" > oraclebackup.sh

8. echo "file=pipe
log=${usr}_arch_.log
compress=N
tables=(
TABLE1,
TABLE2,
TABLE3,
TABLE4
)
buffer=3000000" > backup.par
9. sh oraclebackup.sh

10. rm oraclebackup.sh
11. rm backup.par



You can run it by
sh backup.sh

line 2-3: prepare the Oracle environment
line 4-7: create the backup script
line 4: remove the old pipe (if exist)
line 5: create the pipe
line 6: run the ssh in the background, input is the pipe
line 7: send the exp's output into the pipe
line 8: create exp's .par file (!!! define the output file to the pipe)
line 9: runs the backup script
line 10-11: clean

So exp's output is forwarded into the named pipe, named pipe used as input for ssh.

Is it OK?

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.