rsync-backup written by Christian Schwamborn
published under GNU General Public License v.2

bugs and suggestions to:
christian.schwamborn@nswit.de


You are using this scrip at your own risk, the author is not responsible
for data damage or losses in any way.

WARNING: This documentation is outdated and has to be rewritten

What this is:
You can use this script to backup directories or LV's with rsync, source or
destination can be local or remote (ssh only, no rsync daemon mode supported
yet). If you are using LV, the script can automatically create snapshots.
You can define several sets for incremental backups with different expire
times.

How to use:
The script provides some parameters most of them can be set in the
configuration file as well as in the command line (except config, which can
only used in the command line). Most of them have reasonable defaults, some
should be configured for local needs (i.e. the operator email-address) but
bu_sources is the most important, where you configure your backups.
The only command the script currently understands is 'backup'.
Add this, for a usual backup scenario to your crontab:

0 12 * * 1-5 root /<path>/rsync-backup backup bu_set=0
0 2 * * 2-5 root /<path>/rsync-backup backup bu_set=1
0 2 * * 1 root /<path>/rsync-backup backup bu_set=2

This creates backups at 2:00 and 12:00 every workday, the twelve o'clock
backups will be in backup-set 0, the two am backups form Tuesday to Friday
will be in backup-set 1 and Monday's two am backus will be backup-set 2.
Read further down for backup-sets (read below about backup-sets).

The script has some flaws:
  -This script currently works only with LVM2, no EVMS support yet.
  -Be carefull with the configuration, the parameters are not completely
   checked for every possible misconfiguration.
  -You have to keep track of the freespace of your volumegroups.
  -Be aware, that if your snapshots grow faster than you assumed, they will
   become unusable.
  -Don't use spaces or secial characters that the shell may interprete in your
   pathnames.

This script is written for the bash, other shells might work, it also uses
some external commands: mount, umount, grep, date, logger, lvcreate, lvremove,
sed, awk, ssh, rsync, mutt (and a configured emailsystem)

This is a list of the variables that can be configured. The configuration is
simply sourced, that defines the format of the file.
'config'        (default: "/etc/rsync-backup/backup.conf") only as cli
                parameter defines the configuration file to use, i.e.
                'rsync-backup backup config=/etc/rsync-backup/my.conf'
'rsync_ign_msg' (default: "/etc/rsync-backup/ignore.list") a list of error
                strings we just ignore in the error log, i.e. when files vanish
                during the backup.
'bu_lang'       (default: "rsb_lang_en") this is the language file, there is
                only english available right now, translations are welcome.
'log_path'      (default: "/var/log/rsync-backup") is the directory where the
                logfiles will be stored. Carefull those files can be huge when
                --verbose and --progress is passed to rsync on filesystems
                with lots of files.
'log_keep'      (default: 30) limits the logfile age to the given number of
                days
'debug'         (default: 3) sets the debug level.
                0 = no output at all; 1 = errors only; 2 = warnings;
                3 = normal operation messages; 4 = all debug messages.
'off_days'      (default: "Sat Sun") to exclude weekdays from the expire count
                of backups.
'rsync_flags'   (default: "--archive --compress --numeric-ids --delete") to
                configure the default rsync flags to use, if nothing else is
                given for a specific backup
'rsync_ign_e'   (default: (24)) takes an array as a list of rsync return values
                to be ignored as errors
'snap_size'     (by default a nullsting) gives the size for the snapshots in
                megabytes by default or in the specified suffix [kKmMgGtTpPeE]
                If this is set, the script will create snapshots for all LV's
                that it processes. If snap_size is left empty snapshots will
                only be created for those LV's, where snap_size is specified
                in bu_sources.
'snap_suffix'   (default: "_backupsnap") a suffix appended to the LV's name
                as the name for the snapshot.
'snap_mnt_base' (default: "/srv/share/snapshots"} where the snapshots will be
                mounted.
'bu_set'        (default: 0) the number of the backupset, usefull as a cli
                parameter in crontab to differentiate backups with varying
                expire age. If only one backup-set is used for incremental
                backups, it should stay 0.
'mail_subject'  (default: "[rsync-backup] from localhost.local") subject for
                error messages via email.
