Cygwin 中的 Bash 创建具有未知字符的文件夹
我很困惑。我有一个Linux备份脚本,当我以前使用Linux作为我的主要操作系统时使用过。现在我已经转移到Windows,我想继续在cygwin下使用它。我已经将其移植过来,但发现了一个特殊的问题。
正如您将在下面的代码中看到的,基本上我创建了一个文件夹结构 /device/backups/machinename/。我已经对其进行了概括,因此它通过主机名
确定机器名称。如果文件夹结构不存在,则会创建它。我看到的是,脚本通常可以正常工作,但它偶尔会创建一个重复的计算机名称文件夹,后面带有一个奇怪的方形字符。该字符在 cygwin 中显示为问号。所以,我会看到:/device/backups/machinename 和 /device/backups/machinename?同时。 Cygwin 似乎对这两个文件夹感到困惑,有时备份到第一个,有时备份到第二个。它也不会一致地创建这个文件夹,但如果我让事情每天运行一周,它就会出现。
另请注意,它设计为基于每个文件夹运行,文件夹名称作为参数传入。以FolderName.0.tar.gz、FolderName.1.tar.gz 等格式保存一周的档案。
我现在将尝试通过对计算机名称进行硬编码来解决这个问题,但我真的有兴趣找出问题所在。这是我的脚本的来源:
#!/bin/bash
#Backup Docs
for FOLDER in $@
do
# Location of folder to be backed up
FOLDERLOCATION="/home/sean"
# Mount point of the backup device
DEVICE="/cygdrive/f"
# Hostname of the machine being backed up
HOSTNAME=`hostname`
# NOTE: when I originally posted this question, the above line read:
# HOSTNAME=`txtmsgbreakup`
# which doesn't make any sense. I failed at changing the hard-coded solution back
# to the original command that produces the string "txtmsgbreakup" (my system's name)
BACKUPFOLDER="$FOLDERLOCATION/$FOLDER/"
BACKUPDEST="$DEVICE/backups/$HOSTNAME/$FOLDER/"
# Check to see if device is mounted
if [ -d $DEVICE ]
then
# Create directory if necessary
if [ ! -d $BACKUPDEST ]; then
mkdir -p $BACKUPDEST
fi
# Capture before time for logging
before=$(date +%s)
# First, tar up the old into file named after day of week
DOW=`date +%w`
FILENAME="$DEVICE/backups/$HOSTNAME/$FOLDER.$(( ($DOW+6)%7 )).tar.gz"
if [ -e $FILENAME ]; then
rm $FILENAME
fi
tar -czPf $FILENAME $BACKUPDEST
# Now perform the backup
rsync -a --del --ignore-errors $BACKUPFOLDER $BACKUPDEST
after=$(date +%s)
# Calculate how long the backup took
elapsed_seconds=$(($after-$before))
es=$((elapsed_seconds % 60))
em=$(( (elapsed_seconds / 60) % 60 ))
eh=$((elapsed_seconds / 3600 ))
# Write it all to the system log
echo "$(date) - $BACKUPFOLDER backed up. Elapsed time: $(printf '%02d:%02d:%02d' $eh $em $es)" >> /var/log/backup
else
#External is not mounted if this branch is executed, log this.
echo "$(date) - $BACKUPFOLDER not backed up: Backup device not mounted." >> /var/log/backup
fi
done
I'm baffled. I've got a linux backup script I used when I used to use linux as my main OS. Now that I've moved to windows, I want to keep using it under cygwin. I've ported it over, but am seeing a peculiar issue.
As you'll see in the code below, basically I create a folder structure /device/backups/machinename/. I've generalized it so it determines machine name via hostname
. If the folder structure doesn't exist, it creates it. What I'm seeing is that the script is generally working, but it occasionally likes to create a duplicate machine name folder with an odd square character after it. This character shows as a question mark in cygwin. So, I'd see: /device/backups/machinename and /device/backups/machinename? at the same time. Cygwin seems to get confused between these two folders, sometimes backing up to the first and sometimes backing up to the second. It also doesn't create this folder consistently, but if I let things run every day for a week, it'll show up.
Also note it's designed to run on a per folder basis, folder names passed in as arguments. Keeps a week of archives in the format FolderName.0.tar.gz, FolderName.1.tar.gz, etc.
I'm going to try to work around it by hard coding the machine name for now, but I'm really interested in figuring out what the problem is. Here's my script's source:
#!/bin/bash
#Backup Docs
for FOLDER in $@
do
# Location of folder to be backed up
FOLDERLOCATION="/home/sean"
# Mount point of the backup device
DEVICE="/cygdrive/f"
# Hostname of the machine being backed up
HOSTNAME=`hostname`
# NOTE: when I originally posted this question, the above line read:
# HOSTNAME=`txtmsgbreakup`
# which doesn't make any sense. I failed at changing the hard-coded solution back
# to the original command that produces the string "txtmsgbreakup" (my system's name)
BACKUPFOLDER="$FOLDERLOCATION/$FOLDER/"
BACKUPDEST="$DEVICE/backups/$HOSTNAME/$FOLDER/"
# Check to see if device is mounted
if [ -d $DEVICE ]
then
# Create directory if necessary
if [ ! -d $BACKUPDEST ]; then
mkdir -p $BACKUPDEST
fi
# Capture before time for logging
before=$(date +%s)
# First, tar up the old into file named after day of week
DOW=`date +%w`
FILENAME="$DEVICE/backups/$HOSTNAME/$FOLDER.$(( ($DOW+6)%7 )).tar.gz"
if [ -e $FILENAME ]; then
rm $FILENAME
fi
tar -czPf $FILENAME $BACKUPDEST
# Now perform the backup
rsync -a --del --ignore-errors $BACKUPFOLDER $BACKUPDEST
after=$(date +%s)
# Calculate how long the backup took
elapsed_seconds=$(($after-$before))
es=$((elapsed_seconds % 60))
em=$(( (elapsed_seconds / 60) % 60 ))
eh=$((elapsed_seconds / 3600 ))
# Write it all to the system log
echo "$(date) - $BACKUPFOLDER backed up. Elapsed time: $(printf '%02d:%02d:%02d' $eh $em $es)" >> /var/log/backup
else
#External is not mounted if this branch is executed, log this.
echo "$(date) - $BACKUPFOLDER not backed up: Backup device not mounted." >> /var/log/backup
fi
done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该删除
HOSTNAME=
txtmsgbreakup如果您想放置一个字符串,请尝试:HOSTNAME="txtmsgbreakup" 或者如果您想要动态主机名,则变量 $HOSTNAME 已在您的环境中。
根据我对您问题的理解,您希望使用字符串“txtmsgbreakup”定义 HOSTNAME,但是当您将其放在反引号 ` 内时,您会要求运行不存在的命令。
此外,在 shell 变量中使用更多引号以避免出现问题(有一天)
You should remove
HOSTNAME=
txtmsgbreakupIf you want to put a string, try that : HOSTNAME="txtmsgbreakup" or if you'd want the dynamic hostname, the variable $HOSTNAME is already in your environment.
In my understanding of your problem, you'd want to define HOSTNAME with the string "txtmsgbreakup" but while you put it inside backticks `, you ask to run a command that is not exists.
Morevover, use more quotes in your shell variables to avoid things breaking (someday)