Script Shell de backup de dossier
Voici un script shell qui permet de sauvegarder un certain nombre de répertoires dans un autre répertoire, en archivé, chiffré et daté.
Vous n'avez qu'à paramétrer :
- Une liste de répertoire séparés par des espaces
- Le dossier de destination
- La passphrase pour GPG
- Le fichier ou logguer les actions (default: /var/log/backup.log)
- Le nombre de jours où l'on garde les archives (defaut: 5)
- L'UID de l'utilisateur responsable des archives (défaut: root)
- Le GID de l'utilisateur responsable des archives (défaut: root)
- Le chmod pour l'accès au répertoire (défaut: 400)
Voici le code :
#!/bin/bash
#
# Shell script (BASH) used in cron to made a backup of the given directory in $1
# to the directory in $2
# In order to run this script, you must have following tools installed:
# - tar
# - gpg
# - rm
# - chown
# - chmod
#
#
# Installation
# Customize the script according to your need. You need to setup :
# - A list of folders, separated by space, and finished with the last /
# - The folder where the backup goes
# - A GPG passphrase
# - The file where to log the messages (default /var/log/backup.log)
# - The number of days the backups are stored (default 5)
# - The owner uid (default root)
# - The owner gid (default root)
# - The owner rights (default 400)
#
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 ReFlectiv project.
# Feedback/comment/suggestions : http://www.reflectiv.net/
# -------------------------------------------------------------------------
# List of folders to save
LIST_F0LDERS=`/var/www/site1/ /var/www/site2/ /var/www/site3/`;
# Folder where the backups goes
BACKUP_FOLDER="/root/snapshoots/";
# The password for the archive
GPG_PASS="passphrase";
# File name where to log the messages
LOG_FILE="/var/log/backup.log";
# Number of days the archives are keeped
KEEPING_DAYS=5;
# UID & GID of the archived file
BU_UID="root";
BU_GID="root";
# Access rights to the archived file
BU_CHMOD=400;
# Only change if your UNIX stores bin in diffrent location
TAR="/bin/tar";
GPG="/usr/bin/gpg";
RM="/bin/rm";
CHOWN="/bin/chown";
CHMOD="/bin/chmod";
MKDIR="/bin/mkdir";
#######################################################################
# Do not change anything below
#######################################################################
# We mades the vars
UNIX_DATE=`date +"%Y_%m_%d"`;
CURRENT_DATE=`date +"%d/%m/%Y - %H:%M:%S"`;
TGZ_FILE=$UNIX_DATE".tgz";
GPG_FILE=$TGZ_FILE".gpg";
# Function called to log info into the file
function log {
echo "["$CURRENT_DATE"] - "$1 >> $LOG_FILE;
if [ ! $2 == "" ]; then
exit 1;
fi
}
log "Starting backup";
# Preliminary tests
[ -x $TAR ] || log "tar not find" 1;
[ -x $GPG ] || log "gpg not find" 1;
[ -x $RM ] || log "rm not find" 1;
[ -x $CHOWN ] || log "chown not find" 1;
[ -x $CHMOD ] || log "chmod not find" 1;
[ -x $MKDIR ] || log "mkdir not find" 1;
# We test if the file is correctly called
#[ $# -eq 2 ] || log "Invalid number of parameters" 1;
# We test if the destination folder exists
[ -d $BACKUP_FOLDER ] ||
log "The backup destination folder "$BACKUP_FOLDER" seems to not exists." 1;
for FOLDER in $LIST_F0LDERS
do
# We test if the original folder exists
[ -d $FOLDER ] || log "The folder "$FOLDER" does not exists !" 1;
# We mades sample vars
BACKUP_SUBFOLDER=`basename $FOLDER`;
if [ ! -d $BACKUP_FOLDER$BACKUP_SUBFOLDER"/" ]; then
# We create the folder
$MKDIR $BACKUP_FOLDER$BACKUP_SUBFOLDER"/";
# We set the same rights and acces as the archived/encrypted file
$CHOWN $BU_UID:$BU_GID $BACKUP_FOLDER$BACKUP_SUBFOLDER"/";
$CHMOD $BU_CHMOD $BACKUP_FOLDER$BACKUP_SUBFOLDER"/";
fi
# We goes to the backup folder
cd $FOLDER;
# We make the archive
$TAR -czf $BACKUP_FOLDER$BACKUP_SUBFOLDER"/"$TGZ_FILE .
# If an error occured ...
[ $? -eq 0 ] || log "An error occured with the command tar" 1;
# We goes to the backup directory
cd $BACKUP_FOLDER$BACKUP_SUBFOLDER;
# We encrypt the archive
$GPG --yes -c --passphrase $GPG_PASS --s2k-cipher-algo RIJNDAEL256 $TGZ_FILE;
# If an error occured ...
[ $? -eq 0 ] || log "An error occured with the command gpg" 1;
# We delete the archive
$RM $TGZ_FILE;
# If an error occured ...
[ $? -eq 0 ] || log "An error occured with the command rm" 1;
# We modify the user/groups for the file
$CHOWN $BU_UID:$BU_GID $GPG_FILE;
# If an error occured ...
[ $? -eq 0 ] || log "An error occured with the command rm" 1;
# We modify the chmod for the file
$CHMOD $BU_CHMOD $GPG_FILE;
# If an error occured ...
[ $? -eq 0 ] || log "An error occured with the command rm" 1;
OLD_ARCHIVE=`date --date $KEEPING_DAYS' days ago' "+%Y_%m_%d"`".tgz.gpg";
if [ -f $OLD_ARCHIVE ]; then
log "Deleting old archive "$OLD_ARCHIVE;
rm $OLD_ARCHIVE;
fi
log $FOLDER" folder saved";
done
log "Backup finished successfully";
exit 0;