Scripting: How I handle local backups

#1

I thought it would be a neat idea to see what others are doing with scripting the mundane items, possible backup routines and just general items that may be of interest to anyone.

Of course, when it comes to my creations, I am always open to new ways to script what I present in the current text or even converted into something like C, perl, python etc.

That being said, here is what I do for backups that run every 12 hours. The tarballs or shuffled off to a pair of LUKS drives.

#!/bin/bash
#daily-archive.sh

#Determine the OS being used
os=`hostnamectl |grep "Operating System:" |awk '{print $3" "$4" "$5}'`

#Debian Testing
#ver="_Buster"

#Debian Testing
#ver="_Sid"

#Arch Labs
ver="Labs"

#Arch Linux
#ver="Linux"

#Debian x
#ver=`hostnamectl |grep "Operating System:" |awk '{print "_"$5}'`

#Ubuntu xx.xx
#ver=`hostnamectl |grep "Operating System:" |awk '{print "_"$4}'`

distname=`hostnamectl |grep "Operating System:" |awk '{print $3}'`
destdir=$distname$ver

#Universal parameters - these remain uncommented
bd1=/bin
bd2=/sbin
bd3=/usr/bin
bd4=/usr/sbin
day=$(date +%F_%H%M%S)
hostname=$(hostname -s)
archive_file="$distname$ver"-"$hostname"_"$day.tgz.gpg"
backup_files="/root /etc /opt /home/chris/"
xclude="--exclude="/root/archlive" --exclude="/home/chris/.cache" --exclude="/home/chris/Music" --exclude="/home/chris/Documents" --exclude="/home/chris/Downloads" --exclude="/home/chris/.local/share/Trash""
ziggy="/root/arc_key"
rdest="/mnt/Work/Rebuild/"
dest="/mnt/320a/"

#Mount network share.
$bd1/mount $dest
$bd3/rm $rdest/*.gpg

#Print start status message.
echo "Creating tarball from $hostname ($os) of $backup_files to $destdir $archive_file"
date
echo

#Create the tarball.
$bd1/tar $xclude -czvpf - $backup_files | gpg --symmetric --cipher-algo aes256 --batch --passphrase-file $ziggy -o $rdest/$archive_file
$bd3/cp $rdest/*.gpg $dest/$destdir

#Print end status message.
echo
echo "Backup finished"
date

#Long listing of files in $dest to display file sizes.
ls -lh $dest$destdir
2 Likes

My backup script for dotfiles
#2

You exclude your Documents? That handled by a separate backup? Script reads like a system backup.

Something simple to use rsync to back up my configs:

!/bin/sh

# rsync backup utility script
#

#config
OPT="-arPh"
SRC="/home/doug/"
DEST="/mnt/willow/backups/"
NAME="BANDIT-01"
SPACE="."
DATE=`date "+%F_%s"`

# rsync to backup
rsync $OPT --exclude-from='/home/doug/.rexcludes.txt' $SRC ${DEST}$NAME$SPACE$DATE

WILLOW-01 is hostname of my ArchLabs install, so the backup is stored there.

2 Likes

#3

Good question: I have a Debian server. So my “H” drive locally is Documents but is actually on the Debian Server. The Deb server has its own backup routines so no need to backup the shares (and that includes the Music directory too).

But yes, it is a local system backup (more or less) :slight_smile:

2 Likes

#4

I tried to adapt a script for an exercise, for a test web server and extracted my information in a usb. So the directories are compressed in a file. In my file ‘directories_back’ I write previously the routes (in each line) that I want to copy like this:

#directorios_back CONFIG_FILE

/srv/http/
/srv/ftp/

And the script for the copies is: (There are parts in spanish, but I think it is understood. (And sorry if it’s not a very orthodox code)

#!/bin/bash
#copias_script
#SCRIPT para hacer backups comprimidas de directorios
#PREVIAMENTE crear directorios_back

DATE=$(date +%y%m%d)
FILE=copia$DATE.tar.gz                 #nombra a tu archivo resultante
CONFIG_FILE=/dev/sdc/directorios_back
DESTINATION=/dev/sdc/$FILE
if [ -f $CONFIG_FILE ]
then
    echo
else
    echo
    echo "$CONFIG_FILE no existe"
    echo "La copia no se completó porque no se encuentra el archivo de configuración"
    echo
    exit
fi
FILE_NO=1
exec < $CONFIG_FILE
read FILE_NAME  
while [ $? -eq 0 ]
do
    if [ -f $FILE_NAME -o -d $FILE_NAME ]
    then
        FILE_LIST="$FILE_LIST $FILE_NAME" #Añade el nombre a la lista
    else
        echo
        echo "$FILE_NAME, no existe."
        echo "Obviamente no crearé el archivo"
        echo "Lo he listado en  $FILE_NO del archivo de configuración"
        echo "Continuando la construcción..."
        echo
    fi
    FILE_NO=$[$FILE_NO + 1]
    read FILE_NAME
done
echo " Comprimiendo archivo..."
echo
tar -czf $DESTINATION $FILE_LIST 2> /dev/null #A comprimiiiiiir
echo " Archivo completado"
echo " La ruta es: $DESTINATION"
exit

4 Likes

#5

Great work there @Chris

3 Likes