用于将文件备份到远程 FTP 的 Bash 脚本。删除旧文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12975588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash script to backup files to remote FTP. Deleting old files
提问by Sebastien
I'm writing a bash script to send backups to a remote ftp server. The backup files are generated with a WordPress plugin so half the work is done for me from the start.
我正在编写一个 bash 脚本来将备份发送到远程 ftp 服务器。备份文件是使用 WordPress 插件生成的,所以从一开始就为我完成了一半的工作。
The script does several things.
该脚本做了几件事。
- It looks in the local backup dir for any files older than x and deletes them
- It connects to FTP and puts the backup files in a dir with the current date as a name
- It deletes any backup dirs for backups older than x
- 它在本地备份目录中查找任何早于 x 的文件并删除它们
- 它连接到 FTP 并将备份文件放在一个以当前日期为名称的目录中
- 它删除比 x 早的备份的所有备份目录
As I am not fluent in bash, this is a mishmash of a bunch of scripts I found around the net.
由于我不精通 bash,这是我在网上找到的一堆脚本的大杂烩。
Here is my script:
这是我的脚本:
#! /bin/bash
BACKDIR=/var/www/wp-content/backups
#----------------------FTP Settings--------------------#
FTP=Y
FTPHOST="host"
FTPUSER="user"
FTPPASS="pass"
FTPDIR="/backups"
LFTP=$(which lftp) # Path to binary
#-------------------Deletion Settings-------------------#
DELETE=Y
DAYS=3 # how many days of backups do you want to keep?
TODAY=$(date --iso) # Today's date like YYYY-MM-DD
RMDATE=$(date --iso -d $DAYS' days ago') # TODAY minus X days - too old files
#----------------------End of Settings------------------#
if [ -e $BACKDIR ]
then
if [ $DELETE = "Y" ]
then
find $BACKDIR -iname '*.zip' -type f -mtime +$DAYS -delete
echo "Old files deleted."
fi
if [ $FTP = "Y" ]
then
echo "Initiating FTP connection..."
cd $BACKDIR
$LFTP << EOF
open ${FTPUSER}:${FTPPASS}@${FTPHOST}
mkdir $FTPDIR
cd $FTPDIR
mkdir ${TODAY}
cd ${TODAY}
mput *.zip
cd ..
rm -rf ${RMDATE}
bye
EOF
echo "Done putting files to FTP."
fi
else
echo "No Backup directory."
exit
fi
There are 2 specific things I can't get done:
有两件我无法完成的具体事情:
- The find command doesn't delete any of the old files in the local backup dir.
- I would like mput to only put the .zip files that were created today.
- find 命令不会删除本地备份目录中的任何旧文件。
- 我希望 mput 只放置今天创建的 .zip 文件。
Thanks in advance for the help.
在此先感谢您的帮助。
采纳答案by Janito Vaqueiro Ferreira Filho
To send only zip files that were created today:
仅发送今天创建的 zip 文件:
MPUT_ZIPS="$(find $BACKDIR -iname '*.zip' -type f -maxdepth 1 -mtime 1 | sed -e 's/^/mput /')"
[...]
$LFTP << EOF
open ${FTPUSER}:${FTPPASS}@${FTPHOST}
mkdir $FTPDIR
cd $FTPDIR
mkdir ${TODAY}
cd ${TODAY}
${MPUT_ZIPS}
cd ..
rm -rf ${RMDATE}
bye
EOF
Hope this helps =)
希望这有帮助 =)
回答by Olaf Dietsche
2) If you put todays backup files in a separate directory or link them to a separate directory, you can cd todayand just transfer these files.
2)如果你把今天的备份文件放在一个单独的目录下或者链接到一个单独的目录,你可以cd today并且只传输这些文件。

