您的位置:首页 > 数据库

简单的shell脚本备份数据库和代码

2011-06-03 18:13 537 查看
备份数据库

#!/bin/bash

DATE=`date +%Y-%m-%d`

mysqldump --opt dbname -u username -ppassword | gzip > /bakdir/dbbak/test$DATE.gz

#红色标记根据自己的情况进行修改

备份代码

#!/bin/bash

#运行脚本要给用户执行权限

bakdir=/bakdir/picbak

month=`date +%m`

day=`date +%d`

year=`date +%Y`

#dirname=$year-$month-$day-$hour-$min

gzupload=$year-$month-$day

cd /opt/backpic

tar -czf $bakdir/$gzupload-pic.tgz .

----------------------------------------------------------------------------------

tar cannot stat

Wtf? you might reasonably exclaim, unless you’re used to using *nix (Linux, UNIX, etc) from the command line.

tar is a command for moving a file or files into a single archive file (or for reversing the operation). It can also apply compression/decompression algorithms, and perform various other neat tricks. It’s useful and ubiquitous. Unfortunately, it’s also quite sensitive.

Just now, I was using tar -cfz mytarfile.tar.gz mydirectory to archive a directory, and this returned the error, tar: mytarfile.tar.gz: Cannot stat: No such file or directory

tar: Error exit delayed from previous errors. Hmm, not good.

Easy to fix, though. The solution to the problem is this: I should have written, tar -czf mytarfile.tar.gz mydirectory. In other words, the z and the f get swapped in the options. This is because the f option is supposed to denote that what follows it is a file. So the first command I tried was looking for a file called mytarfile.tar.gz to add to an archive file called z. Since the mytarfile.tar.gz didn’t exist, tar couldn’t locate, or stat, the file. Hence the error.

Oh, what fun is to be had at the command line!
本文出自 “Mr. Kang” 博客,请务必保留此出处http://kangyang.blog.51cto.com/471772/580852
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: