Skip to content

Instantly share code, notes, and snippets.

@baetheus
Last active September 12, 2019 23:21
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save baetheus/ea4dd6e8dab1877868ff to your computer and use it in GitHub Desktop.
Zone Backup Script
#!/bin/sh
# This script will backup all smartos zones to remote servers
# or local directories.
# Brandon Blaylock (brandon@null.pub)
### Defaults
FTPHOST=
SCPHOST=
LOCALDIR=
TEMPDIR=$(mktemp -d)
DATE=$(date '+%Y%m%d')
### Usage Menu
usage()
{
cat << EOF
usage: $0 <OPTIONS>
This script will stop, backup, gzip, and send all vms. It assumes the
credentials for any remote server have been configured. More than one
backup location can be supplied.
OPTIONS:
-h Show this message
-f <server> FTP host to send backups to. (ex. google.com)
-s <server> SCP host to send backups to. (ex. google.com:/backups/)
-l <directory> Directory to store backups locally. (ex. /backups/)
The backups will be stored in the following form:
<alias>-<YEAR><MONTH><DAY>.gz
To restore a gzipped backup, perform the following:
gunzip -c <backup> |vmadm receive
NOTES:
FTP credentials can be stored in ~/.netrc with permissions 600 and
formatted thusly:
machine <fqdn/ip>
login <username>
password <password>
EOF
}
main() {
cd $TEMPDIR
for uuid in `vmadm list -p -o uuid`; do
vm=$(vmadm get $uuid |json alias)
backup="$vm-$DATE.gz"
manifest="$vm-$DATE.json"
echo -n "Saving $vm manifest to $manifest.. "
vmadm get $uuid > $manifest
if [[ $? -ne 0 ]]; then echo "ERROR: Manifest get failed."; exit 2
else echo "Ok"; fi
echo -n "Stopping $vm and saving to $backup.. "
vmadm send $uuid |gzip > $TEMPDIR/$vm-$DATE.gz
if [[ $? -ne 0 ]]; then echo "ERROR: Looks like the backup failed."; exit 3; fi
echo -n "Starting $vm.. "
vmadm start $uuid
if [[ $? -ne 0 ]]; then echo "ERROR: Couldn't start $vm."; exit 4; fi
if [[ -n $FTPHOST ]]; then
echo -n "FTPing $vm and manifest to $FTPHOST.. "
ftp $FTPHOST <<< "put $backup"
if [[ $? -ne 0 ]]; then echo "ERROR: Couldn't ftp $vm."; exit 5; fi
ftp $FTPHOST <<< "put $manifest"
if [[ $? -ne 0 ]]; then echo "ERROR: Couldn't ftp $vm manifest."; exit 6
else echo "Ok"; fi
fi
if [[ -n $SCPHOST ]]; then
echo "SCPing $vm to $SCPHOST.."
scp $backup $SCPHOST
if [[ $? -ne 0 ]]; then echo "ERROR: Couldn't scp $vm."; exit 7; fi
scp $manifest $SCPHOST
if [[ $? -ne 0 ]]; then echo "ERROR: Couldn't scp $vm manifest."; exit 9
else echo "Ok"; fi
fi
if [[ -n $LOCALDIR ]]; then
echo -n "Moving $vm to $LOCALDIR.. "
mv $backup $LOCALDIR
if [[ $? -ne 0 ]]; then echo "ERROR: Couldn't move $vm."; exit 10; fi
mv $manifest $LOCALDIR
if [[ $? -ne 0 ]]; then echo "ERROR: Couldn't move $vm manifest."; exit 11
else echo "Ok"; fi
else
echo -n "Removing $backup.. "
rm $backup $manifest
if [[ $? -ne 0 ]]; then echo "ERROR: Couldn't remove temporary backups for $vm."; exit 12
else echo "Ok"; fi
fi
done
}
### Require Options
[[ $# -gt 0 ]] || { usage; exit 0; }
### Parse Options
while getopts “:hf:s:l:v” OPTION
do
case $OPTION in
h) usage; exit 0;;
f) FTPHOST=$OPTARG;;
s) SCPHOST=$OPTARG;;
l) LOCALDIR=$OPTARG;;
?) usage; exit 0;;
esac
done
if [[ -z $FTPHOST ]] && [[ -z $SCPHOST ]] && [[ -z $LOCALDIR ]]; then
usage
exit 0
else
main
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment