
tägliche ZFS snapshots auf WETU

<source lang="bash">
 ls -all /admin
-r-x------  1 root root 3,4K Mär 28 19:38 lxc-zfs-auto-snapshot_monitor.sh*
-r-x------  1 root root 3,6K Mär 28 19:38 lxc-zfs-auto-snapshot.sh*

ls -all /lib/systemd/system | grep "snapshot"
-rw-r--r--  1 root root  113 Mär 28 17:28 lxc-zfs-auto-snapshot.service
-rw-r--r--  1 root root  264 Mär 28 17:31 lxc-zfs-auto-snapshot.timer
</source>

== SystemD Service Files ==

<source lang="bash">
/lib/systemd/system# cat lxc-zfs-auto-snapshot.service
[Unit]
Description=lxc-zfs-auto-snapshot

[Service]
Type=simple
ExecStart=/admin/lxc-zfs-auto-snapshot.sh

# EOF
</source>

<source lang="bash">
/lib/systemd/system# cat lxc-zfs-auto-snapshot.timer
[Unit]
Description=lxc-zfs-auto-snapshot

[Timer]
# Time to wait after booting before we run first time
OnBootSec=10min
# Time between running each consecutive time
OnUnitActiveSec=1h
Unit=lxc-zfs-auto-snapshot.service

[Install]
WantedBy=multi-user.target

# EOF
</source>

lxc-zfs-auto-snapshot aktivieren

<source lang="bash">
systemctl enable lxc-zfs-auto-snapshot.timer
systemctl start lxc-zfs-auto-snapshot.timer
systemctl status lxc-zfs-auto-snapshot.timer
</source>

== lxc-zfs-auto-snapshot ==

<source lang="bash">
wetu:/admin# cat lxc-zfs-auto-snapshot.sh

#!/bin/sh
### ### ### C3D2 ### ### ###

###
### TODO: 24.03.2015 - MongoDB support
###

### stage0 // ###
#
## Common ZFS snapshot, exclude Database Container
EXCLUDELXC="db|jabber|rotmine|webbuild|wiki|offen"
#
###
### !!! rpool2 container:   rotmine|webbuild|wiki
###
#
###
### !!! exclude LVM:   offen
###
#
## Database Server 1 - MySQL (db)
MYSQLDBSRV1="db"
MYSQLDBSRVUSER1="debian-sys-maint"
MYSQLDBSRVPASSWD1=""
#
#
## Database Server 1 - PostgreSQL (db)
POSTGRESQLDBSRV1="db"
POSTGRESQLDBSRVUSER1="postgres"
POSTGRESQLDBSRVPASSWD1=""
#
#
## Database Server 2 - PostgreSQL (jabber)
POSTGRESQLDBSRV2="jabber"
POSTGRESQLDBSRVUSER2="postgres"
POSTGRESQLDBSRVPASSWD2=""
#
#
## Database Server 3 - PostgreSQL (wiki)
POSTGRESQLDBSRV3="wiki"
POSTGRESQLDBSRVUSER3="postgres"
POSTGRESQLDBSRVPASSWD3=""
#
### // stage0 ###


### stage1 // ###
#
DATE=$(date +%Y%m%d-%H%M)
#
### // stage1 ###


