Browse Source

First commit

master
Christian Baer 7 years ago
commit
94648b47b8
5 changed files with 233 additions and 0 deletions
  1. +1
    -0
      .gitignore
  2. +9
    -0
      LICENSE.md
  3. +107
    -0
      README.md
  4. +18
    -0
      rmbackup.d/sample.conf.dist
  5. +98
    -0
      rmbackup.sh

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
.DS_Store

+ 9
- 0
LICENSE.md View File

@@ -0,0 +1,9 @@
rmbackup is distributed under the MIT license, which is similar in effect to the BSD license.

> Copyright 2013 Christian Busch (http://github.com/chrisb86)
>
> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 107
- 0
README.md View File

@@ -0,0 +1,107 @@
# Remote Backup (rmbackup)

Remote server backup with rsync and config files
Based on [Backup mit RSYNC] (http://wiki.ubuntuusers.de/Skripte/Backup_mit_RSYNC)

rmbackup is a bash script that uses rsync to do incremental centralized backups of remote servers. It can be configured by config files.

There has to be a user on the remote server that is able to run rsync with root privileges and the backup server must be able to login without a password. The script should be run by cron on the backup server.

## Setting things up with a Linux remote server and a FreeBSD backup server
**On the remote Server:**

- Create a user named _rmbackup_:
$ adduser rmbackup

- Edit sudo config to allow rmbackup group to run rsync with no password:
$ visudo

# /etc/sudoers

Cmnd_Alias RSYNC = /usr/bin/rsync
%rmbackup ALL=(ALL) NOPASSWD: RSYNC

**On the backup Server:**

- Copy your public key to the remote server. Run this as the user that will take the backups:
$ ssh-copy-id rmbackup@<remoteserver>

**On the remote Server:**

- Manually set the password to **\*** in /etc/shadow to prevent console logins, the shell can be set to /bin/bash, as there are no interactive logins.

# /etc/shadow

rmbackup:*:15753:0:99999:7:::

**On the backup Server:**

- Create a config file in rmbackup.d/:

$ cp sample.conf.dist <remotehost>.conf

#Sample conf for backup script
## SSH config for the remote server (default port is 22)
SSH_USER="rmbackup"
SSH_SERVER="example.com"
SSH_PORT=22
# Which folders sould be backed up from the remote server?
REMOTE_SOURCES=(/root /etc /home /var)
# In which folder should we store the backups on the backup server
# (subdirs for the server will be created by the script)?
TARGET="/Backups/"
# Additional command line parameters for ssh (verbose mode, exclude patterns...
# see man rsync for further information)
# RSYNC_CONF=(-v)
- Change the path to _rmbackup.d_ in rmbackup.sh:

# rmbackup.sh
#!/bin/bash
...
# Where to look for config files
CONFLOCATION=/etc/rmbackup.d/*.conf

- Setup the cronjob for the user that does the backups:

$ crontab -e

# /etc/crontab - root's crontab for FreeBSD
#
# $FreeBSD: release/9.1.0/etc/crontab 194170 2009-06-14 06:37:19Z brian $
#
SHELL=/usr/local/bin/bash
PATH=/etc:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root
#minute hour mday month wday command
#
0 */1 * * * /home/rmbackup/bin/backup.sh

Backups will now be taken hourly.

## Changelog

**2013-02-18**

- First commit

## License

rmbackup is distributed under the MIT license, which is similar in effect to the BSD license.

> Copyright 2013 Christian Busch (http://github.com/chrisb86)
>
> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 18
- 0
rmbackup.d/sample.conf.dist View File

@@ -0,0 +1,18 @@
#Sample conf for backup script

## SSH config for the remote server (default port is 22)
SSH_USER="rmbackup"
SSH_SERVER="example.com"
SSH_PORT=22

# Which folders sould be backed up from the remote server?
REMOTE_SOURCES=(/root /etc /home /var)

# In which folder should we store the backups on the backup server
# (subdirs for the server will be created by the script)?

TARGET="/Backups/"

# Additional command line parameters for ssh (verbose mode, exclude patterns...
# see man rsync for further information)
# RSYNC_CONF=(-v)

+ 98
- 0
rmbackup.sh View File

@@ -0,0 +1,98 @@
#!/bin/bash

# Remote server backup with rsync and config files
# Based on http://wiki.ubuntuusers.de/Skripte/Backup_mit_RSYNC

# Copyright 2013 Christian Busch
# http://github.com/chrisb86/

# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# Where to look for config files
CONFLOCATION=/etc/rmbackup.d/*.conf

# Uncomment and add your address if you want to receive the script ouput via e-mail
#MAILREC="jdoe@example.com"

# Give the paths to used tools
SSH="/usr/bin/ssh"; LN="/bin/ln"; ECHO="/bin/echo"; DATE="/bin/date";
MAIL="/usr/bin/mail"; RSYNC="/usr/local/bin/rsync";
LAST="last"; INC="--link-dest=../$LAST"

### Do not edit below this line ###

# Loop through configs configs
for f in $CONFLOCATION
do
# set some rsync parameters
RSYNC_CONF_DEFAULT=(--rsync-path='sudo rsync' --delete --quiet)

# Date format for naming the snapshot folders
TODAY=$($DATE +%Y-%m-%d)

# load config file
source $f
LOG=$0.log
echo $($DATE)" starting backup for "$SSH_SERVER" (using config file"$f")" >> $LOG

# some path fiddeling
if [ "${TARGET:${#TARGET}-1:1}" != "/" ]; then
TARGET=$TARGET/
fi

TARGET=$TARGET$SSH_SERVER/

if [ "$SSH_USER" ] && [ "$SSH_PORT" ]; then
S="$SSH -p $SSH_PORT -l $SSH_USER";
fi

# do the backup
for SOURCE in "${REMOTE_SOURCES[@]}"
do
if [ "$S" ] && [ "$SSH_SERVER" ] && [ -z "$TOSSH" ]; then
$ECHO "$RSYNC -e \"$S\" -avR \"$SSH_SERVER:$SOURCE\" ${RSYNC_CONF_DEFAULT[@]} ${RSYNC_CONF[@]} $TARGET$TODAY $INC" >> $LOG
$RSYNC -e "$S" -avR "$SSH_SERVER:\"$SOURCE\"" "${RSYNC_CONF_DEFAULT[@]}" "${RSYNC_CONF[@]}" "$TARGET"$TODAY $INC >> $LOG 2>&1
if [ $? -ne 0 ]; then
ERROR=1
fi
fi
done

# move the folders and link "last"
if ( [ "$S" ] && [ "$SSH_SERVER" ] && [ -z "$TOSSH" ] ) || ( [ -z "$S" ] ); then
$ECHO "$LN -nsf $TARGET$TODAY $TARGET$LAST" >> $LOG
$LN -nsf "$TARGET"$TODAY "$TARGET"$LAST >> $LOG 2>&1
if [ $? -ne 0 ]; then
ERROR=1
fi
fi
echo $($DATE)" finished backup for "$SSH_SERVER"." >> $LOG

# send mail if it's configured
if [ -n "$MAILREC" ]; then
if [ $ERROR ];then
$MAIL -s "Error Backup $LOG" $MAILREC < $LOG
else
$MAIL -s "Backup $LOG" $MAILREC < $LOG
fi
fi

done

Loading…
Cancel
Save