Skip to content

Instantly share code, notes, and snippets.

@mgoodness
Created July 14, 2015 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgoodness/4172eea3f469c111aba9 to your computer and use it in GitHub Desktop.
Save mgoodness/4172eea3f469c111aba9 to your computer and use it in GitHub Desktop.
Script for backing up from multiple locations via rsync using hardlinks
#!/bin/bash
# *** Accepts two optional arguments
# -tDATE: today's date (ISO-8601, yyyy-mm-dd)
# -yDATE: yesterday's date (ISO-8601, yyyy-mm-dd)
#
# Use these if a nightly backup is missed.
# "Yesterday's date" should be the date of the last successful backup.
# Otherwise --link-dest will fail and rsync will do a full copy
# instead of a diff.
#
# Example: the backup from 2013-08-21 failed.
# If the job is allowed to run automatically on 2013-08-22,
# --link-dest will point to the 2013-08-21 folders which do not exist
# (or are empty). That will cause rsync to create a full backup
# instead of a differential one. Instead, run
# 'rsync-backup -t2013-08-22 -y2013-08-20' the following morning. Doing
# so wll preservs the diff going forward.
#set -o nounset -o xtrace
unset PATH
# --- system commands used by script ---
ID=/usr/bin/id
ECHO=/bin/echo
DATE=/bin/date
GETOPT=/usr/bin/getopt
MOUNT=/bin/mount
RM=/bin/rm
MKDIR=/bin/mkdir
RSYNC=/usr/bin/rsync
# --- local locations ---
MOUNT_DEVICE=/dev/mapper/vg_nasit3-lv_backups
SNAPSHOT_RW=/srv/backups
EXCLUDES=/usr/local/etc/backup_exclude
SSH_KEY=/root/.ssh/id_dsa_nasit3
# --- dates ---
TODAY=$($DATE -I)
YESTERDAY=$($DATE -d "1 day ago" -I)
MONTH_AGO=$($DATE -d "3 months ago" -I)
MONTH_AGO_OLD=$($DATE -d "3 months ago" +%d-%b-%Y)
#YESTERDAY=$($DATE -d "1 day ago" +%d-%b-%Y)
DURST_MONTH_AGO=$($DATE -d "1 month ago" -I)
# --- remote locations ---
declare -A sources
sources=( [caldera-cart]=caldera-cart:/srv/backups
[filesrv]=filesrv:/srv/backups
[filesrv_data]=filesrv:/srv/data
[itsupport]=itsupport:/srv/backups
[caldera-tg5]=caldera-tg5:/srv/backups
[caldera-tg4]=caldera-tg4:/srv/backups
[dc1]=dc1:/srv/backups
[dc2]=dc2:/srv/backups
[prep2]=prep2:/srv/backups
[db1]=db1:/srv/backups
[db2]=db2:/srv/backups
[db3]=db3:/srv/backups
[postgresql]=postgresql:/srv/backups
[mserver]=mserver:/srv/backups
[monitor]=monitor:/srv/backups
[PuppetMaster01]=PuppetMaster01:/srv/backups
[services_www]=services:/var/www
[services_extranet]=services:/srv/web
[ca]=ca:/srv/backups
[stash]=stash:/home/stash
)
declare -A durstdb
durstdb=( [r11]=r11:/mnt/images/HotFolders/SYSTEM_BACKUP
[r9]=r9:/mnt/images/HotFolders/SYSTEM_BACKUP
[r8]=r8:/mnt/images/HotFolders/SYSTEM_BACKUP
[r5]=r5:/mnt/images/HotFolders/SYSTEM_BACKUP
[r4]=r4:/mnt/images/HotFolders/SYSTEM_BACKUP
)
declare -A durstsw
durstsw=( [r10]=r10:
[r6]=r6:
)
# --- the script ---
# make sure running as root
if (( `$ID -u` != 0 )); then
{
$ECHO "Sorry, must be root. Exiting..."
exit
} fi
TEMP=`$GETOPT -o t::y:: -n 'rsync-backup.sh' -- "$@"`
eval set -- "$TEMP"
while true; do
case "$1" in
-t ) TODAY="$2"; shift 2 ;;
-y ) YESTERDAY="$2"; shift 2 ;;
* ) break ;;
esac
done
for label in "${!sources[@]}"; do
{
$ECHO -e "\n\n*** $label ***"
$MKDIR -p "$SNAPSHOT_RW/$label/$TODAY"
$RSYNC --link-dest="$SNAPSHOT_RW/$label/$YESTERDAY" \
-avz --numeric-ids --delete --delete-excluded \
--exclude-from="$EXCLUDES" -e "ssh -i "$SSH_KEY"" \
"${sources[$label]}/" "$SNAPSHOT_RW/$label/$TODAY"
$RM -rf "$SNAPSHOT_RW/$label/$MONTH_AGO_OLD"
$RM -rf "$SNAPSHOT_RW/$label/$MONTH_AGO"
} done
for label in "${!durstdb[@]}"; do
{
$ECHO -e "\n\n*** $label ***"
$MKDIR -p "$SNAPSHOT_RW/$label"
$RSYNC -avz --numeric-ids -e "ssh -i "$SSH_KEY"" \
"${durstdb[$label]}/DB*-$TODAY.*" "$SNAPSHOT_RW/$label"
$RM -rf "$SNAPSHOT_RW/$label/DB*-$DURST_MONTH_AGO.*"
} done
for label in "${!durstsw[@]}"; do
{
$ECHO -e "\n\n*** $label ***"
$MKDIR -p "$SNAPSHOT_RW/$label/$TODAY"
$RSYNC --link-dest="$SNAPSHOT_RW/$label/$YESTERDAY" \
-avzR --numeric-ids --delete --delete-excluded \
--exclude-from="$EXCLUDES" -e "ssh -i "$SSH_KEY"" \
"${durstsw[$label]}/usr/local/printsw/" "$SNAPSHOT_RW/$label/$TODAY"
$RSYNC -avz --numeric-ids -e "ssh -i "$SSH_KEY"" \
"${durstsw[$label]}/mnt/images/Statistics/DPJobState-$TODAY.txt" \
"$SNAPSHOT_RW/$label/$TODAY"
$RSYNC -avz --numeric-ids -e "ssh -i "$SSH_KEY"" \
"${durstsw[$label]}/mnt/images/Statistics/DPPrinterState.txt" \
"$SNAPSHOT_RW/$label/$TODAY"
$RSYNC -avz --numeric-ids -e "ssh -i "$SSH_KEY"" \
"${durstsw[$label]}/mnt/images/Statistics/DPQueueState.txt" \
"$SNAPSHOT_RW/$label/$TODAY"
$RM -rf "$SNAPSHOT_RW/$label/$MONTH_AGO"
} done
$ECHO -e "\n\n*** FB1 ***"
$MKDIR -p "$SNAPSHOT_RW/FB1/$TODAY"
$RSYNC --link-dest="$SNAPSHOT_RW/FB1/$YESTERDAY" -avzR --delete --delete-excluded \
--exclude-from="$EXCLUDES" FB10000-021::FB1configs "$SNAPSHOT_RW/FB1/$TODAY"
$RM -rf "$SNAPSHOT_RW/FB1/$MONTH_AGO"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment