We often use Svn or Git to manage our code or project-related files during development, but they all have one thing in common: version control
Many people will ask why they use Svn or Git. Then I will ask: I believe your code is good, but one day you fix a lot of bugs, but suddenly it occurs to you that an important algorithm in the previous code has been accidentally overwritten, what should you do?
Maybe you’ll tell me that you can back up the current file, or the current project, before you change the code.
But how many backup files do you have on your machine over time?
If one day your colleague needed to go back to the previous code to see the cause of the Bug, would you find the file from your machine at the specified time and send it to him?
Of course not, we have Svn or Git after we can easily see the previous version of the file, also can easily compare. So now it’s clear how important version control is.
The same goes for databases, which are 99% of our systems because we’re always storing data.
What if our database is deleted by mistake, hacked, or maliciously modified?
So we can try to let our server automatically to help us back up the database data to the specified location, in case one day our database accidentally shake, we can also use the backup database script directly restore.
On the script:
# db username
db_user="test"
# db password
db_passwd="test"
# db host
db_host="121.201.5.216"
# backup db name
db_name="Alterem"
# the directory for story your backup file.
backup_dir="backup" cd
# date format for backup file (dd-mm-yyyy)
time="$(date +"%Y-%m-%d-%H-%M-%S")"
# mysql, mysqldump and some other bin's path
MYSQL="/usr/local/mysql/bin/mysql"
MYSQLDUMP="/usr/local/mysql/bin/mysqldump"
GZIP="/bin/gzip"
$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db_name | $GZIP -9 > "/data/backup/$backup_dir/$db_name"_"$time.gz"Copy the code
Creating a Timer
crontab -eCopy the code
Adding a timer
30 1 * * * /root/mysql-backup.shCopy the code
Restart timer
/etc/rc.d/init.d/crond restartCopy the code
Cron parsing
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executedCopy the code
Online Cron expression generator: http://cron.qqe2.com/
This article is published by OpenWrite!