1. Check the disk space:
Since it is a regular backup, it is necessary to choose a disk space with sufficient space to avoid backup failure and data loss due to insufficient space!
It is the easiest to store to the current disk, but it is the least recommended; if the server has multiple hard disks, it is best to store the backup on another hard disk; if possible, choose a better and safer storage medium;
# df -h
Filesystem 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:
We used the command above to see that there is sufficient space under /home, so we can consider saving backup files in /home;
cd /home
mkdir backup
cd backup
3. Create a backup shell script:
Note that the DatabaseName in the following command is replaced by the actual database name; of
course, you can also use the actual naming convention!
vi bkDatabaseName.sh
Type/Paste the following:
#!/bin/bash
mysqldump -uusername -ppassword DatabaseName > /home/backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sql
#!/bin/bash
mysqldump -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 permission:
chmod u+x bkDatabaseName.sh
After adding the executable permission, execute it first to see if the script has errors and whether it can be used normally;
./bkDatabaseName.sh
5. Add scheduled tasks
Detect or install crontab
Confirm whether crontab is installed:
If the command not found is reported when the crontab command is executed, it means that it is not installed.
# crontab
-bash: crontab: command not found
If crontab is not installed, you need to install it first
Add scheduled tasks
Excuting an order:
crontab -e
At this time, just like using the vi editor, you can edit the scheduled tasks.
Enter the following and save:
*/1 * * * * /home/backup/bkDatabaseName.sh
What exactly does that mean?
It means to execute the shell script "/home/backup/bkDatabaseName.sh" every minute.
The following means that the script is executed once a day at 0:00
0 0 * * * /home/backup/bkDatabaseName.sh
6. Test whether the task is executed
It is very simple, we just execute the "ls" command several times to see if the file has been created after one minute!
If the task execution fails, you can view the task log with the following command:
# 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 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)
Reprinted from: http://blog.csdn.net/testcs_dn/article/details/48829785
Thanks for sharing! ! !