david/sshbackup
david
/
sshbackup
Archived
1
0
Fork 0

added script, sample config and readme content

This commit is contained in:
David 2012-08-27 11:20:22 +02:00
parent 48f0772211
commit ddac67eeb6
3 changed files with 261 additions and 1 deletions

View File

@ -1,4 +1,37 @@
sshbackup
=========
bash script which does incremental backups through ssh using rsync.
bash script which does incremental backups through ssh using rsync.p
installation/usage:
- put the file sshbackup.sh somewhere you like to have it and run it as root.
- root must have a sshkey to connect to remote machines.
- first option must be the sourcefile.
- sourcefile example can be found in sourcefile_sample.
- logs go to /var/log/sshbackup.log
- configure a cron job for continuous backups
bugs/features:
- the remote sourcepath must be able to be listed by the remote backup user
todo/wanted:
- deploy feature (deploy required settings to remote machine)
- better notification system (for now its just crons mail on output feature)
- check if destination is full
hosted:
- http://git.socialnerds.org/?p=sshbackup.git;a=tree
credits:
- david@socialnerds.org
license:
- public domain (do whatever, i'm not claiming anythinng)

9
sourcefile_sample Normal file
View File

@ -0,0 +1,9 @@
# in here are the backup sources supposed to be
# one source per line (no tailing slashes, bandwidth limit in kbytes/s is optional)
# <user@remote machine> <remote path> <local path> <versions> <bandwidth limit>
user@server1.example.com /srv/projects/* /home/david/testback/git 3
user@server2.example.com /srv/backup/* /home/david/testback/duffman 3
user2@server3 /media/storage2/* /home/david/testback/cartman 3 12500

218
sshbackup.sh Executable file
View File

@ -0,0 +1,218 @@
#!/bin/bash
# ************************************* #
# #
# sshbackup #
# #
# ************************************* #
# **** config section ****
# do not touch as long as you're not me
author="david@socialnerds.org"
version="0.2.0"
#default rsync options. can be overridden by config file
options="-pogEthrzl --numeric-ids --no-motd" #--partial --progress
#dotglob option removes bug while rsyncing folder with no visible files in it
remotecmd="shopt -s dotglob; /usr/bin/sudo /usr/bin/rsync"
localcmd="/usr/bin/rsync"
logfile="/var/log/sshbackup.log"
# **** function definitions ****
#bashtrap will be executed when CTRL+C is pressed
bashtrap()
{
echo
echo "CTRL+C detected."
echo "commiting suicide."
exit 1
}
#prints the usage message
usage()
{
echo
echo "usage: sshbackup <sourcefile>"
echo
echo "OPTIONS:"
echo " -h, --help show this message"
echo " -v, --version show version information"
echo
}
#prints version information
version()
{
echo
echo "sshbackup"
echo
echo -e "vesion: \033[1;37m$version\033[0m"
echo "author: $author"
echo
}
#checks for necessary root privileges
amiroot()
{
local user=$(whoami)
if [ "$(whoami)" != "root" ]; then
echo "error: $USER, you need to gain root privileges. exiting."
exit 1
fi
}
#pulling files through ssh (actual backup process)
sshbackup()
{
#root privileges needed because
#rsync wouldn't keep file permissions
#without root privileges
amiroot
#parameter $1 is the sources file
local sourcefile=$1
#checking if ssh key is available
if [ -z $sshkeyfile ]; then
sshkeyfile="$HOME/.ssh/id_rsa"
fi
if [ -r $sshkeyfile ]; then
:
else
echo "error: ssh keyfile not found in $sshkeyfile. exiting."
exit 1
fi
# reading sources config file (line by line)
while read line; do
# find first letter
local fletter=${line:0:1}
if [ -z $fletter ]; then
#skip line it's empty
:
elif [ $fletter = "#" ]; then
#skip line it's a comment
:
else
#creating needed vars
local server=$(echo "$line" | awk '{print $1}')
local sourcepath=$(echo "$line" | awk '{print $2}')
local destpath=$(echo "$line" | awk '{print $3}')
local versions=$(echo "$line" | awk '{print $4}')
#creating optional vars
local speed=$(echo "$line" | awk '{print $5}')
#check for speed limit
if [ -z $speed ]; then
#no speed limit provided in config
local cmdopt="$options"
else
#adding speed limit to rsync options
local cmdopt="$options --bwlimit=$speed"
fi
#move existing versions
local num=$versions
while [ -d "$destpath/0" ]; do
if [ -d "$destpath/$num" ]; then
mv $destpath/$num $destpath/$((num+1))
if [ $? != 0 ]; then
echo "error - could not move version $num to version $((num+1))"
fi
fi
let num--
done
#create destpath if not existing
mkdir -p $destpath/0
#add link-dest option
if [ -d $destpath/1 ]; then
#existing
local cmdopt="$cmdopt --link-dest=$destpath/1"
fi
#create connectionstring
local constring="$server:$sourcepath"
#run rsync
echo "[$(date '+%Y%m%d%H%M')] starting rsync job: $server:$sourcepath" >> $logfile
$localcmd $cmdopt -e "ssh -q -i $sshkeyfile" --rsync-path="$remotecmd" $constring $destpath/0
if [ $? = "0" ]; then
echo "[$(date '+%Y%m%d%H%M')] rsync job successfully finished: $server:$sourcepath" >> $logfile
else
echo "[$(date '+%Y%m%d%H%M')] rsync job unsuccessful: $server:$sourcepath" >> $logfile
echo "error: rsync job unsuccessful: $server:$sourcepath"
fi
#removing obsolet version(s)
#TODO: remove higher versions if versionsnumber in sourcefile is lower
if [ -d "$destpath/$((versions+1))" ]; then
rm -rf $destpath/$((versions+1))
fi
fi
#count lines for debug output
let linecount++
done < $sourcefile
}
# **** start of script ****
#initialize bashtrap
trap bashtrap INT
#check dependencies/environment
#install dependencies on debian, ubuntu, arch, fedora, redhat, centos
#develope a os_check_install_dependencies_function for different distros
#will be practical for all kinds of scripts
#dependencies: rsync, telnet, bash, sudo
#option handler
if [ -z $1 ]; then
echo "no sourcefile given. exiting."
usage
exit 1
elif [ -r $1 ]; then
sshbackup $1
exit $?
elif [ $1 == "--help" ] || [ $1 == "-h" ]; then
usage
exit 0
elif [ $1 == "--version" ] || [ $1 == "-v" ]; then
version
exit 0
else
echo "unknown option given or sourcefile not readable. exiting."
usage
exit 1
fi
# **** end of script ****