我想制作一个备份脚本,创建一个.tar.gz
我定义的文件夹,比如 fx/root/tekkit/world
然后应将此.tar.gz
文件上传到 FTP 服务器,并按上传时间命名,例如:07-10-2012-13-00.tar.gz
这样的备份脚本应该怎么写呢?
我已经弄清楚了这.tar.gz
部分内容——只需要命名并上传到 FTP。
我知道 FTP 不是最安全的方法,但是因为它是非敏感数据,而且 FTP 是我唯一的选择,所以它可以。
编辑:
我最终得到了这个脚本:
#!/bin/bash
# have some path predefined for backup unless one is provided as first argument
BACKUP_DIR="/root/tekkit/world/"
TMP_DIR="/tmp/tekkitbackup/"
FINISH_DIR="/tmp/tekkitfinished/"
# construct name for our archive
TIME=$(date +%d-%m-%Y-%H-%M)
if [ $1 ]; then
BACKUP_DIR="$1"
fi
echo "Backing up dir ... $BACKUP_DIR"
mkdir $TMP_DIR
cp -R $BACKUP_DIR $TMP_DIR
cd $FINISH_DIR
tar czvfp tekkit-$TIME.tar.gz -C $TMP_DIR .
# create upload script for lftp
cat <<EOF> lftp.upload.script
open server
user user password
lcd $FINISH_DIR
mput tekkit-$TIME.tar.gz
exit
EOF
# start backup using lftp and script we created; if all went well print simple message and clean up
lftp -f lftp.upload.script && ( echo Upload successfull ; rm lftp.upload.script )
答案1
首先,您创建一个 cron 作业,该作业在您想要创建备份时调用,创建备份后,脚本将生成 lftp 脚本以通过 FTP 上传文件。
#!/bin/bash
# have some path predefined for backup unless one is provided as first argument
BACKUP_DIR="/tmp/test"
# construct name for our archive
TIME=$(date +%d-%m-%Y-%H-%M)
if [ $1 ]; then
BACKUP_DIR="$1"
fi
echo "Backing up dir ... $BACKUP_DIR"
tar czvfp $TIME.tar.gz $BACKUP_DIR
# create upload script for lftp
cat <<EOF> lftp.upload.script
open ftp.server.tld
user username password
cd /target/directory
lcd /local/dir/where/tar/gz/archive/is
mput $TIME.tar.gz
exit
EOF
# start backup using lftp and script we created; if all went well print simple message and clean up
lftp -f lftp.upload.script && ( echo Upload successfull ; rm lftp.upload.script )
答案2
简单来说:
DATE=`date +%d-%m-%Y-%H-%M`
tar cfz ${DATE}.tar.gz /root/tekkit/world
或者,减少输入:
tar cfz $(date +%d-%m-%Y-%H-%M).tar.gz /root/tekkit/world
会发生什么?
$ DATE=`date +%d-%m-%Y-%H-%M`
$ echo $DATE
07-10-2012-13-15