Use bash script in linux--regular automatic backup database important files

1. First write your own bash script and give it executable permissions. The code is as follows:

[git_backup.sh]

 1 #!/bin/bash
 2 code_dir="/home/slamgit/code"
 3 bak_code_dir="/media/ubuntu/KINGSTON/code"
 4 function code_backup(){
 5     for element in `ls $code_dir`
 6     do  
 7         dir_respo=$code_dir"/"$element
 8         git clone $dir_respo $bak_code_dir"/"$element
 9     done
10 }
11 
12 
13 while :
14 do
15     current_date="`date +%c`" 
16     echo $current_date
17     if echo $current_date | grep "23时59分59秒" ; then
18         echo "start code backup"
19         if ls /media/ubuntu/KINGSTON ; then
20             rm /media/ubuntu/KINGSTON/code/* -rf
21             code_backup
22             echo "exe code backup":$current_date >> /media/ubuntu/KINGSTON/backup.log
23         fi
 24      fi
 25  done

First define two variables, the purpose is to back up the data in /home/slamgit/code to /media/ubuntu/KINGSTON/code at a glance.

Then a function is defined to perform the backup operation, loop through all the data files in the data directory, and perform the backup operation respectively.

The last is the main loop, which loops to detect whether the current time is the time when the data needs to be backed up. When the conditions are met, the old backup data in the backup directory is deleted, the backup execution function is called, and the backup operation is written to the log.

 

2. Set up the boot script

In the /etc/rc.local file, add a sentence ./home/ubuntu/git_backup.sh before the line of the statement exit 0

Restart the system and the setup is complete.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324521113&siteId=291194637