Skip to content

Instantly share code, notes, and snippets.

@jondkelley
Last active April 6, 2018 19:13
Show Gist options
  • Save jondkelley/b6a6944eaf8991c1ce5ba8729bbacb09 to your computer and use it in GitHub Desktop.
Save jondkelley/b6a6944eaf8991c1ce5ba8729bbacb09 to your computer and use it in GitHub Desktop.

Part 1: Build encrypted tunnel from Xen (HYPERVISOR) to Backup server

ONLY SET THIS UP ON MASTER HOSTS

1.1 Temporarily enable Redhat repositories & perform autossh installation

sudo yum -y --enablerepo=base --enablerepo=updates --enablerepo=extras --enablerepo=base --enablerepo=centosplus install epel-release
sudo yum -y --enablerepo=base --enablerepo=updates --enablerepo=extras --enablerepo=base --enablerepo=centosplus install autossh

1.2 Generate an SSH key for public-key authentication

ssh-keygen

(just keep hitting enter, accept the default values)

1.3 Copy the SSH key over to the backup host

ssh-copy-id xbackup@xenstorage2.gvoperations.com

1.4 Configure rc.local

Existing file

vim /etc/rc.local

# Add this line into the file
autossh -M 10984 -N -f -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -i /home/user/.ssh/id_rsa.pub -L 3049:localhost:2049 xbackup@xenstorage2.gvoperations.com

1.5 Setup rc.local service entry on Xen Server 7.2

New file

# vi /etc/systemd/system/rc-local.service


# Add these lines
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target

1.6 Place rc.local in startup items for Xen

chmod +x /etc/rc.d/rc.local
sudo systemctl start rc-local
sudo systemctl enable rc-local
sudo systemctl status rc-local

1.7 Make sure autoSSH is running

ps auxf | grep autossh

You should see: autossh -M 10984 -N -o PubkeyAuthentication=yes -o PasswordAuthentication=no -i /home/user/.ssh/id_rsa.pub -L 3049:localhost:2049 xbackup@xenstorage2.gvoperations.com

1.8 Build NFS mount point

mkdir -p /mnt/nfs/data

1.9 Test NFS mount

This should mount the data share under /mnt/nfs/data in 30 seconds or less

mount -t nfs -o port=3049 localhost:/mnt/data /mnt/nfs/data

Part 2: Configure backups on the xen host

2.1 Download the backup script off Github

cd /opt
wget https://github.com/NAUbackup/VmBackup/archive/master.zip

2.2 Extract the backup script

unzip master.zip
rm -fv master.zip

Part 3: Execution

3.1 Create backup script to run on schedule

New file

vi /opt/backup.sh

Append this contents:

#!/usr/bin/bash
# Jonathan Kelley :: Feb, 2, 2018
# Check if NFS is currently mounted

PATH=/opt/xensource/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

/usr/bin/mount | /usr/bin/grep 'localhost:/mnt/data on /mnt/nfs/data'  > /dev/null 2>&1
if [ $? -eq 0 ]
then
	/usr/bin/echo "NFS is already mounted, OK."
else
	/usr/bin/echo "NFS not mounted, mounting filesystem."
	/usr/bin/mount -t nfs -o port=3049 localhost:/mnt/data /mnt/nfs/data	
fi

# Parse the configurations
case "$1" in
  "daily")
     /opt/VmBackup-master/VmBackup.py /root/VmBackup.pass /opt/VmBackup-master/daily.cfg
    ;;
  "biweekly")
     /opt/VmBackup-master/VmBackup.py /root/VmBackup.pass /opt/VmBackup-master/biweekly.cfg
    ;;
  "weekly")
     /opt/VmBackup-master/VmBackup.py /root/VmBackup.pass /opt/VmBackup-master/weekly.cfg
    ;;
  "monthly")
     /opt/VmBackup-master/VmBackup.py /root/VmBackup.pass /opt/VmBackup-master/monthly.cfg
    ;;
  *)
    /usr/bin/echo "Error : You must specify daily, biweekly, weekly, or monthly as your first arguement"
    exit 1
    ;;
esac
/usr/bin/umount /mnt/nfs/data

Change permission to executable

