如何定期备份更改的文件夹?

如何定期备份更改的文件夹?

我想在 ubuntu 中实现以下功能,但我不知道如何做。

我需要一个守护进程来定期备份目录,比如dir通过执行以下操作将其备份到另一个位置:

  1. 检查备份位置中的最新目录。
    1. 如果未找到,则备份目录dir并使用时间戳将其重命名为如下名称:dir_timestamp
    2. 如果找到,则检查dir和之间的内容是否有任何变化dir_timestamp
      • 如果有变化,则使用新的时间戳备份目录。
      • 如果没有,则不做任何事情
  2. 等待指定的时间
  3. 返回 1。

我大概知道这个算法,但我不知道该怎么做,因为我对 shell 脚本一无所知。如果能做到这一点,对我的研究将非常有用。有人能帮帮我吗?

答案1

注意:我没有测试过这一点,因为我的备份方式不同。然而我思考它会起作用。

编辑:

#! /bin/bash

newestfile=$(ls /backup/location -td | head -1)

budate=$(newestfile| cut -c10-19)
cdate=$(date --iso)

# Check if there is a backup done today
if [ $cdate = $budate ]; then

    # inform user that it is checking for changes.
    echo "Backup done today, checking for changes."
    notify-send -i /home/<USER>/Pictures/Logos/safe.png "Backup Status" "Checking for changes"

    # get the exit code of the diff command (1 = changes, 0 = no changes)
    diffexit=$(diff /home/<USER>/dir/to/backup /backup/location/backup-$(date +%Y-%m-%d-%H:%M)-<chosendirname> && $?)

    # if there are no changes tell the user
    if [$diffexit = 0 ]; then
        echo "Backup complete, no changes"
        notify-send -i /home/<USER>/Pictures/Logos/safe.png "Backup Status" "Backup complete, no changes"
    else
    # if there are changes, tell the user
        echo "Propagating changes"
        notify-send -i /home/<USER>/Pictures/Logos/safe.png "Backup Status" "Propagating changes"
        # copy it acros
        cp -ar /home/<USER>/dir/to/backup /backup/location/backup-$(date +%Y-%m-%d-%H:%M)-<chosendirname>
        # Tell the user it is finished
        echo "Backup complete, finished propagating changes"
        notify-send -i /home/<USER>/Pictures/Logos/safe.png "Backup Status" "Backup complete, finished all changes"
    fi

# if there wasn"t a folder with the current date.
else
    # Tell the user it is starting
    echo "Starting backup"
    notify-send --expire-time=60000 -i /home/<USER>/Pictures/Logos/safe.png "Backup Status" "Starting backup for today."
    # copy it across.
    cp -ar /home/<USER>/dir/to/backup /backup/location/backup-$(date +%Y-%m-%d-%H:%M)-<chosendirname>
    # tell the user it has finished
    echo "Finished backup for today."
    notify-send --expire-time=60000 -i /home/<USER>/Pictures/Logos/safe.png "Backup Status" "Finished backup for today."
fi

# sleep 3 mins
sleep 500

# run itself
/home/<USER>/./script.sh

旧代码,不太好:

    #! /bin/bash

    newestfile=$(ls /backup/location -td | head -1)

    budate=`echo $newestfile| cut -c10-19`
    cdate=$(date --iso)

    if [ $cdate = $budate ]; then
        echo "Backup Done today, checking for changes."
        notify-send -i /home/<USER>/Pictures/Logos/safe.png "Backup Status" "Checking for changes"

    dirls=$(ls /home/<USER>/dir/to/backup)
    dirbuls=$(ls /backup/location/$(newestfile))

    if [$dirls = $dirbuls ]; then
        notify-send -i /home/<USER>/Pictures/Logos/safe.png "Backup Status" "Backup complete, no changes"
    else
        notify-send -i /home/<USER>/Pictures/Logos/safe.png "Backup Status" "Propagating changes"
        cp -ar /home/<USER>/dir/to/backup /backup/location/backup-$(date +%Y-%m-%d-%H:%M)-<chosendirname>
        notify-send -i /home/<USER>/Pictures/Logos/safe.png "Backup Status" "Backup complete, finished all changes"
    fi

else
    echo "Starting backup"
    notify-send --expire-time=60000 -i /home/<USER>/Pictures/Logos/safe.png 'Backup Status' 'Starting backup for today.'

    cp -ar /home/<USER>/dir/to/backup /backup/location/backup-$(date +%Y-%m-%d-%H:%M)-<chosendirname>


    notify-send --expire-time=60000 -i /home/<USER>/Pictures/Logos/safe.png 'Backup Status' 'Finished backup for today.'
fi

相关内容