AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-1105165

hyhno01's questions

Martin Hope
hyhno01
Asked: 2024-07-08 21:06:24 +0800 CST

Ubuntu:使用 anacron 和 rsync 每月备份到外部驱动器

  • 5

我的目标是每月将我的 ubuntu/home/目录备份到外部驱动器,将其连接到计算机后,我就可以在 中看到它/media/ironman/Elements。为此,我使用rsync。

外部驱动器并不总是挂载的,所以我制作了一个交互式脚本,当驱动器没有挂载时,它会提示我挂载驱动器。我想安排这个脚本,anacron因为我的笔记本电脑经常关机。我认为我的设置不起作用是因为交互式部分:当我从命令行运行脚本时,我自然可以在同一命令行中看到它的输出。但是,anacron 不是从命令行运行的。

这是脚本/home/ironman/scripts/backup_anacron.sh

#!/bin/bash

# Variables
SOURCE_DIR="/home/ironman"
TARGET_DIR="/media/ironman/Elements/backup_$(date +%Y%m%d)"
EXCLUDES="--exclude=\"lost+found\" --exclude=\".cache\""
RSYNC_CMD="rsync -a --info=progress2 $EXCLUDES $SOURCE_DIR/ $TARGET_DIR"


# Function to check if the drive is mounted
check_mount() {
    if grep -qs "/media/ironman/Elements " /proc/mounts; then
        echo "Backup drive is mounted."
        return 0
    else
        return 1
    fi
}

# Check whether backup drive has enough free space
check_space() {
    # Get available space on target drive in kilobytes
    available_space=$(df --output=avail "/media/ironman/Elements" | tail -1)
    # Get required space for the source directory in kilobytes
    required_space=$(du -sk "$SOURCE_DIR" | cut -f1)
    
    if [ "$available_space" -ge "$required_space" ]; then
        echo "Enough space on backup drive. Required: $required_space KB, Available: $available_space KB."
        return 0
    else
        echo "Not enough space on backup drive. Required: $required_space KB, Available: $available_space KB. Manual backup necessary"
        echo $RSYNC_CMD
        exit 1
    fi
}

# Initial check
check_mount
if [ $? -eq 1 ]; then

    # If the drive is not mounted, prompt the user
    echo "Please mount drive within one minute."

    # Wait for up to 60 seconds for the drive to be mounted
    for i in {1..60}; do
    
        # check every second if drive is mounted
        sleep 1
        check_mount
        if [ $? -eq 0 ]; then
        
            # if enough space available, execute rsync, else print error message
        check_space
        if [ $? -eq 0 ]; then
                eval $RSYNC_CMD
                exit 0 
            fi
        fi
    done
fi


# If the drive is still not mounted after 60 seconds, print an error message
if [ $? -eq 1 ]; then
    echo "Backup drive is not mounted. Could not backup /home/ironman/. Command for manual backup:"
    echo $RSYNC_CMD
    exit 1
fi

# if enough space available, execute rsync, else exit script
check_space
if [ $? -eq 0 ]; then
    eval $RSYNC_CMD
    exit 0
fi

我在末尾添加了以下行/etc/anacrontab:

@monthly 15 backup.monthly nice /home/ironman/scripts/backup_anacron.sh

当我强制运行每月任务时/usr/sbin/anacron -n -f cron.monthly,什么也没发生。我做错了什么?

backup
  • 3 个回答
  • 29 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve