备份数据库

#!/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!