Backing up files in Linux is possible in multiple different ways but in my case…

…I am currently trying out different backup strategies with rsync and zip

rsync because of running servers, with a temporary directory

zip because I need acces to files in Windows (in the worst case at least)

And I need some database dumps in there as well!

Let’s go then

First of all I define the variables I need like folder names and backup dates for files and folders

#!/bin/bash
# files
## dump date
BACKUP_DATE=$(date +"%d%m%Y.%H%M") # 130032018.2301
BACKUP_DIR_DATE=$(date +"%d%m%Y") # 13032018

BACKUP_DIR="/home/user/backups"
TMP_DIR="$BACKUP_DIR/tmp"

DAILY_BACKUP_DIR="$BACKUP_DIR/$BACKUP_DIR_DATE/"
DBDUMPS_DIR="$TMP_DIR/dbDumps"
CONFIGS_DIR="$TMP_DIR/etc"
HOMES_DIR="$TMP_DIR/homes"

MYSQL_ROOT_PW="PLAIN_TEXT_PASSWORD"

…and a general function for folder creation, if these do not exist

## create a directory with absolute location $1 
createDirectory () {
if [ ! -d $1 ]; then
  mkdir $1
fi
return 0
}

Then I am going to rsync

…files inside predefined folders into a tmp directory

rsync ing a directory works similar to cp

## rsync all directories inside /var/www (websites)
rsyncWebsites () {
for directory in /var/www/;
do
  rsync -rtq $directory $TMP_DIR;
done
return 0
}

rsyncConfigs () {
CONFIG_DIRS=( "/etc/nginx" "/etc/ssh" "/etc/letsencrypt" )
for dir in ${CONFIG_DIRS[@]}; do
  rsync -rtq $dir $CONFIGS_DIR;
done
return 0
}

rsyncHomes () {
HOME_DIRS=( "/home/ghost" )
for dir in ${HOME_DIRS[@]}; do
  rsync -rtq $dir $HOMES_DIR;
done
return 0
}

-r - recursive - not only files but everything

-q - quiet - no output

-t - times - reserve modification times

And I also need to dump those two databases (in my setup)

dumpDatabases () {
  mysqldump -u root --password=$MYSQL_ROOT_PW db1 > $DBDUMPS_DIR/db1.$BACKUP_DATE.sql
  mysqldump -u root --password=$MYSQL_ROOT_PW db2 > $DBDUMPS_DIR/db2.$BACKUP_DATE.sql
return 0
}

while I could simply mysqldump -u root --password=$MYSQL_ROOT_PW --all-databases > output.sql but as said…only these two are need.

And zip all folders as multiple archives (containing the folder name) from inside tmp out into backups folder - also recursively and quietly

## and zip these dirs
zipBackups () {
  for dir in $(ls $TMP_DIR); do
    ZIPNAME=$DAILY_BACKUP_DIR${dir}.$BACKUP_DATE
    zip -rq $ZIPNAME.zip $TMP_DIR/$dir;
  done
return 0
}

And while no function has been run yet…I still need to run them! How unfortunate:

# kinda the main function
## create directories !ifexists
createDirectory $BACKUP_DIR
createDirectory $TMP_DIR
createDirectory $DAILY_BACKUP_DIR
createDirectory $DBDUMPS_DIR
createDirectory $CONFIGS_DIR
createDirectory $HOMES_DIR

## backup files and dump databases
rsyncWebsites
rsyncConfigs
rsyncHomes
dumpDatabases

## zip synced filed
zipBackups

## remove temporary directory
rm -r $TMP_DIR

## done
exit 0

Further steps and TODOs

add functionality to keep newer files and delete older ones

maybe some standard times as well

  • one per month
  • one per week

remove 3 rsync functions one general function