In linux, mysql databases are automatically backed up on a daily basis.
Overview
Backup is the basis for Disaster Tolerance. To prevent data loss caused by system operation errors or system faults, the process of copying all or part of data sets from the application host's hard disk or array to other storage media. For some websites and systems, the database is everything, so it is crucial to back up the database!
What is backup?
Why backup?
Disaster Tolerance solution Construction
Storage Medium
CD
Tape
Hard Disk
Disk Array
DAS: Directly attached storage
NAS: network attached storage
SAN: Storage Area Network
Cloud storage
Here, we will talk about the addition and use of scheduled tasks and basic backup scripts using local disks as the storage medium. other storage media may not be accessed in the same way as media.
1. view disk space:
Since regular backup is required, You must select a disk with sufficient space to avoid backup failure and data loss due to insufficient space!
It is the simplest but least recommended to store data on the current disk. If the server has multiple hard disks, it is best to store the backup data on another hard disk; select a better and safer storage medium if conditions are met;
# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/VolGroup-lv_root 50G 46G 1.6G 97% /tmpfs 1.9G 92K 1.9G 1% /dev/shm/dev/sda1 485M 39M 421M 9% /boot/dev/mapper/VolGroup-lv_home 534G 3.6G 503G 1% /home
2. Create a backup directory:
The above command shows that/home has sufficient space, so you can consider saving the backup file in/home;
cd /homemkdir backupcd backup
3. Create a backup Shell script:
Note: replace DatabaseName in the following command with the actual database name;
Of course, you can also use the actual naming rules!
vi bkDatabaseName.sh
Enter/paste the following content:
#!/bin/bashmysqldump -uusername -ppassword DatabaseName > /home/backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sql
Compress the backup:
#!/bin/bashmysqldump -uusername -ppassword DatabaseName | gzip > /home/backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sql.gz
Note:
Replace username with the actual user name;
Replace password with the actual password;
Replace DatabaseName with the actual database name;
4. Add executable permissions:
chmod u+x bkDatabaseName.sh
After adding the executable permission, execute it to check whether the script is correct and whether it can be used properly;
./BkDatabaseName. sh
5. Add a scheduled task
Checks or installs crontab
Check whether crontab is installed:
Execute the crontab command. If command not found is reported, it indicates that the crontab is not installed.
# crontab-bash: crontab: command not found
If crontab is not installed, install it first. For detailed steps, see:
Run the yum command in CentOS to install the crontab program of the scheduler.
Run the rpm command to install the scheduled task program crontab from the CentOS system disk.
Add scheduled task
Run the following command:
crontab -e
In this case, you can edit a scheduled task just like using the vi editor.
Enter and save the following content:
*/1 * * * * /home/backup/bkDatabaseName.sh
What does it mean?
The shell script "/home/backup/bkDatabaseName. sh" is executed every minute ".
6. test whether the task is executed
It's easy to execute the "ls" command several times to see if the file has been created one minute later!
If the task fails to be executed, run the following command to view the task log:
# tail -f /var/log/cron
The output is similar to the following:
Sep 30 14:01:01 bogon run-parts(/etc/cron.hourly)[2503]: starting 0anacronSep 30 14:01:01 bogon run-parts(/etc/cron.hourly)[2512]: finished 0anacronSep 30 15:01:01 bogon CROND[3092]: (root) CMD (run-parts /etc/cron.hourly)Sep 30 15:01:01 bogon run-parts(/etc/cron.hourly)[3092]: starting 0anacronSep 30 15:01:02 bogon run-parts(/etc/cron.hourly)[3101]: finished 0anacronSep 30 15:50:44 bogon crontab[3598]: (root) BEGIN EDIT (root)Sep 30 16:01:01 bogon CROND[3705]: (root) CMD (run-parts /etc/cron.hourly)Sep 30 16:01:01 bogon run-parts(/etc/cron.hourly)[3705]: starting 0anacronSep 30 16:01:01 bogon run-parts(/etc/cron.hourly)[3714]: finished 0anacronSep 30 16:15:29 bogon crontab[3598]: (root) END EDIT (root)
Summary
The above section describes how to implement daily automatic backup of mysql Databases in linux. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!