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>
<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>保存退出