I know there could already have OS backups of database servers, so we usually don't have to backup Oracle products by ourselves. However, if you don't have any OS backups or you want more protection on database servers, you might need to know how to backup Oracle homes before patching. Of course, you may also consider to backup Oracle home periodically by scripts.
Backup Oracle Home
To tar a directory recursively, you need to take care the path of target, either absolute or relative path works. The latter one is more common and easier to understand and operate.
In this case, we choose to do it with the relative path, so we change the present working directory (PWD) to the upper level of the backup directory.
Here we set ORACLE_HOME, then change PWD to its upper directory.
[root@test ~]# export ORACLE_HOME=/u01/app/oracle/product/19.3.0/dbhome_1
[root@test ~]# cd $ORACLE_HOME/..
[root@test 19.3.0]# pwd
If the target you want to backup is Grid home, you should set ORACLE_HOME to the location of Grid home. For example:
[root@primary01 ~]# export ORACLE_HOME=/u01/app/19.3.0/grid
1. GNU tar Command
In Linux, we usually use tar command to backup Oracle homes. More precisely, we call such Oracle product as DB home compared to Grid home.
[root@test 19.3.0]# tar -zcvf /backup/homes/DB_HOME_20220620.tar.gz dbhome_1 > /backup/homes/DB_HOME_20220620_tar_backup.log
[root@test 19.3.0]# echo $?
[root@test 19.3.0]# ll /backup/homes/DB_HOME_20220620.tar.gz
-rw-r--r-- 1 root root 3952582998 Jun 20 21:55 /backup/homes/DB_HOME_20220620.tar.gz
GUN tar in Linux has the ability to filter tar archive through gzip as long as you specify -z option in the command.
For OS superusers like root, ownership, permission and timestamp are all preserved by default, you don't have to set option -p to demand it.
2. UNIX tar Command
In some Unix OS, like AIX, you might only have Unix tar command which does not have the filter to compress archive. In such case, you need to compress the tar file explicitly by gzip.
As usual, we switch PWD to the upper level of ORACLE_HOME, then issue the command.
[root@test 19.3.0]# tar -pcvf - dbhome_1 | gzip > /backup/homes/DB_HOME_20220620.tar.gz
Somewhile, the backup file may be needed to restore Oracle home.