我需要将计算机中的文件与我只能通过 FTP 访问的服务器同步,并且我希望在 crontab 中添加一行,每天检查新文件或更新的文件是否被传输(覆盖)。
这个问题几年前就被问过,但没有简单的答案,我想知道今天是否有比 ncftput
更好的解决方案, wput
等等不允许
ncftpput -R -z -u "USER" -p "PASS" webxx.at /dir/ /source/
据传有效,但-z
switch好像是“of label”的用途。我的实验似乎表明时间检查不可靠。
最佳答案
只需将此代码放入文件 FtpSync.sh
中,然后将此文件添加到您的 crontab 中即可。
您可以调整文件中的参数。我尝试创建“说话”参数,以便它们自行解释。
调用此脚本将下载或上传文件(取决于参数CopyServerdataToLocal
)。如果您想同时执行这两项操作,只需使用不同的参数启动脚本两次(或创建两个脚本文件)即可。
FtpSync.sh
#!/bin/bash
# Michael Hutter, 20.11.2021
# This Script can be used to synchronize a remote FTP directory and a local directory
HOST='ftp.mywebspace.de'
USER='web1234567f1'
PASS='YourSecretPwd'
SERVERFOLDER='/FolderOnWebspace'
PCFOLDER='/home/michael/ftpsync/MyLocalFolder'
CopyMoreThanOneFileSimultaneously="--parallel=10"
CopyServerdataToLocal=1 # 0=Upload, 1=Download
IgnoreSubdirectories=1
ContinuePartiallyTransmittedFiles=0
DontOverwriteNewerExistingFiles=0 # If this is used ContinuePartiallyTransmittedFiles will not work!
DeleteAdditionalFilesInDestinationDir=0 # Deletes files in DestDir which are not present in SourceDir
RemoveSourceFilesAfterTransfer=0 # Moves the files instead of copying them
ExcludeParams='--exclude-glob .* --exclude-glob .*/' # Exclude (hidden) files and direcories -> starting with a dot
OnlyShowChangesButDontChangeFiles=0 # DryRun mode
OutputAsMuchInfoAsPossible=1 # Verbose mode
################################################################################
if [ $CopyServerdataToLocal -eq 1 ]; then
if [ $OutputAsMuchInfoAsPossible -eq 1 ]; then
echo "Modus=Download"
fi
DIRECTORIES="$SERVERFOLDER $PCFOLDER"
else
if [ $OutputAsMuchInfoAsPossible -eq 1 ]; then
echo "Modus=Upload"
fi
REVERSE='--reverse'
DIRECTORIES="$PCFOLDER $SERVERFOLDER"
fi
if [ $IgnoreSubdirectories -eq 1 ]; then
IGNORESUBDIRS='--no-recursion'
fi
if [ $ContinuePartiallyTransmittedFiles -eq 1 ]; then
CONTINUEFILES='--continue'
fi
if [ $DontOverwriteNewerExistingFiles -eq 1 ]; then
ONLYNEWER='--only-newer'
fi
if [ $DeleteAdditionalFilesInDestinationDir -eq 1 ]; then
DELETE='--delete'
fi
if [ $RemoveSourceFilesAfterTransfer -eq 1 ]; then
REMOVE='--Remove-source-files'
fi
if [ $OnlyShowChangesButDontChangeFiles -eq 1 ]; then
DRYRUN='--dry-run'
fi
if [ $OutputAsMuchInfoAsPossible -eq 1 ]; then
VERBOSE='--verbose'
fi
lftp -f "
open $HOST
user $USER $PASS
lcd $PCFOLDER
mirror $DRYRUN $REVERSE $IGNORESUBDIRS $DELETE $REMOVE $CONTINUEFILES $ONLYNEWER $CopyMoreThanOneFileSimultaneously --use-cache $ExcludeParams $VERBOSE $DIRECTORIES
bye
"
关于ftp - 在 Linux 中使用 FTP(唯一选项)通过单行命令同步目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69318470/