-04- clouds above a zebra -mysql implemented automatically back

Original link: https: //blog.csdn.net/qq_31659985/java/article/details/84668379

mysql_backup.sh for backing up data

mysql_restore.sh used to recover data

remove_backup.sh for a period of time before delete backup files

First, data backup

1. Create a backup directory

  We put the backup file / data / backup / mysql below, the following script in / data / backup

  Create a folder: mkdir -p / data / backup / mysql

2. Create a script file

  Create a file mysql_backup.sh

   

   we mysql_backup.sh 

#! / bin / bash 
# necessary, change yourself where oh 
#db_user = ' root ' 
#db_password = `CAT / the Data / the WWW / mysql_password` 
db_name = ' the Test ' 
BACKUP_DIR = ' / the Data / Backup / MySQL / ' 
CURRENT_TIME = $ (DATE + ' %% Y-M- D_% H% M% S% ' ) 
filepath = $ $ BACKUP_DIR CURRENT_TIME ' .sql.gz ' 
# not used here $ db_password $ db_user, has been written to the profile 
echo ' to start the export database ... ' 
mysqldump --defaults-Extra-File = / the Data / Backup / my_mysql.cnf $ db_name | gzip>filepath $ 
echo ' export is successful, the file name: ' $ filepath
mysql_backup.sh 

The above script does not use the $ db_passoword, if necessary, we can not create a configuration file directly to the -p $ db_password -u $ db_user stitching to the back mysqldupm command

Use the command gzip compression, you can save 80% space
mysql 5.6 or more will be prompted to enter the password on the command line of insecurity, there will be a row notice, but can also be exported successfully
if the direct use of a password, will report the following warning
mysqldump: [ Warning] Using a password on the command line interface can be insecure.
settlement is not being given the password is written to the configuration file, refer to the following contents of the file, the configuration file we can create a

vi my_mysql.cnf

[mysqldump]
max_allowed_packet    = 400M
host=11.19.113.203
user=root
password='111111111111'
[mysql]
host=11.19.113.203
user=root
password='111111111111'
View Code

 We need to put the above parameters into their own just fine, do not missed this point oh.

Mysqldump wherein the parameters are derived using a command, the parameters used when mysql is introduced

3. Assign executable permission to the script

  chmod +x ./mysql_backup.sh

4. Test the script

  Export

  sh ./mysql_backup.sh

  View

  ll ./mysql 

  Extract and compare file sizes

  gzip -dc ./mysql/2020-03-26_223108.sql.gz > ./mysql/2020-03-26_223108.sql

 

 

5. If the error during execution

  mysql_backup.sh: line 11: mysqldump: command not found

1, first set out to find a path with a find command mysqldump

find  / -name mysqldump -print

2, mysql: command not found establish a flexible connection (the one that we have been established)
ln -s / usr / local / MySQL / bin / MySQL / usr / bin
3, mysqldump: the Command not found establish soft link
ln -s / usr / local / mysql / bin  / mysqldump / usr / bin

 

 

Second, data recovery

 

1. Restore directory 

  We have put the backup file / data / backup / mysql Now, the backup script on the following / data / backup 

  Similarly, placed below the mysql_restore.sh files / data / backup 

2. Create a script file 

  Create a file mysql_restore.sh  

   we mysql_restore.sh 

#! / bin / the bash 

IF [the -Z $ . 1 ] || [! -f $ . 1 ] 
the then 
    echo " Please enter sql compressed file (* .sql.gz) " 
    Exit . 1 
Fi 

db_name = ' Test ' 
base_dir = ' / Data / Backup / MySQL / ' 
gz_sql_file = $ `the basename . 1 ` 

file_ext = $ {gz_sql_file ## * .}
 IF [file_ext $! = ' GZ ' ] 
the then 
    echo ' file format is not correct, enter .sql.gz file ' 
    exit. 1 
Fi 

sql_file = $ {. Gz_sql_file% * } 
echo ' extracting file ... ' 
the gzip -dc $ base_dir gz_sql_file $> $ $ sql_file base_dir 
echo ' extraction is complete. ' 
Echo ' start importing database ... ' 

MySQL - = Extra-file-Defaults / Data / Backup / db_name my_mysql.cnf $ < $ $ sql_file base_dir 

IF [- F $ $ base_dir sql_file] 
the then 
    echo ' delete temporary files. ' 
    RM - F $ $ sql_file base_dir 
Fi 
echo ' import complete . '
mysql_restore.sh

The code above configuration file, the configuration file is created when the first step of us here the same use

3. Assign executable permission to the script

  chmod +x ./mysql_restore.sh

4. Test the script

  View the database,

  

 

   Delete the database, (important database does not recommend doing this)

 

  By script recovery

  sh ./mysql_restore.sh ./mysql/ backup file name .sql.gz

  If the database does not exist, it must first establish a database name to be created and (mysql_restore.sh set the same)

  Recovery is complete (welcome back Joe Smith)

   

 

Third, create a scheduled task

 

1. Create a script file

-e crontab
# paste the following content, everyone according to their need to change it
0 1,12 * * * /data/backup/mysql_backup.sh # every day 1:00, 12:00 Back Up Data

 

# Here we can first use this as a test Oh, one minute running time, we only need to look at ll / data / backup / mysql this folder like
* / 1 * * * * /data/backup/mysql_backup.sh

2. Test

 

Fourth, add automatic backup cleanup

 

1. automatically deleted 

  We have put the backup file / data / backup / mysql Now, the backup and restore scripts placed below / data / backup 

  Similarly, placed below the remove_backup.sh files / data / backup 

2. Create a script file 

  Create a file remove_backup.sh  

   we remove_backup.sh

# / bin / bash 
 
# delete backup one day before 
 the Find / the Data / Backup / MySQL -mtime The -type f + 1 | xargs -f RM
remove_backup.sh

If it is, delete 30 days ago, the corresponding position changed to 30
  the Find / the Data / Backup / MySQL +30 -mtime The -type f | xargs -f RM

3. Assign executable permission to the script

  chmod +x ./remove_backup.sh

4. Create a scheduled task  

crontab - E 
# paste the following content, everyone according to their needs change can be a 
0  1 , 12 * * * / the Data / Backup / remove_backup.sh # every day 1:00, 12:00 Back Up Data 

 

# we can first use below this as a test, oh, two minutes running time, we only need to look at LL / the Data / Backup / MySQL this folder like
 * / 2 * * * * /data/backup/remove_backup.sh
View Code

5. test scripts

5.1 Preparations

 Modify (delete script) remove_backup.sh file set the time to delete the half-hour historical documents

   find /data/backup/mysql -type f -mmin +30 | xargs rm -f

  Modifying (scheduled task) the time is set to the crontab not two minutes to perform a

  find /data/backup/mysql -type f -mtime +30 | xargs rm -f

 Scheduled tasks arranged per minute

 

 

 

 

5.2 began to observe

Before deleting

 

 

 

 After deleting

 

 

 

5.2 knock off adjustment

  1: The scheduled tasks set to back up at 3:00 am every day. 5:00 in the morning every day to delete outdated backups

 

 

 

  2: The retention time delete the backup modified to 15 days

  

 

 

Fifth, incremental backup and delete

 

Guess you like

Origin www.cnblogs.com/YK2012/p/12576327.html