'operator_mail' (default: "") if set, error messages will be sent there.
'ip_suffix'     (default: "_inprogress") a string suffix that is appended to
                the backups beeing in progress.
'bu_sources'    (no default) here are the backups defined, it's shell array of
                strings with no spaces.
'dst_fs_ids'    (default: "") if set, we try to mount the fs with the specific
                id as destination for the backup
'id_path'       (default: "/dev/disk/by-uuid") the path where we searech for
                filesystem ids
'dst_fs_mnt_path' (default: "/mnt/backup_drive") where to mount the destination
                filesystem
'dst_fs_mnt_opts' (default: "-o rw,noexec,nodev,noatime,acl,user_xattr") the
                default mount options for the destination filesystem

The last one is the most important parameter described further:
bu_sources=("<source_path>;<snap_mnt_base>;<destination>;<backup-sets>;\
<snap_size>;<rsync_flags>(;<dst_fs_ids>)?(;<dst_fs_mnt_opts>)?" "...")
'source_path'   a path you want to backup or an LV, i.e. /home/myname
                or /dev/GROUP1/home (for a LV home on the VG GROUP1). The
                script will determine by it's own where the LV is mounted.
                This can also be a remote path similar to the rsync syntax
                i.e. root@hostname.local:/home/myname or
                root@hostname.local:/dev/GROUP1/home (the latter is unlike
                rsync, the script will substitute the mount path for the
                backup. I have done this for snapshots of LV's that are not
                mounted on the machine where they're backuped from, i.e.
                when you want to backup LV's used in virtual servers like xen.
'snap_mnt_base' see above. can be omitted when no snapshot is used
'destination'   similar to <source_path> this can be a local or remote path,
                when you use incremental backups the script will modify the
                path: /srv/share/backups/myhome will become for example:
                /srv/share/backups/myhome_2011-03-22_16-03-00_0
'backup-sets'   is a list of expire time in days for backup sets. i.e.
                5,10,30 means for the crontab example from above the twelve
                o'clock backups will be kept 5 workdays (one week, if
                off_days is set to "Sat Sun"), the two o'clock backups from
                Tuesday to Friday will be deleted after 10 workdays and the
                early Monday backup will stay 1 1/2 months.
                If backup-sets is empty, a normal rsync into destination will
                be performed without incremental functionality.
'snap_size'     see above. can be omitted when no snapshot is wanted. Only if
                snap_size is set the script will create a snapshot.
'rsync_flags'   see above.
'dst_fs_ids'	this is optionally, see above.

Write a config (/etc/rsync-backup/backup.conf) with at least bu_sources and
operator_mail like this to backup a simple workstation:

operator_mail="your.email@address.org"
my_rsync_flags="--archive,--compress,--numeric-ids,--delete,--progress,--verbose"
my_dst_base="root@some.server.de:/srv/share/backup"
bu_sources=("/dev/GROUP1/home;/mnt/backup;${my_dst_base}/home;\
5,10,60;1G;${my_rsync_flags}" \
"/dev/GROUP1/root;/mnt/backup;${my_dst_base}/root;\
2,5,30;512M;${my_rsync_flags},--one-file-system" \
"/dev/GROUP1/var;/mnt/backup;${my_dst_base}/var;\
2,5,30;512M;${my_rsync_flags},--one-file-system")

Since the configuration is simply sourced, make sure you don't overwrite
some internal script variables by accident.

If you use remote source or destination, you want to use a public key
authentication for ssh, since all remote traffic is tunneled thrugh ssh.
A ssh key can be easily created with "ssh-keygen -t rsa -b 4096" for a 4096 bit
rsa key or "ssh-keygen -t dsa" for a dsa key (leave the bits alone, a dsa key
must be exactly 1024 bits).
Place the public key of the pair you just created in ~/.ssh/authorized_keys of
the user you're connecting with. After that you should connect at least once
manually to accept the servers ssh key. Use the same URL to connect, as you
used in your config (short machine name or fqdn) to store the public key for
the right name in ~/.ssh/known_hosts.

An easy way to configure a simple local MTA is to use ssmtp if you want emails
when something with the backup fails.

