backup.sh
per Victor Carceler
—
darrera modificació
2020-07-07T13:37:36+01:00
Shell script para el backup de un servidor de MVs.
backup.sh — 4.0 KB
Continguts del fitxer
#!/bin/bash # # Backup de las MVs # # $BASE_SNAPSHOT must exists on destination. # # $BASE_SNAPSHOT may be transferred manually: # # zfs send -R FAST/libvirt@inicial | zfs receive -v -F -o mountpoint=/opt/backup-libvirt SLOW/backup-libvirt # zfs send -R pool/images@2017-06-03_12:50:49 | ssh root@192.168.0.9 "zfs receive -v -F backup/images" # # Ojo: Al enviar un pool que está montado (/var/lib/libvirt) el pool recibido se intenta montar # automáticamente en el mismo sitio. SCRIPT=$(basename $0) DOMAINS=$(virsh list --name --state-running) BASE_SNAPSHOT_FILE="/root/bin/base_snapshot" BASE_SNAPSHOT=$(cat $BASE_SNAPSHOT_FILE) SRC_DATASET="FAST/libvirt" DST_DATASET="SLOW/backup-libvirt" DATE_TIME=$(date +%F_%T) NEW_SNAPSHOT="$SRC_DATASET@$DATE_TIME" MAX_SNAPSHOTS=20 NEW_SNAPSHOT_DIR="/var/lib/libvirt/.zfs/snapshot/$DATE_TIME" function log () { logger -t "$SCRIPT" "$1" } log "Start $SCRIPT" log "DOMAINS=$DOMAINS" log "BASE_SNAPSHOT=$BASE_SNAPSHOT" log "SRC_DATASET=$SRC_DATASET" log "DST_DATASET=$DST_DATASET" log "DATE_TIME=$DATE_TIME" log "NEW_SNAPSHOT=$NEW_SNAPSHOT" log "MAX_SNAPSHOTS=$MAX_SNAPSHOTS" log "NEW_SNAPSHOT_DIR=$NEW_SNAPSHOT_DIR" # Copy .xml domain files # log "Copy .xml domain files to /var/lib/libvirt/backup-domains" cp -a /etc/libvirt/qemu /var/lib/libvirt/backup-domains/ && log "Ok" # Save VMs # log "Saving VMs:" for x in $DOMAINS do log " Saving: $x" virsh managedsave "$x" done log "All VMs saved." # New snapshot log "Creating snapshot $NEW_SNAPSHOT" /sbin/zfs snapshot $NEW_SNAPSHOT && log "Created new snapshot: $NEW_SNAPSHOT" # Start VMs # log "Restoring VMs:" for x in $DOMAINS do log " Restoring: $x" virsh start "$x" done log "All VMs restored." # ZFS send # log "Start zfs send to Gondolin" log "BASE_SNAPSHOT=$BASE_SNAPSHOT NEW_SNAPSHOT=$NEW_SNAPSHOT" /sbin/zfs send -R -I $BASE_SNAPSHOT $NEW_SNAPSHOT | /sbin/zfs receive -v -F -o mountpoint=/opt/backup-libvirt $DST_DATASET && echo $NEW_SNAPSHOT >$BASE_SNAPSHOT_FILE && log "zfs send completed" # Snapshot destroy c=0 for snapshot in `/sbin/zfs list -H -o name -t snapshot -S creation | grep $SRC_DATASET` do c=`expr $c + 1` [ $c -gt $MAX_SNAPSHOTS ] && /sbin/zfs destroy $snapshot && log "Deleted snapshot: $snapshot" done c=0 for snapshot in `/sbin/zfs list -H -o name -t snapshot -S creation | grep $DST_DATASET` do c=`expr $c + 1` [ $c -gt $MAX_SNAPSHOTS ] && /sbin/zfs destroy $snapshot && log "Deleted snapshot: $snapshot" done # Backup off-site log "Starts backup off-site with rclone" for x in `ls $NEW_SNAPSHOT_DIR/backup-domains/qemu/*.xml` do filename=$(/usr/bin/basename $x) log "Starts rclone $filename" /usr/bin/nice -n 19 /bin/cat $x | /usr/bin/nice -n 19 /usr/bin/rclone --drive-impersonate backup-ovh@elpuig.xeill.net rcat backup-drive:ovh-backup/$DATE_TIME/backup-domains/qemu/$filename --config /root/.config/rclone/rclone.config --syslog --tpslimit=5 --bwlimit 8M && log "$filename rcloned" || log "$filename failed" done for x in `ls $NEW_SNAPSHOT_DIR/qemu/save/*.save` do filename=$(/usr/bin/basename $x).zst log "Starts rclone $filename" /usr/bin/nice -n 19 /bin/cat $x | /usr/bin/nice -n 19 /usr/bin/pzstd -c | /usr/bin/nice -n 19 /usr/bin/rclone --drive-impersonate backup-ovh@elpuig.xeill.net rcat backup-drive:ovh-backup/$DATE_TIME/qemu/save/$filename --config /root/.config/rclone/rclone.config --syslog --tpslimit=5 --bwlimit 8M && log "$filename rcloned" || log "$filename failed" done log "Starts rclone tar $NEW_SNAPSHOT_DIR/images" /usr/bin/nice -n 19 /bin/tar cf - $NEW_SNAPSHOT_DIR/images | /usr/bin/nice -n 19 /usr/bin/pzstd -10 -c -p 4 | /usr/bin/nice -n 19 /usr/bin/rclone --drive-impersonate backup-ovh@elpuig.xeill.net rcat backup-drive:ovh-backup/$DATE_TIME/images.tar.zst --config /root/.config/rclone/rclone.config --syslog --tpslimit=5 && log "tar $NEW_SNAPSHOT_DIR/images rcloned" || log "tar $NEW_SNAPSHOT_DIR/images failed" log "Backup off-site ended" # End script log "End $SCRIPT"