6

我正在尝试制作一个简单的备份脚本,但在创建名称为当前日期的文件夹时遇到问题

我的脚本就是这样,基本上问题出在最后一行

drivers=$(ls /media/)

declare -i c=0
for word in $drivers
do
    echo "($c)$word"
    c=c+1
done

read -n 1 drive
echo

c=0
for word in $drivers
do
    if [ $c -eq $drive ]
    then
        backuppath="/media/$word/backup"
    fi
    c=c+1
done


echo "doing back up to $backuppath"

cp -r /home/stefanos/Programming $backuppath/$(date +%Y-%m-%d-%T)

输出:

(0)0362-BA96
(1)Data
(2)Windows
0
doing back up to /media/0362-BA96/backup
cp: cannot create directory `/media/0362-BA96/backup/2012-12-05-21:58:37': Invalid argument

在 /media/0362-BA96/ 之前存在的路径经过三次检查

已解决: janisz所说的最终脚本看起来像吗

drivers=$(ls /media/)

declare -i c=0
for word in $drivers
do
    echo "($c)$word"
    c=c+1
done

read -n 1 drive
echo

c=0
for word in $drivers
do
    if [ $c -eq $drive ]
    then
        backuppath="/media/$word/backup"
    fi
    c=c+1
done
echo "doing back up to $backuppath"

backup(){
  time_stamp=$(date +%Y_%m_%d_%H_%M_%S)
  mkdir -p "${backuppath}/${time_stamp}$1"
  cp -r "${1}" "${backuppath}/${time_stamp}$1"

  echo "backup complete in $1"
}

#####################The paths to backup####################

backup "/home/stefanos/Programming"
backup "/home/stefanos/Android/Projects"
backup "/home/stefanos/Dropbox"
4

2 回答 2

18

尝试将其更改为:

time_stamp=$(date +%Y-%m-%d-%T)
mkdir -p "${backuppath}/${time_stamp}"
cp -r /home/stefanos/Programming "${backuppath}/${time_stamp}"
于 2012-12-05T20:09:06.243 回答
14

:在 FAT 上无效(用于指定磁盘)。一些 M$ 无效字符适用于 GNU/Linux 系统,但避免它们更安全(只需替换为.)。使用以下日期格式

date +%Y_%m_%d_%H_%M_%S

它应该适用于大多数文件系统,但对于 MS DOS FAT 来说可能太长了。您可以在此处找到更多信息。

于 2012-12-05T20:55:08.860 回答