Skip to content

Instantly share code, notes, and snippets.

@maurelio79
Last active December 22, 2015 08:38
Show Gist options
  • Save maurelio79/6445875 to your computer and use it in GitHub Desktop.
Save maurelio79/6445875 to your computer and use it in GitHub Desktop.
A simple bash script to backup important directory in your Linux client/server
################################################
#
# Configuration file for script sys_backup.sh
#
# ATTENTION!!! ATTENTION!!! ATTENTION!!!
# Do not edit anything before double dot!
# Double dot included!
#
################################################
# Insert directory to backup, separeted by a space, with absolute path and without slash at the end.
Directory to backup : /etc /var/www
# Insert destination backup folder with absolute path and without slash at the end.
Destination folder : /app/backup
# Insert file name for log with absolute path.
Log file : /var/log/sys_backup.log
# Define if you want archive old backup (.bak).
# Possible options are: yes | no
Archive .bak : yes
#!/bin/bash
####################################################################
#
# Script to weekly backup important system folder.
#
# Author: Mauro Lomartire
# Script Name: sys_backup
# Script Version: 1.0 BETA
# License: GPLv2 http://www.gnu.org/licenses/licenses.en.html
#
####################################################################
# Define configuration file
CONFIG=/etc/sys_backup.conf
if [ ! -f $CONFIG ]; then
echo "Configuration file not found"
exit 1
fi
# Define variables needed for script
SOURCE=`cat $CONFIG | grep "Directory to backup" | cut -d: -f2`
DESTDIR=`cat $CONFIG | grep "Destination folder" | cut -d: -f2`
DAYS=`cat $CONFIG | grep "Days" | cut -d: -f2`
LOG=`cat $CONFIG | grep "Log file" | cut -d: -f2`
# ARCHIVE=`cat $CONFIG | grep "Archive .bak" | cut -d: -f2`
if [[ ! $SOURCE || ! $DESTDIR || ! $DAYS || ! $LOG ]]; then
echo "Some element in configuration file are not present."
exit 1
fi
# Declare array with directory to backup
declare -a dirtobak=($SOURCE);
# Remove backup older than 3 days
clean() {
find $DESTDIR/ -type f -mtime +$DAYS | xargs rm -f -- 2>> $LOG
}
# Move all backupped diretctory in a single archive to keep for 3 days
tarbak(){
ls $DESTDIR/*.tar 2>> $LOG
if [ $? -eq 0 ]; then
tar cf $DESTDIR/archive`date +%d%m%y`.tar.bz2 $DESTDIR/*.tar 2>> $LOG
# Need to fix here: a check for tar
rm -f $DESTDIR/*.tar 2>> $LOG
fi
############################################################
# for k in `ls $DESTDIR/*.tar`; do
# NEWNAME=$k.bak
# mv $k $NEWNAME
# if [ $? -gt 0 ]; then
# echo "Error during renaming of $k" >> $LOG
# fi
# done
# fi
#
# if [ $ARCHIVE == "yes" ]; then
# ls $DESTDIR/*.bak 2>> $LOG
# if [ $? -eq 0 ]; then
# tar cf $DESTDIR/archive`date +%d%m%y`.tar $DESTDIR/*.bak 2>> $LOG
# if [ $? -gt 0 ]; then
# echo "Error creating archive.tar" >> $LOG
# fi
# fi
####################################################################
}
# Start backup of directory defined in configuration file
backup(){
for i in ${dirtobak[@]}; do
tar cf $DESTDIR/`basename $i`-`date +%H%M%S`.tar $i 2>> $LOG
if [ $? -gt 0 ]; then
echo "Error during tar of $i" >> $LOG
fi
done
}
echo "----------------------------------------------------------" >> $LOG
echo `date` >> $LOG
echo "----------------------------------------------------------" >> $LOG
# Launch all function
clean
tarbak
backup
#!/bin/bash
####################################################################
#
# GUI Script to configure sys_backup script.
#
# Author: Mauro Lomartire
# Script Name: sys_backup_config
# Script Version: 1.0 BETA
# License: GPLv2 http://www.gnu.org/licenses/licenses.en.html
#
####################################################################
####################################################################
#
# DESCRIPTION
# This script help you to create the configuration file
# for the sys_backup script.
#
####################################################################
#############################################
# TODO
# - Check folder if exist
# - Check correct execution of function
# - Check id $DAYS is a number
# - Verify ctime and mtime
#############################################
##########################################
#
# Define variables and function
#
##########################################
CONFIG="/etc/sys_backup.conf"
FOLDER_LOG="/var/log"
# If config file already exist, we delete it.
function del_files(){
if [ -f $CONFIG ]; then
rm -f $CONFIG
fi
# If log file already exist, we delete it.
if [ -f $FOLDER_LOG/$LOG ]; then
rm -f $FOLDER_LOG/$LOG
fi
}
# Write down the configuration file
function create_conf(){
echo "Directory to backup : $SOURCE" >> $CONFIG
echo "Destination folder : $DESTDIR" >> $CONFIG
echo "Days : $DAYS" >> $CONFIG
echo "Log file : $LOG" >> $CONFIG
}
# Write in log file.
function write_log(){
# echo "Configuration file correctly created as $CONFIG"
# echo "Now you can run sys_backup script located in /usr/local/bin"
echo "----------------------------------------------------------" >> $FOLDER_LOG/$LOG
echo `date` >> $FOLDER_LOG/$LOG
echo "Configuration file created." >> $FOLDER_LOG/$LOG
echo "----------------------------------------------------------" >> $FOLDER_LOG/$LOG
}
##########################################
#
# Starting the script
#
##########################################
# Check if we are in a window system or not; if not execute the text version
if [ ! $DISPLAY ]; then
# Verify if user is root, if not he can not configure script.
user=`whoami`
if [ $user != "root" ]; then
echo "You need to be root in order to configure sys_backup script"
exit 1
fi
echo "Enter source folders to backup separeted by space and press [enter]:"
read SOURCE
while [[ ! $SOURCE || ! -d $SOURCE ]]
do
echo "Error! Is this a real folder? Enter source folders to backup separeted by space and press [enter]:"
read SOURCE
done
echo "Enter destination folder of backup and press [enter]:"
read DESTDIR
while [[ ! $DESTDIR || ! -d $DEST ]]
do
echo "Error! Is this a real folder? Enter destination folder of backup and press [enter]:"
read DESTDIR
done
echo "Enter number of days to keep backup and press [enter]:"
read DAYS
while [ ! $DAYS ]
do
echo "Enter number of days to keep backup and press [enter]:"
read DAYS
done
echo "Enter log file name (it will create in /var/log/) and press [enter]:"
read LOG
while [ ! $LOG ]
do
echo "Enter position and log file name and press [enter]:"
read LOG
done
del_files
create_conf
write_log
exit 0
fi
# If we are here it means that we are in window X system, so zenity version of script will be used.
# Verify if user is root, if not he can not configure script.
user=`whoami`
if [ $user != "root" ]; then
zenity --error --title="You are not root" --text="You need to be root in order to configure sys_backup script"
exit 1
fi
# Display zenity window
PARAMETERS=`zenity --title="System Backup Configurator" --forms --text="All files/folders need to be in absolute path format and without / at the end;\n source folder can be more than 1 but they need to be sperated by space.\n" --add-entry="Source folders" --add-entry="Destination folder" --add-entry="Days to keep old backup" --add-entry="Log File" --separator=:`
# Extract variables needed in config file
SOURCE=`echo $PARAMETERS | cut -d: -f1 `
DESTDIR=`echo $PARAMETERS | cut -d: -f2`
DAYS=`echo $PARAMETERS | cut -d: -f3`
LOG=`echo $PARAMETERS | cut -d: -f4`
# If all variables are not set, display error
if [[ ! $SOURCE || ! $DESTDIR || ! $DAYS || ! $LOG || ! $CONFIG ]]; then
# echo "Please fill all fields!"
zenity --error --title="Config dialog Error" --text="Please fill all fields! They are necessary to correctly run the script."
exit 1
fi
del_files
create_conf
write_log
zenity --info --title="Well Done!" --text="Configuration file correctly created as $CONFIG\nNow you can run sys_backup script located in /usr/local/bin"
##########################################
#
# End of script
#
##########################################
@maurelio79
Copy link
Author

With this script and his configuration file you can quickly backup important dir on your linux.

Usage
Just modify sys_backup.conf as you need and run the script.

@maurelio79
Copy link
Author

Added a minimal zenity GUI (sys_backup_config.sh) to create the configuration file.

13/10/13
Added a check to see if we are in X window system or not.
In the first case the script will show a zenity GUI.
In the second case the script will show a shell based menu.

Usage
Run sys_backup_config.sh and fill all field. A configuration file named sys_backup.conf will be create inside /etc
After that you can run the script sys_backup.sh
IMPORTANT!
Every path has to be written without / at the end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment