Backup
使用定時子文件夾將腳本備份到 FTP
我想製作一個備份腳本,它包含
.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 )
首先,您創建 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 )