6. zfs-auto-snapshot

 

 


zfs-auto-snapshot

 

 * 純 sh script

Tips: zfs-auto-snapshot will snapshot all datasets except user-property com.sun:auto-snapshot is set to false

Installation

cd /usr/src

LINK=https://github.com/zfsonlinux/zfs-auto-snapshot/archive/upstream/1.2.4.tar.gz

wget $LINK -O zfs-auto-snapshot-1.2.4.tar.gz

md5sum zfs-auto-snapshot-1.2.4.tar.gz

6c71731b441e1e0f8da385df583460d5  zfs-auto-snapshot-1.2.4.tar.gz

tar -zxf zfs-auto-snapshot-1.2.4.tar.gz

cd zfs-auto-snapshot-upstream-1.2.4

cp -a etc/zfs-auto-snapshot.cron.daily /etc/cron.daily/zfs-auto-snapshot

cp -a src/zfs-auto-snapshot.sh /usr/local/sbin/zfs-auto-snapshot

chmod 750 /etc/cron.daily/zfs-auto-snapshot /usr/local/sbin/zfs-auto-snapshot

vim /etc/cron.daily/zfs-auto-snapshot

#!/bin/sh

# Only call zfs-auto-snapshot if it's available
which zfs-auto-snapshot > /dev/null || exit 0

exec zfs-auto-snapshot --quiet --syslog --label=daily --keep=7 //

/etc/anacrontab

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin

Remark

"make install" 會一次過安許多 cron jobs

/etc/cron.d/zfs-auto-snapshot          # */15, -k 4
/etc/cron.hourly/zfs-auto-snapshot     # -k 24
/etc/cron.daily/zfs-auto-snapshot      # -k 31
/etc/cron.weekly/zfs-auto-snapshot     # -k 8
/etc/cron.monthly/zfs-auto-snapshot    # -k 12
/usr/local/sbin/zfs-auto-snapshot

Usage

# '//' for all ZFS datasets

zfs-auto-snapshot [options] [-l label] <'//' | name [name...]>

Opts

  • -n, --dry-run        # Print actions without actually doing anything.
  • -k, --keep=NUM   # Keep NUM recent snapshots
  • -g, --syslog          # Write messages into the system log. (/var/log/messages)
  • -r, --recursive      # Snapshot named filesystem and all descendants.      
  • -l, --label=LAB     # LAB is usually 'hourly', 'daily', or 'monthly'.  
  • -p, --prefix=PRE   # PRE is 'zfs-auto-snap' by default.

--pre-snapshot=COMMAND

Command to run before each dataset is snapshotted.

If it returns non-zero, snapshotting this dataset is aborted.

It is passed the dataset and snapshot name.  

--post-snapshot=COMMAND

--destroy-only

Do not create new snapshots, but do destroy older snapshots.

Has no effect unless used with -k.

ie.

建立一次 backup

/etc/cron.daily/zfs-auto-snapshot

Check

zfs list -t snapshot

NAME                                                USED  AVAIL     REFER  MOUNTPOINT
lxc@zfs-auto-snap_daily-2022-01-20-0436               0B      -       24K  -
lxc/ispconfig@zfs-auto-snap_daily-2022-01-20-0436  1.05M      -     2.50G  -

"0436" 是 GMT Time 來

Log

grep zfs-auto-snap /var/log/messages

... zfs-auto-snap[2409]: @zfs-auto-snap_daily-2022-01-18-0758, 1 created, 0 destroyed, 0 warnings.
... zfs-auto-snap[2202]: @zfs-auto-snap_daily-2022-01-18-0759, 1 created, 0 destroyed, 0 warnings.
...
... zfs-auto-snap[2562]: @zfs-auto-snap_daily-2022-01-18-0807, 1 created, 1 destroyed, 0 warnings.

Daily Report

# Mail service
dnf install postfix mailx -y
systemctl enable postfix
systemctl start postfix

# Settings
mkdir /root/scripts
MyName=report_zfs-auto-snap
MyScript=/root/scripts/$MyName
[email protected]

# Script
cat > $MyScript << EOF
grep zfs-auto-snap /var/log/messages | mail -s ${MyName}_$(hostname) ${MyAdmin}
EOF
chmod 755 $MyScript

# Test report (send e-mail)
$MyScript

# cron job
echo -e "# daily report \n 1 10 * * * $MyScript" | crontab -

 

 

Creative Commons license icon Creative Commons license icon