Home > Database > Mysql Tutorial > linux定时备份MySQL数据库并删除七天前的备份文件 (2014

linux定时备份MySQL数据库并删除七天前的备份文件 (2014

WBOY
Release: 2016-06-07 15:53:10
Original
1309 people have browsed it

1、创建备份文件夹 #cd /bak #mkdir mysqlbak 2、编写运行脚本 #nano -w /usr/sbin/bakmysql.sh 注:如使用 nano 编辑此代码需在每行尾添加 '' 或 ';' 连接符,否则生成的文件名末尾字符为乱码 代码: #!/bin/bash # Name:bakmysql.sh # This is a ShellScri

1、创建备份文件夹
<p><span><span>#cd /bak</span>
<span>#mkdir mysqlbak</span></span></p>
Copy after login

2、编写运行脚本
<p><span><span>#nano -w /usr/sbin/bakmysql.sh</span>
<span>注:如使用</span><span>nano</span><span>编辑此代码需在每行尾添加</span><span>'&&'</span><span>或</span><span>';'</span><span>连接符,否则生成的文件名末尾字符为乱码</span>
</span></p>
Copy after login
代码:
<p><span><span>#!/bin/bash</span>
<span># Name:bakmysql.sh</span>
<span># This is a ShellScript For Auto DB Backup and Delete old Backup</span>
<span>#</span>
<span>backupdir</span><span>=</span><span>/bak/</span><span>mysqlbak</span>
<span>time</span><span>=</span><span>`date +%Y%m%d%H`</span>
<span>mysql_bin_dir</span><span>/</span><span>mysqldump </span><span>-</span><span>u user </span><span>-</span><span>ppassword dataname1 </span><span>|</span><span> gzip </span><span>></span><span> $backupdir</span><span>/</span><span>name1$time</span><span>.</span><span>sql</span><span>.</span><span>gz</span>
<span>mysql_bin_dir</span><span>/</span><span>mysqldump </span><span>-</span><span>u user </span><span>-</span><span>ppassword dataname2 </span><span>|</span><span> gzip </span><span>></span><span> $backupdir</span><span>/</span><span>name2$time</span><span>.</span><span>sql</span><span>.</span><span>gz</span>
<span>#</span>
<span>find $backupdir </span><span>-</span><span>name </span><span>"name_*.sql.gz"</span><span>-</span><span>type f </span><span>-</span><span>mtime </span><span>+</span><span>5</span><span>-</span><span>exec</span><span> rm </span><span>{}</span><span> \; </span><span>></span><span>/dev/</span><span>null</span><span>2</span><span>>&</span><span>1</span>
</span></p>
Copy after login
保存退出
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template