Tuesday, June 17, 2025

.::: Trial Backup & Restore MariaDB MySQL with crontab -l / crontab -e & /etc/crontab :::.

 


A. Prepare Script backup & Restore

1. create backup script

[root@teguhth cron]# pwd
/data/cron
[root@teguhth cron]# cat backup_test.sh
echo "backup full mulai" > log_backup.txt
date >> log_backup.txt

mysqldump -uroot -pxxx -CfQq --max-allowed-packet=1G --hex-blob --order-by-primary --single-transaction --routines=true --triggers=true --no-data=false teguhth | gzip -c > teguhth_ori.sql.gz ;

echo "backup full selesai " >> log_backup.txt
date  >> log_backup.txt
[root@teguhth cron]#


2. create restore script 

[root@teguhth cron]# pwd
/data/cron
[root@teguhth cron]# cat restore_test.sh
echo "restore mulai" > /data/cron/log_restore.txt
date >> /data/cron/log_restore.txt
echo "drop & create database teguhth02;"  >> /data/cron/log_restore.txt
mysql -uroot -pxxx -e "drop database teguhth02;"
mysql -uroot -pxxx -e "create database teguhth02;"

gunzip -c < /data/cron/teguhth_ori.sql.gz  | mysql -uroot -pxxx -f teguhth02

echo "restore selesai" >> /data/cron/log_restore.txt
date >> /data/cron/log_restore.txt
[root@teguhth cron]#

 


B. Create Cron job for restore

1. if using /etc/crontab 

[root@teguhth cron]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

56 13 17 6 *  root /data/cron/restore_test.sh
[root@teguhth cron]#

[root@teguhth ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

02 14 17 6 *  root sh /data/cron/restore_test.sh
[root@teguhth ~]#

2. if using crontab -lh
[root@teguhth ~]# crontab -l
08 14 17 6 *  /bin/bash  /data/cron/restore_test.sh
[root@teguhth ~]#


C. Check log 

[root@teguhth cron]# pwd
/data/cron
[root@teguhth cron]# cat log_restore.txt
restore mulai
Tue Jun 17 14:08:01 WIB 2025
drop & create database teguhth02;
restore selesai
Tue Jun 17 14:08:01 WIB 2025
[root@teguhth cron]#

 


D. Check log cron 

[root@teguhth ~]# tail -f /var/log/cron
Jun 12 13:01:01 teguhth CROND[8634]: (root) CMD (run-parts /etc/cron.hourly)
Jun 12 13:01:01 teguhth run-parts(/etc/cron.hourly)[8634]: starting 0anacron
Jun 12 13:01:01 teguhth run-parts(/etc/cron.hourly)[8643]: finished 0anacron
Jun 17 13:10:54 teguhth crond[708]: (CRON) INFO (Syslog will be used instead of sendmail.)
Jun 17 13:10:54 teguhth crond[708]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 3% if used.)
Jun 17 13:10:54 teguhth crond[708]: (CRON) INFO (running with inotify support)
Jun 17 13:41:01 teguhth crond[708]: (*system*) RELOAD (/etc/crontab)
Jun 17 13:41:01 teguhth crond[708]: (/bin/bash) ERROR (getpwnam() failed)
Jun 17 13:42:01 teguhth crond[708]: (*system*) RELOAD (/etc/crontab)
Jun 17 13:42:01 teguhth crond[708]: (CRON) bad command (/etc/crontab)
Jun 17 13:44:01 teguhth crond[708]: (*system*) RELOAD (/etc/crontab)
Jun 17 13:44:02 teguhth CROND[3935]: (root) CMD (/data/cron/restore_test.sh)
Jun 17 13:44:02 teguhth CROND[3934]: (root) CMDOUT (/data/cron/restore_test.sh: line 7: teguhth_ori.sql.gz: No such file or directory)
Jun 17 13:46:01 teguhth crond[708]: (*system*) RELOAD (/etc/crontab)
Jun 17 13:46:01 teguhth CROND[4199]: (root) CMD (/data/cron/restore_test.sh)
Jun 17 13:49:01 teguhth crond[708]: (*system*) RELOAD (/etc/crontab)
Jun 17 13:49:01 teguhth CROND[4480]: (root) CMD (/data/cron/restore_test.sh)
Jun 17 13:49:01 teguhth CROND[4479]: (root) CMDOUT (ERROR 1008 (HY000) at line 1: Can't drop database 'teguhth02'; database doesn't exist)
Jun 17 13:52:01 teguhth crond[708]: (*system*) RELOAD (/etc/crontab)
Jun 17 13:52:01 teguhth CROND[4899]: (root) CMD (/data/cron/restore_test.sh)
Jun 17 13:52:01 teguhth CROND[4898]: (root) CMDOUT (ERROR 1008 (HY000) at line 1: Can't drop database 'teguhth02'; database doesn't exist)
Jun 17 13:54:01 teguhth crond[708]: (*system*) RELOAD (/etc/crontab)
Jun 17 13:54:01 teguhth CROND[5158]: (root) CMD (/data/cron/restore_test.sh)
Jun 17 13:54:01 teguhth CROND[5157]: (root) CMDOUT (ERROR 1008 (HY000) at line 1: Can't drop database 'teguhth02'; database doesn't exist)
Jun 17 13:56:01 teguhth crond[708]: (*system*) RELOAD (/etc/crontab)
Jun 17 13:56:01 teguhth CROND[5370]: (root) CMD (/data/cron/restore_test.sh)
Jun 17 13:56:01 teguhth CROND[5369]: (root) CMDOUT (ERROR 1008 (HY000) at line 1: Can't drop database 'teguhth02'; database doesn't exist)

