当前位置:网站首页>MySQL backup and manual execution of shell scripts are OK, and crontab scheduled execution fails

MySQL backup and manual execution of shell scripts are OK, and crontab scheduled execution fails

2022-06-10 20:02:00 Small Gunner

Backup scripts

vim mysql_backup.sh

#!/bin/bash

source /etc/profile
source ~/.bash_profile
 
#  Saved Directory 
data_dir="****"
#  Save the name of the file , Be careful date Yes, it is # Backquotes # Wrapped up  
data_name="bak_`date +%Y%m%d%H`.sql"
 
if [ ! -d $data_dir ]; then
  mkdir -p $data_dir
fi
 
# mysqldump Backup command 
# -uroot    user name 
# -p123456  password 
# test  Database name 
mysqldump -uroot -p123456 test > $data_dir/$data_name
 

Backup file

vim remove_backup.sh

#/bin/bash

source /etc/profile
source ~/.bash_profile
#  Saved Directory 
data_dir="****" 

#  Delete 7 Days ago backup files 
find $data_dir -mtime +7 -type f -name "bak_*.sql" ;

atime、ctime、mtime

atime: Access time (access time), It refers to the time when the file or directory was last accessed ;
ctime: Change the time (change time), It refers to the last change of file or directory ( What's changed is the original data, that is : attribute ) Time for ;
mtime: Modification time (modify time), It refers to the time when the file or directory was last modified ;
Use stat Command to view three time values : Such as stat filename

find Find based on time

mtime The parameters are as follows :
-mtime n according to ⽂ Change the time to find ⽂ Pieces of ,n Integers .
n surface ⽰⽂ The change time distance is n God
-n surface ⽰⽂ The change time distance of the piece is n Within days
+n surface ⽰⽂ The change time distance of the piece is n Before the day
 

for example :
-mtime 0 surface ⽰⽂ The modification time distance of this document is currently 0 Days of ⽂ Pieces of , That is less than the current time 1 God (24⼩ when ) Inside ⽂ Pieces of .
-mtime 1 surface ⽰⽂ The modification time distance of this document is currently 1 Days of ⽂ Pieces of , That is, from the current time 1 God (24⼩ when -48⼩ when ) Of ⽂ Pieces of .
-mtime+1 surface ⽰⽂ The modification time of this document is ⼤ On 1 Days of ⽂ Pieces of , That is, from the current time 2 God (48⼩ when ) In addition to the ⽂ Pieces of
-mtime -1 surface ⽰⽂ The modification time of this document is ⼩ On 1 Days of ⽂ Pieces of , That is, from the current time 1 God (24⼩ when ) Within ⽂ Pieces of
 

Grant authority

chmod   755 ***.sh

Timing task

crontab -e


0 */2 * * * root  mysql_backup.sh # Every time 2 Hours 

0 1 * * * root remove_backup.sh #  Every morning 1 spot , Automatically delete 7 Days ahead backup 

shell The script can be executed manually ,crontab Scheduled execution failed

Problem description :

   Shell The script can be executed manually and run normally , And get the right results ; Use Crontab When scheduling ,Shell The amount of result data from the execution of the script is 0.

reason :

     Linux Next use crontab Executing scheduled tasks will not default from the user profile Read the environment variable parameters from the file , So it often leads to success when executing a script manually , But to crontab When you try to make it execute regularly, you will make an error . This is because the user logs in Linux When operating the system ,”/etc/profile”, “~/.bash_profile” And other configuration files will be executed automatically , and crontab The configuration file may not be executed during scheduled scheduling .
 

Solution :

Shell The script defaults to  #!/bin/sh  start Line break Add... On the first line after

source /etc/profile
source ~/.bash_profile

原网站

版权声明
本文为[Small Gunner]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101741137582.html