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
    • 最新
    • 标签
主页 / ubuntu / 问题 / 20508
In Process
Evan
Evan
Asked: 2011-01-08 11:09:12 +0800 CST2011-01-08 11:09:12 +0800 CST 2011-01-08 11:09:12 +0800 CST

用于增量文件系统备份的脚本

  • 772

我想每周运行以下不完整的脚本作为 cron 作业,以将我的主目录备份到安装为 /mnt/backups 的外部驱动器

#!/bin/bash
#

TIMEDATE=$(date +%b-%d-%Y-%k:%M)

LASTBACKUP=pathToDirWithLastBackup

rsync -avr --numeric-ids --link-dest=$LASTBACKUP /home/myfiles /mnt/backups/myfiles$TIMEDATE

我的第一个问题是如何正确地将 LASTBACKUP 设置为 /backs 最近创建的目录?

其次,我的印象是,使用 --link-desk 意味着以前备份中的文件如果仍然存在,则不会在以后的备份中复制,而是会象征性地链接回最初复制的文件?但是,我不想永远保留旧文件。在某个日期之前删除所有备份而不丢失可能认为在这些备份中由当前备份链接的文件的最佳方法是什么?基本上,我希望将某个日期之前的所有文件合并到某个日期,如果这比我最初提出问题的方式更有意义:)。--link-dest 是否可以创建硬链接,如果可以,仅删除以前的目录实际上不会删除链接文件?

最后,我想在我的脚本中添加一行来压缩每个新创建的备份文件夹 (/mnt/backups/myfiles$TIMEDATE)。基于阅读这个问题,我想知道我是否可以使用这条线

gzip --rsyncable /backups/myfiles$TIMEDATE

在我运行 rsync 以便顺序 rsync --link-dest 执行会找到已经复制和压缩的文件之后?

我知道这很多,非常感谢您的帮助!

bash
  • 4 4 个回答
  • 16900 Views

4 个回答

  • Voted
  1. Ryan C. Thompson
    2011-01-08T12:12:04+08:002011-01-08T12:12:04+08:00

    您可能希望使用为您自动化整个过程的工具,例如rsnapshot,它似乎实现了您正在尝试做的事情。

    • 7
  2. Martin Owens -doctormo-
    2011-01-08T11:54:06+08:002011-01-08T11:54:06+08:00

    请一次一个问题,我只回答第一个问题,您可以将其他问题发布到另一个问题:

    为了知道最后一个备份目录是什么,您只需添加:

    echo /mnt/backups/myfiles$TIMEDATE > /mnt/backups/last.dir
    

    到脚本的最后,然后将 LASTBACKUP= 更改为:

    LASTBACKUP=`cat /mnt/backups/last.dir`
    

    这样,您始终知道您将使用最后一个完整的备份目录,而不是在该目录中创建的任何其他文件/文件夹,或失败的备份文件夹(这会产生大量重复)

    • 4
  3. cprofitt
    2011-11-17T06:28:02+08:002011-11-17T06:28:02+08:00

    你可以使用 rsync。

    Listing one: make_snapshot.sh
    
    #!/bin/bash
    # ----------------------------------------------------------------------
    # mikes handy rotating-filesystem-snapshot utility
    # ----------------------------------------------------------------------
    # this needs to be a lot more general, but the basic idea is it makes
    # rotating backup-snapshots of /home whenever called
    # ----------------------------------------------------------------------
    
    unset PATH  # suggestion from H. Milz: avoid accidental use of $PATH
    
    # ------------- system commands used by this script --------------------
    ID=/usr/bin/id;
    ECHO=/bin/echo;
    
    MOUNT=/bin/mount;
    RM=/bin/rm;
    MV=/bin/mv;
    CP=/bin/cp;
    TOUCH=/bin/touch;
    
    RSYNC=/usr/bin/rsync;
    
    
    # ------------- file locations -----------------------------------------
    
    MOUNT_DEVICE=/dev/hdb1;
    SNAPSHOT_RW=/root/snapshot;
    EXCLUDES=/usr/local/etc/backup_exclude;
    
    
    # ------------- the script itself --------------------------------------
    
    # make sure we're running as root
    if (( `$ID -u` != 0 )); then { $ECHO "Sorry, must be root.  Exiting..."; exit; } fi
    
    # attempt to remount the RW mount point as RW; else abort
    $MOUNT -o remount,rw $MOUNT_DEVICE $SNAPSHOT_RW ;
    if (( $? )); then
    {
        $ECHO "snapshot: could not remount $SNAPSHOT_RW readwrite";
        exit;
    }
    fi;
    
    
    # rotating snapshots of /home (fixme: this should be more general)
    
    # step 1: delete the oldest snapshot, if it exists:
    if [ -d $SNAPSHOT_RW/home/hourly.3 ] ; then         \
    $RM -rf $SNAPSHOT_RW/home/hourly.3 ;                \
    fi ;
    
    # step 2: shift the middle snapshots(s) back by one, if they exist
    if [ -d $SNAPSHOT_RW/home/hourly.2 ] ; then         \
    $MV $SNAPSHOT_RW/home/hourly.2 $SNAPSHOT_RW/home/hourly.3 ; \
    fi;
    if [ -d $SNAPSHOT_RW/home/hourly.1 ] ; then         \
    $MV $SNAPSHOT_RW/home/hourly.1 $SNAPSHOT_RW/home/hourly.2 ; \
    fi;
    
    # step 3: make a hard-link-only (except for dirs) copy of the latest snapshot,
    # if that exists
    if [ -d $SNAPSHOT_RW/home/hourly.0 ] ; then         \
    $CP -al $SNAPSHOT_RW/home/hourly.0 $SNAPSHOT_RW/home/hourly.1 ; \
    fi;
    
    # step 4: rsync from the system into the latest snapshot (notice that
    # rsync behaves like cp --remove-destination by default, so the destination
    # is unlinked first.  If it were not so, this would copy over the other
    # snapshot(s) too!
    $RSYNC                              \
        -va --delete --delete-excluded              \
        --exclude-from="$EXCLUDES"              \
        /home/ $SNAPSHOT_RW/home/hourly.0 ;
    
    # step 5: update the mtime of hourly.0 to reflect the snapshot time
    $TOUCH $SNAPSHOT_RW/home/hourly.0 ;
    
    # and thats it for home.
    
    # now remount the RW snapshot mountpoint as readonly
    
    $MOUNT -o remount,ro $MOUNT_DEVICE $SNAPSHOT_RW ;
    if (( $? )); then
    {
        $ECHO "snapshot: could not remount $SNAPSHOT_RW readonly";
        exit;
    } fi;
    

    第二个:

    Listing two: daily_snapshot_rotate.sh
    
    #!/bin/bash
    # ----------------------------------------------------------------------
    # mikes handy rotating-filesystem-snapshot utility: daily snapshots
    # ----------------------------------------------------------------------
    # intended to be run daily as a cron job when hourly.3 contains the
    # midnight (or whenever you want) snapshot; say, 13:00 for 4-hour snapshots.
    # ----------------------------------------------------------------------
    
    unset PATH
    
    # ------------- system commands used by this script --------------------
    ID=/usr/bin/id;
    ECHO=/bin/echo;
    
    MOUNT=/bin/mount;
    RM=/bin/rm;
    MV=/bin/mv;
    CP=/bin/cp;
    
    # ------------- file locations -----------------------------------------
    
    MOUNT_DEVICE=/dev/hdb1;
    SNAPSHOT_RW=/root/snapshot;
    
    # ------------- the script itself --------------------------------------
    
    # make sure we're running as root
    if (( `$ID -u` != 0 )); then { $ECHO "Sorry, must be root.  Exiting..."; exit; } fi
    
    # attempt to remount the RW mount point as RW; else abort
    $MOUNT -o remount,rw $MOUNT_DEVICE $SNAPSHOT_RW ;
    if (( $? )); then
    {
        $ECHO "snapshot: could not remount $SNAPSHOT_RW readwrite";
        exit;
    }
    fi;
    
    
    # step 1: delete the oldest snapshot, if it exists:
    if [ -d $SNAPSHOT_RW/home/daily.2 ] ; then          \
    $RM -rf $SNAPSHOT_RW/home/daily.2 ;             \
    fi ;
    
    # step 2: shift the middle snapshots(s) back by one, if they exist
    if [ -d $SNAPSHOT_RW/home/daily.1 ] ; then          \
    $MV $SNAPSHOT_RW/home/daily.1 $SNAPSHOT_RW/home/daily.2 ;   \
    fi;
    if [ -d $SNAPSHOT_RW/home/daily.0 ] ; then          \
    $MV $SNAPSHOT_RW/home/daily.0 $SNAPSHOT_RW/home/daily.1;    \
    fi;
    
    # step 3: make a hard-link-only (except for dirs) copy of
    # hourly.3, assuming that exists, into daily.0
    if [ -d $SNAPSHOT_RW/home/hourly.3 ] ; then         \
    $CP -al $SNAPSHOT_RW/home/hourly.3 $SNAPSHOT_RW/home/daily.0 ;  \
    fi;
    
    # note: do *not* update the mtime of daily.0; it will reflect
    # when hourly.3 was made, which should be correct.
    
    # now remount the RW snapshot mountpoint as readonly
    
    $MOUNT -o remount,ro $MOUNT_DEVICE $SNAPSHOT_RW ;
    if (( $? )); then
    {
        $ECHO "snapshot: could not remount $SNAPSHOT_RW readonly";
        exit;
    } fi;
    

    根据您的需要创建脚本后,将其添加到 cron 作业中。

    crontab -e

    添加以下内容:

    0 */4 * * * /usr/local/bin/make_snapshot.sh

    0 13 * * * /usr/local/bin/daily_snapshot_rotate.sh

    它们导致 make_snapshot.sh 每四个小时运行一次,daily_snapshot_rotate.sh 每天在 13:00(即下午 1:00)运行。

    来源: http: //www.mikerubel.org/computers/rsync_snapshots/

    * * * * * command to be executed
    - - - - -
    | | | | |
    | | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
    | | | ------- Month (1 - 12)
    | | --------- Day of month (1 - 31)
    | ----------- Hour (0 - 23)
    ------------- Minute (0 - 59)
    

    如果您希望它每小时运行一次,您将每小时添加一个 cron 作业。

    另一种可能的选择是使用 rsnapshot

    1. 安装 rsnapshot(软件中心提供)

    2. 配置 rsnapshot 并指定备份源目录

    打开 /etc/rsnapshot.conf 并取消注释以下行。

    # nano /etc/rsnapshot.conf
    
    cmd_cp          /bin/cp
    cmd_ssh /usr/bin/ssh
    cmd_du          /usr/bin/du
    cmd_rsnapshot_diff      /usr/local/bin/rsnapshot-diff
    logfile /var/log/rsnapshot
    
    1. 在 /etc/rsnapshot.conf 中定义您的目标备份目录,如下所示。在这个例子中,

      /home - 应备份的源目录 localhost/ - 将存储备份的目标目录。请注意,该目录将在 /.snapshots/{internal.n}/ 目录下创建,如上一步所示。

      纳米 /etc/rsnapshot.conf

      备份 /home/ 本地主机 /

    2. 测试 rsnapshot 配置

    执行配置测试以确保 rsnapshot 设置正确并准备好执行 linux rsync 备份。

    # rsnapshot configtest
    Syntax OK
    
    1. 验证 rsnapshot 每小时备份配置

    您可以按不同的时间间隔备份 linux 目录或文件。默认情况下,配置每小时和每日备份。

    验证每小时备份配置。

    # rsnapshot -t hourly
    echo 6490 > /var/run/rsnapshot.pid
    mkdir -m 0700 -p /.snapshots/
    mkdir -m 0755 -p /.snapshots/hourly.0/
    /usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded /home \
    /.snapshots/hourly.0/localhost/
    mkdir -m 0755 -p /.snapshots/hourly.0/
    /usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded /etc \
    /.snapshots/hourly.0/localhost/
    mkdir -m 0755 -p /.snapshots/hourly.0/
    /usr/bin/rsync -a --delete --numeric-ids --relative --delete-excluded \
    /usr/local /.snapshots/hourly.0/localhost/
    touch /.snapshots/hourly.0/
    
    1. 验证 rsnapshot 每日备份配置

    验证每日 rsnapshot cwrsync 备份过程配置是否正确。

    # rsnapshot -t daily
    echo 6493 > /var/run/rsnapshot.pid
    mkdir -m 0700 -p /.snapshots/
    /.snapshots/hourly.5 not present (yet), nothing to copy
    
    1. 为 rsnapshot 添加 Crontab 条目

    一旦您在 rsnapshot cwrsync 实用程序中验证了 rsync 每小时和每日备份配置已正确设置,就可以在 crontab 中设置此小狗,如下所示。

    # crontab -e
    0 */4 * * * /usr/local/bin/rsnapshot hourly
    30 23 * * * /usr/local/bin/rsnapshot daily
    

    来源:http ://www.thegeekstuff.com/2009/08/tutorial-backup-linux-using-rsnapshot-rsync-utility/

    • 2
  4. Frank
    2011-01-08T13:27:26+08:002011-01-08T13:27:26+08:00

    所以我将尝试回答第二个(和第三个)问题: --link-dest创建指向目标文件系统上现有文件的附加硬链接。访问权限后的数字是“链接计数”,它告诉您有多少链接(想想文件名)指向文件。例如:

    $ touch x
    $ ls -l
    -rw-rw-r-- 1 frank frank 0 2011-01-07 22:18 x
    $ ln x y
    $ ls -l
    -rw-rw-r-- 2 frank frank 0 2011-01-07 22:18 x
    

    您可以简单地进行rm -rf旧备份。rm意味着unlink。它减少了文件的链接计数。文件系统只会覆盖链接计数为零的文件。如果您使用 gzip 创建其他文件,它们只会使用额外的空间,并且您会失去硬链接的优势。

    • 1

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

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

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    我需要什么命令来解压缩/提取 .tar.gz 文件?

    • 8 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Martin Hope
    EmmyS 我需要什么命令来解压缩/提取 .tar.gz 文件? 2011-02-09 14:50:41 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve