Script backup tự động Oracle database bằng rman thông qua crontab
– Tạo file kịch bản bacup tự động increamental level0 => lưu ở đường dẫn /home/oracle
su oracle cd
vi backup_level0.sh
########################################## #!/bin/bash # Script to delete archived logs using RMAN # Set Oracle environment variables (change accordingly) export ORACLE_HOME=/u01/app/oracle/product/19.3.0/dbhome_1 export ORACLE_SID=oradb export PATH=$ORACLE_HOME/bin:$PATH export LD_LIBRARY_PATH=$ORACLE_HOME/lib # Run RMAN command to delete archived logs rman target / <<EOF CROSSCHECK BACKUP; DELETE NOPROMPT EXPIRED BACKUP; DELETE NOPROMPT OBSOLETE; CROSSCHECK ARCHIVELOG ALL; delete noprompt expired archivelog all; backup incremental level 0 database TAG 'full_backup_level0' ; ALTER SYSTEM SWITCH LOGFILE; backup archivelog all; DELETE ARCHIVELOG UNTIL TIME 'SYSDATE-2'; EXIT; EOF echo "Backup thanh cong LV0." >> log_backup.txt date >> log_backup.txt #################################################
– gán quyền thực thi cho file
chmod +x backup_level0.sh
– Tạo file kịch bản bacup tự động increamental level1 lưu trong /home/oracle
su oracle cd
vi backup_level1.sh
########################################## #!/bin/bash # Script to delete archived logs using RMAN # Set Oracle environment variables (change accordingly) export ORACLE_HOME=/u01/app/oracle/product/19.3.0/dbhome_1 export ORACLE_SID=oradb export PATH=$ORACLE_HOME/bin:$PATH export LD_LIBRARY_PATH=$ORACLE_HOME/lib # Run RMAN command to delete archived logs rman target / <<EOF CROSSCHECK BACKUP; DELETE NOPROMPT EXPIRED BACKUP; DELETE NOPROMPT OBSOLETE; CROSSCHECK ARCHIVELOG ALL; delete noprompt expired archivelog all; backup incremental level 1 database TAG 'backup_level1' ; ALTER SYSTEM SWITCH LOGFILE; backup archivelog all; DELETE ARCHIVELOG UNTIL TIME 'SYSDATE-1'; EXIT; EOF echo "Backup thanh cong LV1." >> log_backup.txt date >> log_backup.txt #################################################
– gán quyền thực thi cho file
chmod +x backup_level1.sh
– Tạo crontab chạy script tự động
crontab -e
0 0 * * 3,7 /home/oracle/backup_level0.sh 0 0 * * 1,2,4,5,6 /home/oracle/backup_level1.sh
Khuyến nghị:
Level 0: thực hiện backup tuần 2 lần vào thứ 4 và chủ nhật hàng tuần lúc 0:0AM.
Level 1: thực hiện backup trong tất cả các ngày còn lại lúc 0:0AM.
Backup vào thời gian ít tải nhất, thường là ban đêm.
Chúc các bạn thành công.