### stage2 // ###
#
## MySQL       - ZFS snapshot - LXC db
#/ echo "--- --- --- > MySQL      - LXC: $MYSQLDBSRV1 ZFS snapshotting"
lxc-attach -n $MYSQLDBSRV1 -- mysql -h localhost -u $MYSQLDBSRVUSER1 -p$MYSQLDBSRVPASSWD1 -e "set autocommit=0;FLUSH LOGS;FLUSH TABLES WITH READ LOCK;"
## PostgreSQL  - ZFS snapshot - LXC db
#/ echo "--- --- --- > PostgreSQL - LXC: $POSTGRESQLDBSRV1 ZFS snapshotting"
lxc-attach -n $POSTGRESQLDBSRV1 -- su -m $POSTGRESQLDBSRVUSER1 -c 'psql -c "SELECT PG_START_BACKUP('\''zfs-auto-snapshot'\'', true)" postgres;' > /dev/null 2>&1
## AUTOSNAP_DB - Database ZFS snapshot
lxc-attach -n $MYSQLDBSRV1 -- sync
zfs snapshot rpool/lxc/$MYSQLDBSRV1@_AUTOSNAP_DB_$DATE
## set Database online
lxc-attach -n $MYSQLDBSRV1 -- mysql -h localhost -u $MYSQLDBSRVUSER1 -p$MYSQLDBSRVPASSWD1 -e "UNLOCK TABLES;"
lxc-attach -n $POSTGRESQLDBSRV1 -- su -m $POSTGRESQLDBSRVUSER1 -c 'psql -c "SELECT PG_STOP_BACKUP();" postgres;' > /dev/null 2>&1
#
#
## PostgreSQL  - ZFS snapshot - LXC jabber
#/ echo "--- --- --- > PostgreSQL - LXC: $POSTGRESQLDBSRV2 ZFS snapshotting"
lxc-attach -n $POSTGRESQLDBSRV2 -- su -m $POSTGRESQLDBSRVUSER2 -c 'psql -c "SELECT PG_START_BACKUP('\''zfs-auto-snapshot'\'', true)" postgres;' > /dev/null 2>&1
## AUTOSNAP_DB - Database ZFS snapshot
lxc-attach -n $POSTGRESQLDBSRV2 -- sync
zfs snapshot rpool/lxc/$POSTGRESQLDBSRV2@_AUTOSNAP_DB_$DATE
## set Database online
lxc-attach -n $POSTGRESQLDBSRV2 -- su -m $POSTGRESQLDBSRVUSER2 -c 'psql -c "SELECT PG_STOP_BACKUP();" postgres;' > /dev/null 2>&1
#
#
## PostgreSQL  - ZFS snapshot - LXC wiki - RPOOL2
#/ echo "--- --- --- > PostgreSQL - LXC: $POSTGRESQLDBSRV3 ZFS snapshotting"
lxc-attach -n $POSTGRESQLDBSRV3 -- su -m $POSTGRESQLDBSRVUSER3 -c 'psql -c "SELECT PG_START_BACKUP('\''zfs-auto-snapshot'\'', true)" postgres;' > /dev/null 2>&1
## AUTOSNAP_DB - Database ZFS snapshot
lxc-attach -n $POSTGRESQLDBSRV3 -- sync
zfs snapshot rpool2/lxc/$POSTGRESQLDBSRV3@_AUTOSNAP_DB_$DATE
## set Database online
lxc-attach -n $POSTGRESQLDBSRV3 -- su -m $POSTGRESQLDBSRVUSER3 -c 'psql -c "SELECT PG_STOP_BACKUP();" postgres;' > /dev/null 2>&1
#
### // stage2 ###


### stage3 // ###
#
## AUTOSNAP - Common ZFS snapshot
#/ echo "--- --- --- > ALL        - LXC: (exclude $EXCLUDELXC) ZFS snapshotting"
lxc-ls --active | egrep -v "$EXCLUDELXC" | sed 's/^/rpool\/lxc\//' | xargs -L1 -I {} zfs snapshot {}@_AUTOSNAP_$DATE
#
## AUTOSNAP - Common ZFS snapshot (special) <--- etc/crontab daily
#/ zfs snapshot zbackup/rpool2/lxc/webbuild@_AUTOSNAP_$DATE
#
lxc-ls --active | grep "rotmine" | sed 's/^/rpool2\/lxc\//' | xargs -L1 -I {} zfs snapshot {}@_AUTOSNAP_$DATE
#
### // stage3 ###


exit 0
### ### ### C3D2 ### ### ###
# EOF
</source>
