Maison  >  Article  >  base de données  >  linux定时备份MySQL数据库并删除七天前的备份文件 (2014

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

WBOY
WBOYoriginal
2016-06-07 15:53:101286parcourir

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>

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>
代码:
<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>
保存退出
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:dos下进入mysql环境Article suivant:Java与MySql数据库对应类型