From 1cf6fdfd22c1250045695f5723e84c0d7d0c84bd Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 7 Oct 2014 00:34:29 +0200 Subject: [PATCH] Fuse backup-nonpriv.sh into backup.sh. --- backup-nonpriv.sh | 233 ---------------------------------------------- backup.sh | 104 ++++++++++++++++----- 2 files changed, 80 insertions(+), 257 deletions(-) delete mode 100755 backup-nonpriv.sh diff --git a/backup-nonpriv.sh b/backup-nonpriv.sh deleted file mode 100755 index 06e8e73..0000000 --- a/backup-nonpriv.sh +++ /dev/null @@ -1,233 +0,0 @@ -#!/bin/sh - -# Modifisert utgave av backup.sh som tar backup også på maskiner der kolje ikke har root. - -#dirs -confdir=/home/backup/conf/ #configuration files -storagedir=/backup #mountpoint of huge disc -### - -#exclude-pattern -exclude=$confdir/exclude -[ ! -f $exclude ] && printf "tmp\ncore\n" > $exclude -### - -#syntax of remotestat: -#hostname:/directory/ -remotestatf=$confdir/remotestat -[ -f $remotestatf ] && remotestat=`cat $remotestatf` -### - -PATH=/local/bin:$PATH:/store/bin -export PATH - -#start the logfile -date -echo -echo "Exclude pattern:" -cat $exclude -echo -echo - -umask 027 - -# The computers we want to back up -unixcomputers=`cat $confdir/computers.unix.nonpriv | grep -v ^# | grep -v ^$` -### - -# Backup only one computer, from command line? -if [ $1 ] ; then - unixcomputers=$1 -fi - -#disse bør kunne varieres fra fs til fs? -maksantallfulle=3 #hvor mange fulle vi tar vare på (kan overstyres med conf) -dagermellomfulle=30 #antall dager før det er på tide med ny full -logw=40 -### - -#lager datovariabeler -DATE=`date "+%Y%m%d%H%M"` #format: touch -DATEs=`date "+%Y-%m-%d %H:%M"` #format: tar -### - -#selve backupen -# krever at noen variabler er satt -# krever at vi er i rett katalog -backup() -{ - - printf "%-${logw}s %s\n" "$computer:$filesystem" "$backuplevel backup" - - if [ "$backuplevel" = "daglig" ] || [ "$backuplevel" = "incremental" ] - then - lastd=`cat ../.date` - lastcmd="\"$lastd\"" - else - lastcmd="" - fi - - #expand the exclude-path for use with tar - exf=`ssh $username@$computer "ls ~/.backup/exclude"` - - #We try to run tar on the remote computer - # C is instead of 'cd $filesystem' - # - is 'this directory' - # l is 'stay in local filesystem - if `ssh $username@$computer "sudo /usr/local/sbin/output-encrypted-backup $filesystem $lastcmd" | dd of=$DATE.tmp 2>/dev/null` - then - # perhaps it did work - mv $DATE.tmp $DATE.tgz.gpg - - # FIXME: ignore the file lists for now - - # fix perm - chmod 600 *tgz.gpg #only for us -# chmod 644 *sdx *idx 2>/dev/null #everyone can read - - echo $DATEs > ../.date && - touch -t $DATE ../.date - else - #it did not work - rm $DATE.tmp 2>/dev/null - echo "$backuplevel backup of $computer:$filesystem failed and deleted" - fi - -} - -#Løper gjennom listen av unixmaskiner som vi skal ta backup av -for computer in $unixcomputers -do - - # find the user to do the non-root parts as - if [ -f $confdir/username.$computer ] ; then - username=`cat $confdir/username.$computer` - else - username=backup - fi - - # Try to SSH to the computer without entering a password. - if `ssh -n -o NumberOfPasswordPrompts=0 $username@$computer /bin/true`; then - echo "Passwordless SSH to $username@$computer works." - else - echo "Could not use passwordless SSH to $username@$computer. Exiting." - exit 1; - fi - - # see if se have a local exclude file - if [ -f $confdir/exclude.$computer ] ; then - localexclude=$confdir/exclude.$computer - - echo - echo "Overriding local exclude pattern with:" - cat $localexclude - echo - echo - else - localexclude=$exclude - fi - - localmaksantallfulle=$maksantallfulle - # Sjekk om det skal være et annet antall fulle backuper av en boks - if [ -f $confdir/maksfulle.$computer ] ; then - localmaksantallfulle=`cat $confdir/maksfulle.$computer` - fi - - #tømmer variabelen for sikkerhets skyld - filesystems="" - - #Sjekker nest siste felt i fstab. Om det er 0 tar vi ikke backup - filesystems=`ssh -n $username@$computer "cat /etc/fstab" \ - | grep -v nfs \ - | grep -v "^#" \ - | grep -v "^$" \ - | awk '{ if ( $(NF-1) != "0" ) print $2}' ` - - #clean up our dir at this client - ssh $username@$computer "rm -r ~/.backup ; mkdir -m 700 ~/.backup" - - #try to copy $exclude to $computer - scp $localexclude $username@$computer:~/.backup/exclude || - echo "could not copy $localexclude to $computer :(" - - #try to copy preeexec and postexec if they exist - [ -f $confdir/preexec.$computer ] && - scp $confdir/preexec.$computer $username@$computer:~/.backup/preexec - [ -f $confdir/postexec.$computer ] && - scp $confdir/postexec.$computer $username@$computer:~/.backup/postexec - - #try to run preexec if it exist - ssh $username@$computer "[ -f ~/.backup/preexec ] && /bin/bash -x ~/.backup/preexec" - - for filesystem in $filesystems - do - #lager en variant uten tegnet "/" eller $ (gjelder NT) - sfilesystem=`echo $filesystem | tr '\/\$' '__'` - - #lager det som trengs av kataloger - mkdir -m 755 -p $storagedir/$computer/$sfilesystem/full 2>/dev/null - mkdir -m 755 -p $storagedir/$computer/$sfilesystem/daglig 2>/dev/null - - # - echo $filesystem > ${storagedir}/${computer}/.${sfilesystem}.name - chmod 644 ${storagedir}/${computer}/.${sfilesystem}.name - - #set default backuplevel - backuplevel=daglig - - if [ ! -f $storagedir/$computer/$sfilesystem/.date ] - then - #take the first full backup of this filesystem on this computer - backuplevel=full - echo $DATEs > $storagedir/$computer/$sfilesystem/.date - fi - - #sjekker om det er på tide med en full - if - [ -z "`find $storagedir/$computer/$sfilesystem/full/ -name \*tgz.gpg -mtime -$dagermellomfulle`" ] - then - backuplevel=full - fi - - #gå ned i rett katalog - cd $storagedir/$computer/$sfilesystem/$backuplevel || exit 1 - - #perform the actual backup - backup - - #delete complete backups - mf=$(($localmaksantallfulle+1)) - - for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz.gpg | tail -n +$mf` - do - prefix=`echo $full | sed "s/\.[^.]*$//"` - echo "$computer:$filesystem sletter full $prefix (for mange)" - rm $prefix* - done - - #delete incremental backups older than the oldest complete backup - oldf=`ls -t1 $storagedir/$computer/$sfilesystem/full/*tgz.gpg | tail -n 1` - find \ - $storagedir/$computer/$sfilesystem/daglig \ - -type f \ - \! -newer $oldf \ - -exec rm {} \; - #denne funker bare med gnu find - #-printf "$computer:$filesystem sletter daglig %f (for gammel)\n" \ - - done - - #try to run postexec if it exist - ssh $username@$computer "[ -f ~/.backup/postexec ] && /bin/bash -x ~/.backup/postexec" - -done - -#create report -#/home/backup/report.sh $storagedir > /tmp/report.txt -#[ ! -z $remotestat ] && scp /tmp/report.txt $remotestat -### - -# print diskusage to logfile -df -k - -date diff --git a/backup.sh b/backup.sh index 7a27318..c794322 100755 --- a/backup.sh +++ b/backup.sh @@ -79,12 +79,12 @@ backup() infomsg "$computer:$filesystem $backuplevel backup" localsnar="${storagedir}/${computer}/${sfilesystem}/.incremental.snar" - remotesnar="/root/.backup/${sfilesystem}.snar" + remotesnar="$remotehome/.backup/${sfilesystem}.snar" if [ "$backuplevel" = "daglig" ]; then # If incremental backup, we need to copy the incremental status to $computer. # If it does not exist, whine a bit and then run date-based instead. if [ -s "$localsnar" ]; then - if ! scp $localsnar root@$computer:$remotesnar; then + if ! scp $localsnar $username@$computer:$remotesnar; then diemsg "Could not copy .incremental.snar to $computer" fi lastcmd="--listed-incremental=$remotesnar --no-check-device" @@ -112,8 +112,8 @@ backup() # Pipe the stuff over ssh to ourselves, run pee to cat the contents to a # file and build a file list, respectivly. TARFILE=$DATE.tmp - TARCMD="ssh root@$computer \"nice -n 19 ionice -c3 tar --one-file-system --use-compress-program $compressor -cf - -C $filesystem . $lastcmd \ - --exclude-from=/root/.backup/exclude\" | pee \"cat > $TARFILE\" \"tar tzvf -\"" + TARCMD="ssh $username@$computer \"nice -n 19 ionice -c3 tar --one-file-system --use-compress-program $compressor -cf - -C $filesystem . $lastcmd \ + --exclude-from=$remotehome/.backup/exclude\" | pee \"cat > $TARFILE\" \"tar tzvf -\"" infomsg "Running $TARCMD" eval $TARCMD > $DATE.idx @@ -121,7 +121,7 @@ backup() # File is >0 in size and neither cat or tar tzvf failed; we assume it worked. if [ "$remotesnar" ]; then - if ! scp root@$computer:$remotesnar $localsnar.tmp; then + if ! scp $username@$computer:$remotesnar $localsnar.tmp; then diemsg "Could not copy .incremental.snar from $computer" fi fi @@ -140,7 +140,46 @@ backup() chmod 644 *idx 2>/dev/null #everyone can read # Let the remote computer know that we ran a successful backup (for nagios) - ssh root@$computer touch $filesystem/.lastbackup + ssh $username@$computer touch $filesystem/.lastbackup + else + # Something wrong happened. + rm $TARFILE + diemsg "tar failed or $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted" + fi +} + +# Non-privileged backups currently have a few limitations compared to regular ones: +# +# - No .idx files. +# - No .snar-style incremental backups, only date-based. +# - No touching of .lastbackup for Nagios. +nonprivbackup() +{ + infomsg "$computer:$filesystem $backuplevel non-privileged backup" + + if [ "$backuplevel" = "daglig" ]; then + lastd=`cat ../.date` + lastcmd="\"$lastd\"" + else + lastcmd="" + fi + + TARFILE=$DATE.tmp + TARCMD="ssh $username@$computer \"sudo /usr/local/sbin/output-encrypted-backup $filesystem $lastcmd\"" + infomsg "Running $TARCMD" + eval $TARCMD > $TARFILE + + if [ $? -lt 2 ] && [ -s $TARFILE ]; then + # File is >0 in size and the remote tar did not report any fatal errors; we assume it worked. + + # Move tar file in place + mv $TARFILE $DATE.tgz.gpg + + # Update timestamp + echo $DATEs > ../.date && touch -t $DATE ../.date + + # Fix permissions + chmod 600 *tgz.gpg #only for us else # Something wrong happened. rm $TARFILE @@ -158,16 +197,29 @@ targetfilesystem=`df -P "$storagedir/$computer"` if [ "$rootfilesystem" == "$targetfilesystem" ]; then diemsg "Target filesystem ($storagedir/$computer) was mounted on /." fi + +# Find the user to do the backup as. +if [ -f $confdir/username.$computer ] ; then + username=`cat $confdir/username.$computer` +else + username=root +fi infomsg "Backing up $computer" # Try to SSH to the computer without entering a password. -if ! ssh -n -o NumberOfPasswordPrompts=0 root@$computer /bin/true; then +if ! ssh -n -o NumberOfPasswordPrompts=0 $username@$computer /bin/true; then diemsg "Could not use passwordless SSH." fi +# Find the home directory of the backup user +remotehome=`ssh $username@$computer "echo ~"` +if [ -z "$remotehome" ]; then + diemsg "Could not expand ~ for user $username" +fi + # Check if pigz is available -if ssh -n root@$computer "pigz -V 2>/dev/null"; then +if ssh -n $username@$computer "pigz -V 2>/dev/null"; then compressor=pigz else infomsg "pigz missing; falling back to gzip." @@ -175,37 +227,37 @@ else fi # Check dump bit in fstab -filesystems=`ssh -n root@$computer "cat /etc/fstab" \ +filesystems=`ssh -n $username@$computer "cat /etc/fstab" \ | grep -v nfs \ | grep -v "^#" \ | grep -v "^$" \ | awk '{ if ( $(NF-1) != "0" ) print $2}' ` # Clean up our dir at this client -if ! ssh root@$computer "rm -r /root/.backup ; mkdir -m 700 /root/.backup"; then - diemsg "Could not create backup staging area at $computer:/root/.backup" +if ! ssh $username@$computer "rm -r $remotehome/.backup ; mkdir -m 700 $remotehome/.backup"; then + diemsg "Could not create backup staging area at $computer:$remotehome/.backup" fi # Try to copy $exclude to $computer -if ! scp $exclude root@$computer:/root/.backup/exclude > /dev/null; then +if ! scp $exclude $username@$computer:$remotehome/.backup/exclude > /dev/null; then diemsg "Could not copy exclude.txt to $computer" fi # Try to copy preeexec and postexec if they exist if [ -f $confdir/preexec.$computer ]; then - if ! scp $confdir/preexec.$computer root@$computer:/root/.backup/preexec >&2; then - diemsg "Could not copy preexec.$computer to $computer:/root/.backup/preexec" + if ! scp $confdir/preexec.$computer $username@$computer:$remotehome/.backup/preexec >&2; then + diemsg "Could not copy preexec.$computer to $computer:$remotehome/.backup/preexec" fi fi if [ -f $confdir/postexec.$computer ]; then - if ! scp $confdir/postexec.$computer root@$computer:/root/.backup/postexec >&2; then - diemsg "Could not copy postexec.$computer to $computer:/root/.backup/postexec" + if ! scp $confdir/postexec.$computer $username@$computer:$remotehome/.backup/postexec >&2; then + diemsg "Could not copy postexec.$computer to $computer:$remotehome/.backup/postexec" fi fi # Try to run preexec if it exists -if ! ssh root@$computer "[ ! -f /root/.backup/preexec ] || /bin/bash -x /root/.backup/preexec" >&2; then - diemsg "Could not run $computer:/root/.backup/preexec" +if ! ssh $username@$computer "[ ! -f $remotehome/.backup/preexec ] || /bin/bash -x $remotehome/.backup/preexec" >&2; then + diemsg "Could not run $computer:$remotehome/.backup/preexec" fi for filesystem in $filesystems; do @@ -239,7 +291,7 @@ for filesystem in $filesystems; do fullbackup_min_for_this_machine=$daysbetweenfull fi - if [ -z "`find $storagedir/$computer/$sfilesystem/full/ -name \*tgz -mtime -$fullbackup_min_for_this_machine`" ]; then + if [ -z "`find $storagedir/$computer/$sfilesystem/full/ -name \*tgz\* -mtime -$fullbackup_min_for_this_machine`" ]; then backuplevel=full fi @@ -247,7 +299,11 @@ for filesystem in $filesystems; do cd $storagedir/$computer/$sfilesystem/$backuplevel || diemsg "$storagedir/$computer/$sfilesystem/$backuplevel does not exist" # Perform the actual backup - backup + if [ "$username" = "root" ]; then + backup + else + nonprivbackup + fi # Check if this box has a custom number of full backups if [ -f $confdir/maksfulle.$computer.$sfilesystem ]; then @@ -259,14 +315,14 @@ for filesystem in $filesystems; do fi # Delete old full backups - for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz | tail -n +$mf`; do + for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz* | tail -n +$mf`; do prefix=`echo $full | sed "s/\.[^.]*$//"` infomsg "$computer:$filesystem Deleting full backup $prefix" rm $prefix* done # Delete incremental backups older than the oldest full backup - oldf=`ls -t1 $storagedir/$computer/$sfilesystem/full/*tgz | tail -1` + oldf=`ls -t1 $storagedir/$computer/$sfilesystem/full/*tgz* | tail -1` find \ $storagedir/$computer/$sfilesystem/daglig \ -type f \ @@ -276,8 +332,8 @@ for filesystem in $filesystems; do done # Try to run postexec if it exist -if ! ssh root@$computer "[ ! -f /root/.backup/postexec ] || /bin/bash -x /root/.backup/postexec" >&2; then - diemsg "Could not run $computer:/root/.backup/postexec" +if ! ssh $username@$computer "[ ! -f $remotehome/.backup/postexec ] || /bin/bash -x $remotehome/.backup/postexec" >&2; then + diemsg "Could not run $computer:$remotehome/.backup/postexec" fi # Use warn so that we will get an email even on success. -- 2.39.2