chmod 755 /opt/backup.sh
chmod 755 /opt/VmBackup-master/VmBackup.py

3.2 Setup crontab (scheduled tasks) to perform the backups

Change existing file /etc/crontab

vim /etc/crontab

Contents

31 15 * * * /opt/backup.sh daily
30 2 * * 1,5 /opt/backup.sh biweekly
30 8 * * 3 /opt/backup.sh weekly
1 3 1 * * /opt/backup.sh monthly

Part 4: Agent Configuration

Now we will setup the configurations

4.1 Configure the password for XenAPI

Replace password in the single quotes with your Xen console password. This is used to export the images over Xen API. This step needs to be re-run every time the root passwords change.

/opt/VmBackup-master/VmBackup.py 'ROOT PASSWORD' create-password-file=/root/VmBackup.pass

NOTE This is MASTERS hypervisor password. Re-run this with a new password if the hypervisor password changes, otherwise backups will stop.

4.2 Place the backup configurations

cd /opt/VmBackup-master/

vi biweekly.cfg
# File contents in this gist (below)
vi daily.cfg
# File contents in this gist (below)
vi monthly.cfg
# File contents in this gist (below)
vi weekly.cfg

# IMPORTANT! IMPORTANT! ADVISIO!
# Please update the variables in each config file.
# EXAMPLE:
# backup_dir=/mnt/nfs/data/hydra-p1
# status_log=/mnt/nfs/data/hydra-p1.log

4.3 Verify you have configured all backup paths

If you see any results, those files still need to be updated with current POOL NAME

grep -i hydra *.cfg

4.4 Create the mount point for backups

mkdir -p $(grep backup_dir daily.cfg | cut -d"=" -f2)

Part 5: Configure customers / servers for backups

Every guest VM needs a name to get backups. This is a 'tag' in the name which tells the backup script to do the job.

