首頁 > 資料庫 > mysql教程 > linux定时备份MySQL数据库并删除七天前的备份文件 (2014

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

WBOY
發布: 2016-06-07 15:53:10
原創
1299 人瀏覽過

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>
登入後複製
保存退出
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板