Linux Shell 脚本 - 挂载和备份文件

Linux Shell Script - Mounting and Backing Up Files

我正在编写我的第一个 shell 脚本。我想检查是否安装了文件系统。如果是这样,则将文档从我的主驱动器备份到安装的备份。如果没有,挂载文件系统,然后备份文件。

#!/bin/bash
# Check if linux_backup filesystem is mounted
# Mount filesystem if not mounted already, and then backup documents

docs= "/home/user1/Documents"
mount= "/run/media/user1"
docsbackup= "/run/media/user1/linux_backup/home/user1/Documents"
filesystem= "/dev/sdc3"

sync_files() {
    SOURCE=
    TARGET=
    sudo rsync --progress -r --update -aAXv $SOURCE $TARGET
}

if mount | awk '{if ( == "/run/media/user1") { exit 0}} ENDFILE{exit -1}';
then
    echo "linux_backup is mounted"
    sync_files $docs $docksbackup
else
    echo "linux_backup is not mounted. Will mount now"
    mount $filesystem $mount
    sync_files $docs $docksbackup
fi

我执行了脚本,但我得到的只是很多文本。它没有像我想要的那样表现。我是否没有为其中一个函数使用正确的选项或参数?

我给你推荐这样一个脚本:

docs="/home/user1/Documents"
mount="/run/media/user1"
backup="/run/media/user1/linux_backup/home/user1/Documents"
device="/dev/sdc3"

sync_files() {
    SOURCE=
    TARGET=
    sudo rsync --progress -r --update -aAXv $SOURCE $TARGET
}

# Check if FS is mounted
if grep -qs "/run/media/user1" /proc/mounts; then
    # Device is mounted
    sync_files $docs $docksbackup
else
    # Device isn't mounted, mounting...
    mount $mount $device
    # Back upping
    sync_files $docs $docksbackup
fi