Backup is the basis of DISASTER recovery (Dr). It is a process of copying all or part of data sets from disks or arrays of application hosts to other storage media to prevent data loss caused by system errors or system faults. And for some sites, systems, the database is everything, so do a good database backup is crucial!

What is a backup?

Why backup

Disaster recovery plan construction

A storage medium

  • CD
  • tape
  • The hard disk
  • Disk array
  • DAS: Directly attached storage
  • NAS: Network attached storage
  • SAN: storage area network
  • Cloud storage

Here, the local disk is used as the storage medium to talk about the addition and use of scheduled tasks, basic backup scripts, other storage media but the access mode of media may be different.

1. Check the disk space: Since it is a scheduled backup, choose a disk space with sufficient space to avoid backup failure and data loss due to insufficient space!

Save to current disk this is easiest, but least recommended; The server has multiple hard drives, so it is best to store backups on another hard drive. Choose a better and safer storage medium when conditions permit;

# df -hfilesystem = /home; # df -hfilesystem = /home;

cd /home
mkdir backupcd backup
Copy the code

Create a backup Shell script: replace DatabaseName with the actual DatabaseName; Of course, you can also use the actual naming rules!

Vi bkDatabaseName. Sh Enter/paste the following content:

#! /bin/bash mysqldump -uusername -ppassword DatabaseName > /home/backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sqlCopy the code

Compress the backup:

#! /bin/bash mysqldump -uusername -ppassword DatabaseName | gzip > /home/backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sql.gzCopy the code

Note:

Replace username with the actual username. Replace password with the actual password; Replace DatabaseName with the actual DatabaseName;

4, add executable permission: chmod u+x bkDatabaseName.

/bkDatabaseName. Sh 5. Add scheduled task detection or install crontab

Check whether crontab is installed:

If command not found is displayed, the crontab command is not installed

# crontab
-bash: crontab: command not found
Copy the code

If crontab is not installed, you need to install it first. For details, see:

Command yum to install scheduled task program crontab blog.csdn.net/testcs_dn/a…

Run the RPM command to install the scheduled task program crontab blog.csdn.net/testcs_dn/a…

Adding a Scheduled Task

Execute command:

Crontab -e now allows you to edit scheduled tasks just like using the VI editor.

Enter the following and save:

*/1 * * * * /home/backup/bkDatabaseName.sh
Copy the code

What exactly does that mean?

Mean every minute to perform a shell script “/ home/backup/bkDatabaseName. Sh”.

6, test whether the task is executed is very simple, we just run the “ls” command a few times, to see if the file is created after a minute!

If the task fails to be executed, run the following command to view the task log:

# tail -f /var/log/cron

Sep 30 14:01:01 bogon run-parts(/etc/cron.hourly)[2503]: starting 0anacron
Sep 30 14:01:01 bogon run-parts(/etc/cron.hourly)[2512]: finished 0anacron
Sep 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 0anacron
Sep 30 15:01:02 bogon run-parts(/etc/cron.hourly)[3101]: finished 0anacron
Sep 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 0anacron
Sep 30 16:01:01 bogon run-parts(/etc/cron.hourly)[3714]: finished 0anacron
Sep 30 16:15:29 bogon crontab[3598]: (root) END EDIT (root)
Copy the code