BUDAILY-servername
BUWEEKLY-servername
BUBIWEEK-servername
BUMONTHLY-servername
# Title: NAUbackup / VmBackup - a XenServer vm-export and vdi-export Backup Script
# Package Contents: README, VmBackup.py, example.cfg (this file)
# Example config file for VmBackup.py (V3.1)
#### high level VmBackup settings ################
#### note - if any of these are not specified ####
#### then VmBackup has default constants ####
# Take Xen Pool DB backup: 0=No, 1=Yes (script default to 0=No)
pool_db_backup=0
# How many backups to keep for each vm (script default to 4)
max_backups=3
#Backup Directory path (script default /snapshots/BACKUPS)
backup_dir=/mnt/nfs/data/hydra-p1
status_log=/mnt/nfs/data/hydra-p1.log
# applicable if vdi-export is used
# vdi_export_format either raw or vhd (script default to raw)
vdi_export_format=vhd
### Note: All excludes must come before any vdi-export or vm-export definitions
# exclude selected VMs from VM prefix wildcards
#exclude=PROD-WinDomainController
#exclude=DEV-DestructiveTest
#### specific VMs backup settings ####
### Note: vdi-export definitions should come before vm-export definitions and
### will take precedence in the event that any duplicates are found.
# special vdi-export - only backs up first disk. See README Documenation!
#vdi-export=my-vm-name
# vm-export VM name-label of vm to backup. One per line - notice :max_backups override.
#vm-export=my-vm-name
#vm-export=my-second-vm
#vm-export=my-third-vm:3
# vm-export using VM prefix wildcard - notice DEV* has :max_backups overide
#vm-export=PROD.*
#vm-export=DEV.*:2
vm-export=BUBIWEEK.*
# Title: NAUbackup / VmBackup - a XenServer vm-export and vdi-export Backup Script
# Package Contents: README, VmBackup.py, example.cfg (this file)
# Example config file for VmBackup.py (V3.1)
#### high level VmBackup settings ################
#### note - if any of these are not specified ####
#### then VmBackup has default constants ####
# Take Xen Pool DB backup: 0=No, 1=Yes (script default to 0=No)
pool_db_backup=0
# How many backups to keep for each vm (script default to 4)
max_backups=3
#Backup Directory path (script default /snapshots/BACKUPS)
backup_dir=/mnt/nfs/data/hydra-p1
status_log=/mnt/nfs/data/hydra-p1.log
# applicable if vdi-export is used
# vdi_export_format either raw or vhd (script default to raw)
vdi_export_format=vhd
### Note: All excludes must come before any vdi-export or vm-export definitions
# exclude selected VMs from VM prefix wildcards
#exclude=PROD-WinDomainController
#exclude=DEV-DestructiveTest
#### specific VMs backup settings ####
### Note: vdi-export definitions should come before vm-export definitions and
### will take precedence in the event that any duplicates are found.
# special vdi-export - only backs up first disk. See README Documenation!
#vdi-export=my-vm-name
# vm-export VM name-label of vm to backup. One per line - notice :max_backups override.
#vm-export=my-vm-name
#vm-export=my-second-vm
#vm-export=my-third-vm:3
# vm-export using VM prefix wildcard - notice DEV* has :max_backups overide
#vm-export=PROD.*
#vm-export=DEV.*:2
vm-export=BUDAILY.*
# Title: NAUbackup / VmBackup - a XenServer vm-export and vdi-export Backup Script
# Package Contents: README, VmBackup.py, example.cfg (this file)
# Example config file for VmBackup.py (V3.1)
#### high level VmBackup settings ################
#### note - if any of these are not specified ####
#### then VmBackup has default constants ####
# Take Xen Pool DB backup: 0=No, 1=Yes (script default to 0=No)
pool_db_backup=0
# How many backups to keep for each vm (script default to 4)
max_backups=3
#Backup Directory path (script default /snapshots/BACKUPS)
backup_dir=/mnt/nfs/data/hydra-p1
status_log=/mnt/nfs/data/hydra-p1.log
# applicable if vdi-export is used
# vdi_export_format either raw or vhd (script default to raw)
vdi_export_format=vhd
### Note: All excludes must come before any vdi-export or vm-export definitions
# exclude selected VMs from VM prefix wildcards
#exclude=PROD-WinDomainController
#exclude=DEV-DestructiveTest
#### specific VMs backup settings ####
### Note: vdi-export definitions should come before vm-export definitions and
### will take precedence in the event that any duplicates are found.
# special vdi-export - only backs up first disk. See README Documenation!
#vdi-export=my-vm-name
# vm-export VM name-label of vm to backup. One per line - notice :max_backups override.
#vm-export=my-vm-name
#vm-export=my-second-vm
#vm-export=my-third-vm:3
# vm-export using VM prefix wildcard - notice DEV* has :max_backups overide
#vm-export=PROD.*
#vm-export=DEV.*:2
vm-export=BUMONTHLY.*
# Title: NAUbackup / VmBackup - a XenServer vm-export and vdi-export Backup Script
# Package Contents: README, VmBackup.py, example.cfg (this file)
# Example config file for VmBackup.py (V3.1)
#### high level VmBackup settings ################
#### note - if any of these are not specified ####
#### then VmBackup has default constants ####
# Take Xen Pool DB backup: 0=No, 1=Yes (script default to 0=No)
pool_db_backup=0
# How many backups to keep for each vm (script default to 4)
max_backups=3
#Backup Directory path (script default /snapshots/BACKUPS)
backup_dir=/mnt/nfs/data/hydra-p1
status_log=/mnt/nfs/data/hydra-p1.log
# applicable if vdi-export is used
# vdi_export_format either raw or vhd (script default to raw)
vdi_export_format=vhd
### Note: All excludes must come before any vdi-export or vm-export definitions
# exclude selected VMs from VM prefix wildcards
#exclude=PROD-WinDomainController
#exclude=DEV-DestructiveTest
#### specific VMs backup settings ####
### Note: vdi-export definitions should come before vm-export definitions and
### will take precedence in the event that any duplicates are found.
# special vdi-export - only backs up first disk. See README Documenation!
#vdi-export=my-vm-name
# vm-export VM name-label of vm to backup. One per line - notice :max_backups override.
#vm-export=my-vm-name
#vm-export=my-second-vm
#vm-export=my-third-vm:3
# vm-export using VM prefix wildcard - notice DEV* has :max_backups overide
#vm-export=PROD.*
#vm-export=DEV.*:2
vm-export=BUWEEKLY.*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment