linux下备份MYSQL数据库的方法
程序员文章站
2023-12-04 10:59:04
只需按照下面3步做,一切都在你的掌控之下: 第一步:在服务器上配置备份目录代码: ---------------------------------------------...
只需按照下面3步做,一切都在你的掌控之下:
第一步:在服务器上配置备份目录代码:
--------------------------------------------------------------------------------
mkdir /var/lib/mysqlbackup
cd /var/lib/mysqlbackup
--------------------------------------------------------------------------------
第二步:编写备份脚本代码:
--------------------------------------------------------------------------------
vi dbbackup.sh
--------------------------------------------------------------------------------
粘帖以下代码,务必更改其中的username,password和dbname。
代码:
--------------------------------------------------------------------------------
#!/bin/sh #username to access the mysql server username="username" # username to access the mysql server password="password" # list of dbnames for backup dbname="dbname" #date timestamp for log message date=`/bin/date +%y-%m-%d_%hh%mm` #output file outdir="/var/lib/mysqlbackup/" outfile="ip_bindass".$date."sql.gz" #working directory dir="/var/lib/mysqlbackup/"#cd $dir # mysql backup /usr/bin/mysqldump --database $dbname --opt --single-transaction -u$username -p$password | /usr/bin/gzip -9 > $outdir$outfile
--------------------------------------------------------------------------------
更改备份脚本权限
代码:
--------------------------------------------------------------------------------
chmod +x dbbackup.sh
--------------------------------------------------------------------------------
第三步:用crontab定时执行备份脚本代码:
--------------------------------------------------------------------------------
crontab -e
--------------------------------------------------------------------------------
若每天下午3点20备份,添加如下代码,
代码:
--------------------------------------------------------------------------------
20 15 * * * /var/lib/mysqlbackup/dbbackup.sh
--------------------------------------------------------------------------------
搞定!
第一步:在服务器上配置备份目录代码:
--------------------------------------------------------------------------------
mkdir /var/lib/mysqlbackup
cd /var/lib/mysqlbackup
--------------------------------------------------------------------------------
第二步:编写备份脚本代码:
--------------------------------------------------------------------------------
vi dbbackup.sh
--------------------------------------------------------------------------------
粘帖以下代码,务必更改其中的username,password和dbname。
代码:
--------------------------------------------------------------------------------
#!/bin/sh #username to access the mysql server username="username" # username to access the mysql server password="password" # list of dbnames for backup dbname="dbname" #date timestamp for log message date=`/bin/date +%y-%m-%d_%hh%mm` #output file outdir="/var/lib/mysqlbackup/" outfile="ip_bindass".$date."sql.gz" #working directory dir="/var/lib/mysqlbackup/"#cd $dir # mysql backup /usr/bin/mysqldump --database $dbname --opt --single-transaction -u$username -p$password | /usr/bin/gzip -9 > $outdir$outfile
--------------------------------------------------------------------------------
更改备份脚本权限
代码:
--------------------------------------------------------------------------------
chmod +x dbbackup.sh
--------------------------------------------------------------------------------
第三步:用crontab定时执行备份脚本代码:
--------------------------------------------------------------------------------
crontab -e
--------------------------------------------------------------------------------
若每天下午3点20备份,添加如下代码,
代码:
--------------------------------------------------------------------------------
20 15 * * * /var/lib/mysqlbackup/dbbackup.sh
--------------------------------------------------------------------------------
搞定!