Backup script for Linux or OS X

A simple backup script for backing up my laptop. I have an OS X and Linux laptop so I wanted one script to put on both of them:


#!/usr/bin/env

homedir="$(echo $HOME)"
dom=$(date +%d)
###################
# START CONFIGURATION
###################

# On the 15th of the month run an rsync --delete to clean out old file. Otherwise just run a regular rsync.
if [ ${dom} -eq 15 ]; then
  rsynccmnd="rsync --delete -avhz --stats --progress"
else
  rsynccmnd="rsync -avhz --stats --progress"fi

# Path to the exclude file
host=$(uname -a|cut -d ' ' -f 1)
if [ "${host}" = "Darwin" ]; then
 # OS X
 excludefile="path to exclude file for OS X"
 #EXAMPLE
 # excludefile="${homedir}/Documents/Backup/backuplaptop_exclude_osx.txt"
else
 # Linux
 #excludefile="path to exclude file for Linux"

 #EXAMPLE
 # excludefile="${homedir}/Documents/Backup/backuplaptop_exclude_linux.txt"

fi

# Information for destination server
dstsrvr="destination.server"
dstusr="username on destination server" # if different from local uid

# Destination directory

#Linux version
#dstdir="Backups/$(hostname)/"

#OS X version
dstdir="Backups/$(hostname -s)/"
vmdstdir="Backups/vm/"

# Information for localhost

# Location of VMware directory on local machine
vmloc="path to virtual machines"
# EXMPLE
# vmloc="Documents/VirtualMachines/windows7vm.vmwarevm"
# DOW to backup VMware directory. Sunday is 0. Saturday is 6.
vmday=1
###################
# END CONFIGURATION
###################

start=$(date +%Y%m%d_%H%M)
echo "===================="
echo " Backup START: ${start}"
echo "===================="
echo " ====="
echo " Backup regular files"
echo " ====="
  ssh ${dstusr}@${dstsrvr} "if [ ! -d \"${dstdir}\" ]; then mkdir -p \"${dstdir}\"; fi"
  ${rsynccmnd} ${homedir}/ --exclude-from=${excludefile} ${dstusr}@${dstsrvr}:${dstdir}

dow=$(date +%w)
if [ ${dow} -eq ${vmday} ]; then
  echo " ====="
  echo " Backup VMware session: ${start} "
  echo " ====="
  ssh ${dstusr}@${dstsrvr} "if [ ! -d \"${vmdstdir}/${vmloc}\" ]; then mkdir -p \"${vmdstdir}/${vmloc}\"; fi"
  ${rsynccmnd} ${homedir}/${vmloc}/ ${dstusr}@${dstsrvr}:${vmdstdir}/${vmloc}/
fi
endtime=$(date +%Y%m%d_%H%M)
echo "===================="

echo " Backup START: ${start}"
echo " Backup END: ${endtime}"

echo "===================="

Comments

Popular posts from this blog

Database, schema, and table sizes in Greenplum

Greenplum update with multiple tables

Show running queries on Postgresql/Greenplum