我正在尝试创建一个 Bash 脚本,每周备份一次主文件夹中的重要文件。我还没有开始 crontab 部分,但我一直在尝试让这个进度条工作。我想知道要等多久才能完成。我不想只输入 echo 。"Process ongoing, please wait"
请帮忙
Bash 代码:
TODAY=$(date +%d-%m-%Y)
echo $TODAY
file="Exist_or_Not.txt"
type pv > $file
if grep -q "pv" $file; then
echo "exists"
else
echo "Installing pv for progress bar"
sudo apt install pv
fi
#SIZE='du -sk /home/user/{Desktop,Documents,Downloads} | cut -f 1'
sudo tar -pczf /media/backups/backup${TODAY}.tar.gz /home/user/Desktop /home/user/Documents /home/user/Downloads | pv -s 40978228k > /dev/null
我通过计算 Python 中 $SIZE 的值得到了 40978228k。将这些数字相加,num1 + num 2 + num3,因为${SIZE}k
没有用。它一直说pv: -s: integer argument expected
答案1
编辑:完整代码,有效!
#!/bin/bash
TODAY=$(date +%d-%m-%Y)
f="tekst.txt"
type pv > $f
if grep -q "pv" $f; then
echo "PV exists"
else
echo "Installing PV..."
sudo apt install pv
fi
f2="home_backup${TODAY}.tar.gz"
echo "Your username is: $(whoami)"
echo "1) Backup your home directory"
echo "2) Extract your backup"
read -p "Choose the option: " choice
if [ -z $choice ]; then
echo "Choose the option"
continue
elif [ $choice -eq 1 ]; then
echo "Backup directory: /media/backup/"
sleep 1
echo "Backup Process Activated"
sudo tar -cf - /home/$(whoami) -P | pv -s $(du -sb /home/$(whoami) | awk '{print $1}') | gzip > /media/extracted/$f2
elif [ $choice -eq 2 ]; then
echo "Extracting your backupped home directory from /media/backup/"
echo "Extracted directories go to /media/extracted/"
sleep 1
echo "Extraction Process Activated"
pv /media/extracted/$f2 | sudo tar -xzf - -C /media/extracted/
fi