Backup

From Unix Wiki
Jump to: navigation, search

HP Data Protector

rsync backup script

#!/bin/bash

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

hostName=`hostname -s`
dirName="${hostName}-`date +%d-%m-%Y`"
sourceList="/list /of /backup/dirs"
dest="/path/to/destination"
keepDays="2"

if [ ! -d "${dest}" ]; then
    echo "Destination ${dest} does not exist."
    exit 1
fi

for i in ${sourceList} ; do
    if [ ! -d "${i}" ]; then
        echo "${i} does not exist."
        exit 1
    fi
done

mkdir "${dest}/${dirName}"

if [ ! -d ${dest}/${dirName} ]; then
    echo "Cannot create ${dest}/${dirName}, volume not writable."
    exit 1
fi

rsync -avR --stats ${sourceList} "${dest}/${dirName}"

find "${dest}" -maxdepth 1 -type d -name "${hostName}-*" \
    -mtime +"${keepDays}" -exec rm -rf '{}' \;

exit 0

tar backup script

#!/bin/bash

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

hostName=`hostname -s`
dirName="${hostName}-`date +%d-%m-%Y`"
sourceList="/list /of /backup/dirs"
dest="/path/to/destination"
keepDays="2"

if [ ! -d "${dest}" ]; then
    echo "Destination ${dest} does not exist."
    exit 1
fi

for i in ${sourceList} ; do
    if [ ! -d "${i}" ]; then
        echo "${i} does not exist."
        exit 1
    fi
done

mkdir "${dest}/${dirName}"

if [ ! -d ${dest}/${dirName} ]; then
    echo "Cannot create ${dest}/${dirName}, volume not writable."
    exit 1
fi

tar -cpzf "${dest}/${dirName}/${dirName}.tar.gz" ${sourceList}

find "${dest}" -maxdepth 1 -type d -name "${hostName}-*" \
    -mtime +"${keepDays}" -exec rm -rf '{}' \;

exit 0

tar backup script + encryption

#!/bin/bash

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

hostName=`hostname -s`
dirName="${hostName}-`date +%d-%m-%Y`"
sourceList="/list /of /backup/dirs"
dest="/path/to/destination"
keepDays="2"
gpgPassPhrase="SUPERDUPERSECRETPASSWORD"

if [ ! -d "${dest}" ]; then
    echo "Destination ${dest} does not exist."
    exit 1
fi

for i in ${sourceList} ; do
    if [ ! -d "${i}" ]; then
        echo "${i} does not exist."
        exit 1
    fi
done

mkdir "${dest}/${dirName}"

if [ ! -d ${dest}/${dirName} ]; then
    echo "Cannot create ${dest}/${dirName}, volume not writable."
    exit 1
fi

tar -cvzp ${sourceList} | /usr/bin/gpg -c --verbose --cipher-algo AES256 \
    --batch --yes --passphrase ${gpgPassPhrase} -o "${dest}/${dirName}/${dirName}.tar.gz"

find "${dest}" -maxdepth 1 -type d -name "${hostName}-*" \
    -mtime +"${keepDays}" -exec rm -rf '{}' \;

exit 0