当前位置:网站首页>Windows and Linux regularly backup MySQL database

Windows and Linux regularly backup MySQL database

2022-06-26 10:44:00 MervynLammm

Backup scripts

I wrote one before Mysql Backup and restore commands , Today's backup script is mentioned in this article mysqldump.

mysqldump yes mysql The built-in client-side tool to dump the database for backup . It is often used to back up databases

# mysqldump -h host  -P port  -u user  -p password  [ Options ] >  Dump target file 
mysqldump -h127.0.0.1 -P3306 -uroot -p --all-databases > E:\all.sql
#  among  --all-databases  To dump all databases 
#  There are often  --databases, Specify one or more databases 
# --ignore-table, Exclude a table 
#  Other options search and learn by yourself 

Timing task

Linux

Linux Use crontab Implement automatic script execution .

  1. establish backup.sh file , Writing backup statements , Grant execution permission chmod a+x backup.sh
  2. Set up crontab Timing task
    1. Open profile crontab -e
    2. Join a scheduled task 0 0 * * * /root/backup.sh, For example, every day 0 When the script is executed
    3. preservation

Windows

  1. establish backup.bat file , Writing backup statements .
  2. Search task scheduler and open .
  3. Create basic tasks
    1. Fill in the name
    2. Triggers are filled in according to their own needs
    3. Select backup script file
    4. preservation

Delete obsolete backup files

Backing up the database every day will eventually lead to too many backup files , It is useless to back up files 、 Outdated issues . You can add delete file statements to a scheduled task script . Implementation keeps only files after the specified date .

Linux

find /root/backup -mtime +7 -name "all*.sql" -exec rm -Rf {
    } \;
#  lookup /root/backup Under the folder ,7 Days ago , The name is "all*.sql" The file of , Delete 
# mtime Is the time condition for file filtering ,+n by n Days ago, ,-n by n Days. 

Combined with backup statements

find /root/backup -mtime +7 -name "all*.sql" -exec rm -Rf {
    } \;
mysqldump -h127.0.0.1 -P3306 -uroot -p123456 --all-databases > /root/backup/all_$(date "+%Y%m%d%H%M%S").sql
#  Backup the database to the specified directory /root/backup in , The file is named all_ Date and time at the time of backup .sql
#  Delete 7 Specified file days ago 
#  It can be realized by cooperating with scheduled tasks , Automatically back up the database every day , And only the most recent... Is kept in the backup folder 7 Days of backup data .

Windows

forfiles /p "E:\backup" /m "all*.sql" /d -7 /c "cmd /c del /f /q /a @path"
# /p  specify the path to a file 
# /m  Find the filename 
# /d  Specify Date ,-7 by 7 Days ago, 
# /c  Run command line 
# @path  Return the full path of the file 
#  Delete the specified directory 、 name , Seven days ago 

Combined with backup statements

mysqldump -h127.0.0.1 -P3306 -uroot -p123456 --all-databases > /root/backup/all_$(date "+%Y%m%d%H%M%S")[email protected] off
:: demonstration : Delete the specified days before the specified path ( The last modification date of the document shall prevail ) The file of .
:: If the demonstration results are correct , hold del Ahead echo Get rid of , You can really delete .
:: This example requires Win7/Win2008/Win2012/Win2016 Self contained forfiles Command support 

rem  Specify the storage path of the file to be deleted 
set SrcDir=E:\backup\
rem  Specify the number of days 
set DaysAgo=7
forfiles /p %SrcDir% /m "all*.sql" /d -%DaysAgo% /c "cmd /c echo del /f /q /a @path"
mysqldump -h127.0.0.1 -P3306 -uroot -p123456 --all-databases > E:\backup\all_%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%.sql
#  Backup the database to the specified directory E:\backup\ in , The file is named all_ Date and time at the time of backup .sql
#  Delete 7 Specified file days ago 
#  It can be realized by cooperating with scheduled tasks , Automatically back up the database every day , And only the most recent... Is kept in the backup folder 7 Days of backup data .

Reference material

The batch bat Script deletes files before the specified number of days

Linux Batch delete files by time ( Delete N It's a document )

【 The batch 】 Print the current date and time

原网站

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