Friday, October 26, 2012

Rsync Backup Script.

This script can keep history of last 3 backups.

Source Link
http://michaeldadams.org/projects/backup/backup


#!/bin/sh

## Copyright (c) 2010-2011, Michael D. Adams
## 
## 
## Permission to use, copy, modify, and/or distribute this software for any
## purpose with or without fee is hereby granted, provided that the above
## copyright notice and this permission notice appear in all copies.
## 
## THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
## WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
## MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
## ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
## WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
## ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
## OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

set -u # Variables that are not set are errors
set -x # Print commands when executed

HOST=user@host.com
SRC=/path/to/src # no slash at end
DST=/path/to/dst # no slash at end

# If backup.0 already exists then bump age numbers and remove old backup.3
if ssh "$HOST" test -d "$DST"/backup.0; then
    ssh "$HOST" rm -rf "$DST"/backup.3
    ssh "$HOST" mv "$DST"/backup.2 "$DST"/backup.3
    ssh "$HOST" mv "$DST"/backup.1 "$DST"/backup.2
    ssh "$HOST" mv "$DST"/backup.0 "$DST"/backup.1
fi

# --progress: so we know if the program froze
# --archive:
#     recursive, links, permissions, times, group, owner, devices, specials
# --delete: remove no longer existant files in old backup.pre.
#     We don't delete the entire old backup.pre so we can do a fast restart.
# -F: use per-directory ".rync-filter" files to filter
# --rsh: tunnel over SSH
# --link-dest: share hard links to unchanged files with previous backup
rsync --progress --archive --delete -F --rsh=ssh --link-dest=../backup.1 \
  "$SRC" "$HOST":"$DST"/backup.pre &&
ssh "$HOST" mv "$DST"/backup.pre "$DST"/backup.0

No comments:

Post a Comment