nano /etc/rsnapshot.conf

nano /etc/rsnapshot.conf

我想每周运行以下不完整的脚本作为 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 执行是否会找到已复制和压缩的文件?

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

答案1

您可能希望使用可以自动完成整个过程的工具,例如快照,这似乎可以实现您想要做的事情。

答案2

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

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

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

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

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

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

答案3

您可以使用 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}/ 目录下创建,如上一步所示。

    nano /etc/rsnapshot.conf

    备份 /home/localhost/

  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/

答案4

因此,我将尝试回答第二个(和第三个)问题: --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 创建其他文件,它们只会占用额外的空间,并且您会失去硬链接的优势。

相关内容