View previous topic :: View next topic |
Author |
Message |
earcar Guru
Joined: 20 Sep 2004 Posts: 425 Location: Bologna, Italy
|
Posted: Wed Feb 08, 2006 9:25 pm Post subject: [TOOL]zeppola v0.3, ovvero come ti backuppo un ftp da locale |
|
|
Zeppola è uno script per backuppare uno o più server ftp, utile in caso vogliate un backup dei file del vostro sito
L'unica dipendenza che ha è ncftp.
Che fa?
Si connette al server ftp, si sposta nella directory che volete e ne scarica tutto il contenuto, sottodir comprese
e lo tarbzippa (o targzippa se volete) in un file a scelta in una dir a scelta
Supporta backup multiplo tramite profili.
Eccolo qua:
Code: |
#!/bin/bash
#
# Zeppola ftp backupper version 0.3
#
# Made by Carmine 'earcar' Paolino <earcar at gmail dot com>
#
#####################
PROFILES_DIR="/etc/zeppola"
#####################
DATE=`date +%Y%m%d`
PARAM=$1
PATTERN_FILE="$HOME/.pattern"
PATTERN="FTP_HOST\nFTP_PORT\nFTP_USER\nFTP_PASS\nFTP_BDIR\nBACKUP_FILE\nBACKUP_FORMAT\nBACKUP_DIR"
function dir_create {
if [ -d $BACKUP_DIR ]
then einfo "Backup directory ${BACKUP_DIR} already exists"
else
ewarn "Backup directory ${BACKUP_DIR} not exists, creating..."
if mkdir -p ${BACKUP_DIR}
then einfo "Directory ${BACKUP_DIR} successfully created"
else
eerror "Error while creating backup directory ${BACKUP_DIR}"
exit 1
fi
fi
}
function ftp_retrieve {
[ -d ${BACKUP_DIR}/tmp ] || mkdir ${BACKUP_DIR}/tmp
ebegin "Connecting to ${FTP_HOST} and transferring files"
cd ${BACKUP_DIR}/tmp
echo "get -R *" | if ncftp -u ${FTP_USER} -p ${FTP_PASS} -P ${FTP_PORT} ftp://${FTP_HOST}/${FTP_BDIR}
then einfo "Data transferred successfully"
else
eerror "Error while trasferring files"
exit 1
fi
}
function tar_data {
cd ${BACKUP_DIR}/tmp
if [ $BACKUP_FORMAT == "tar.gz" ]
then
ebegin "TarGzipping data"
tar zcf ${BACKUP_DIR}/${BACKUP_FILE}.tar.gz * 2> /dev/null
elif [ $BACKUP_FORMAT == "tar.bz2" ]
then
ebegin "TarBzipping data"
tar jcf ${BACKUP_DIR}/${BACKUP_FILE}.tar.bz2 * 2> /dev/null
else
ewarn "Wrong BACKUP_FORMAT specified: \"${BACKUP_FORMAT}\". Using default \"tar.bz2\""
ebegin "TarBzipping data"
tar jcf ${BACKUP_DIR}/${BACKUP_FILE}.tar.bz2 * 2> /dev/null
fi
cd ..
ebegin "Removing temporary files"
rm -rf ${BACKUP_DIR}/tmp
einfo "All Done!"
echo
}
function backup_all {
profdir_checks
for conf in ${PROFILES_DIR}/*
do
einfo "Using backup profile \"$conf\""
source $conf
do_backup || exit 1
done
}
function backup_single {
echo -e "${PATTERN}" > ${PATTERN_FILE}
if [ -f "${PROFILES_DIR}/${PARAM}" ]
then
if grep -f ${PATTERN_FILE} ${PROFILES_DIR}/${PARAM} &> /dev/null
then
rm -f ${PATTERN_FILE}
einfo "Using backup profile \"${PARAM}\""
source ${PROFILES_DIR}/${PARAM}
do_backup
else
rm -f ${PATTERN_FILE}
eerror "Profile \"${PARAM}\" has invalid syntax"
exit 1
fi
else
rm -f ${PATTERN_FILE}
eerror "Profile \"${PARAM}\" not found in \"${PROFILES_DIR}\""
exit 1
fi
}
function do_backup {
dir_create && ftp_retrieve && tar_data
}
function get_params {
if [ "$PARAM" == "all" ]
then backup_all
elif [ -n "$PARAM" ]
then backup_single
else
eerror "No parameter specified!\n\nUsage:\n\nzeppola all backups all profiles specified in \"${PROFILES_DIR}\" \nzeppola profile_name backups only specified profile"
exit 1
fi
}
function profdir_checks {
echo -e ${PATTERN} > ${PATTERN_FILE}
if [ -d "${PROFILES_DIR}" ] && [ -r "${PROFILES_DIR}" ]
then
for conf in ${PROFILES_DIR}/*
do
if grep -f ${PATTERN_FILE} $conf &> /dev/null
then break
else eerror "Profile \"$conf\" has invalid syntax"
fi
rm -f ${PATTERN_FILE}
exit 1
done
else
eerror "Profile directory \"${PROFILES_DIR}\" not exists or it\'s not readable"
rm -f ${PATTERN_FILE}
exit 1
fi
}
if [ -f "/sbin/functions.sh" ]
then source /sbin/functions.sh
else
echo "Error:"
echo "This script is designed for Gentoo GNU/Linux, if you're interested to run in another distribution contact me!"
echo
exit 1
fi
if which ncftp &> /dev/null
then get_params
else
eerror "NcFTP not found, emerge it first!"
exit 1
fi
|
Il codice fa schifo e sarà di sicuro un covo di bacarozzi, pertanto qualsiasi consiglio, correzione, feedback, commento o insulto è bene accetto
Changelog:
20060211 versione 0.3: +verifiche sulla cartella e sui file dei profili (syntax checking)
20060210 versione 0.2: possibilità di backuppare più server tramite configurazioni multiple
20060209 versione 0.1.1: pulito il codice da un paio di istruzioni che usavo per il debug , possibilità di scegliere il formato del backup, aggiunto controllo per ncftp, qualche bugfix
Ciauz! _________________ Bip... bip... bip... -- Sputnik 1
Last edited by earcar on Mon Feb 13, 2006 1:42 pm; edited 7 times in total |
|
Back to top |
|
|
earcar Guru
Joined: 20 Sep 2004 Posts: 425 Location: Bologna, Italy
|
Posted: Fri Feb 10, 2006 2:57 pm Post subject: |
|
|
Dalla versione 0.2 si possono usare più profili per backuppare più server ftp e non c'è più bisogno di modificare lo script per cambiare configurazione
Per creare un profilo basta creare un file simile al seguente nella dir /etc/zeppola (o in una a vostra scelta cambiando la variabile PROFILES_DIR nello script):
Code: | # cat /etc/zeppola/ftp.server.org
FTP_HOST="ftp.server.org"
FTP_PORT="21"
FTP_USER="utente"
FTP_PASS="password"
FTP_BDIR="/public_html/htdocs"
BACKUP_FILE="${DATE}"
BACKUP_FORMAT="tar.bz2" # Allowed arguments: "tar.bz2", "tar.gz"
BACKUP_DIR="/root/backups/${FTP_HOST}" # Must be an absolute path |
visto che contengono password consiglio di rendere questi file leggibili SOLO dall'utente che esegue zeppola
EDIT: prossima feature: creazione guidata profilo
Ciauz _________________ Bip... bip... bip... -- Sputnik 1 |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|