Jun 17 14:01:01 teguhth crond[708]: (*system*) RELOAD (/etc/crontab)
Jun 17 14:01:01 teguhth CROND[5946]: (root) CMD (run-parts /etc/cron.hourly)
Jun 17 14:01:01 teguhth run-parts(/etc/cron.hourly)[5946]: starting 0anacron
Jun 17 14:01:01 teguhth anacron[5955]: Anacron started on 2025-06-17
Jun 17 14:01:01 teguhth anacron[5955]: Will run job `cron.daily' in 21 min.
Jun 17 14:01:01 teguhth anacron[5955]: Jobs will be executed sequentially
Jun 17 14:01:01 teguhth run-parts(/etc/cron.hourly)[5957]: finished 0anacron
Jun 17 14:02:01 teguhth crond[708]: (*system*) RELOAD (/etc/crontab)
Jun 17 14:02:01 teguhth CROND[6086]: (root) CMD (sh /data/cron/restore_test.sh)
Jun 17 14:02:01 teguhth CROND[6085]: (root) CMDOUT (ERROR 1008 (HY000) at line 1: Can't drop database 'teguhth02'; database doesn't exist)
Jun 17 14:03:42 teguhth crontab[6298]: (root) BEGIN EDIT (root)
Jun 17 14:04:12 teguhth crontab[6298]: (root) REPLACE (root)
Jun 17 14:04:12 teguhth crontab[6298]: (root) END EDIT (root)
Jun 17 14:04:20 teguhth crontab[6394]: (root) LIST (root)
Jun 17 14:05:01 teguhth CROND[6479]: (root) CMD (root /data/cron/restore_test.sh)
Jun 17 14:05:01 teguhth CROND[6477]: (root) CMDOUT (/bin/sh: root: command not found)
Jun 17 14:05:14 teguhth crontab[6505]: (root) LIST (root)
Jun 17 14:05:24 teguhth crontab[6527]: (root) BEGIN EDIT (root)
Jun 17 14:05:35 teguhth crontab[6527]: (root) REPLACE (root)
Jun 17 14:05:35 teguhth crontab[6527]: (root) END EDIT (root)
Jun 17 14:05:37 teguhth crontab[6555]: (root) LIST (root)
Jun 17 14:06:01 teguhth crond[708]: (root) RELOAD (/var/spool/cron/root)
Jun 17 14:06:01 teguhth CROND[6605]: (root) CMD (root sh /data/cron/restore_test.sh)
Jun 17 14:06:01 teguhth CROND[6604]: (root) CMDOUT (/bin/sh: root: command not found)

Jun 17 14:07:29 teguhth crontab[6786]: (root) BEGIN EDIT (root)
Jun 17 14:07:43 teguhth crontab[6786]: (root) REPLACE (root)
Jun 17 14:07:43 teguhth crontab[6786]: (root) END EDIT (root)
Jun 17 14:08:01 teguhth crond[708]: (root) RELOAD (/var/spool/cron/root)
Jun 17 14:08:01 teguhth CROND[6858]: (root) CMD (/bin/bash  /data/cron/restore_test.sh)
Jun 17 14:08:01 teguhth CROND[6857]: (root) CMDOUT (ERROR 1008 (HY000) at line 1: Can't drop database 'teguhth02'; database doesn't exist)
Jun 17 14:08:10 teguhth crontab[6888]: (root) LIST (root)

No comments:

Post a Comment

Popular Posts