~drscream
Kleines einfaches Backup (Bash-)Script
Es gibt sicher einiges effektivere und bessere Scripte um ein System und MySQL Backup zu machen. Das ist jetzt auch nur die erste Version des ganzen und evtl. kann es jemand gebrauchen :)
#####################################################################
# srv-backup.sh - backup the system and the databases #
# @author: drscream@cyber-tec.org #
# @version: Thu Oct 4 11:35:39 CEST 2007 #
#####################################################################
# CONFIGURATION
#####################################################################
# crontab example:
# 21 4 * * * /opt/av-srv/av-srv-backup.sh mysql
# 2 3 1,15 * * /opt/av-srv/av-srv-backup.sh system
# 2 4 * * * /opt/av-srv/av-srv-backup.sh webroot
# 3 6 * * * /opt/av-srv/av-srv-backup.sh pgsql
## log information ##################################################
log_file="/local/backup/backup.log"
datetime=$(date +%Y%m%d-%H%M%S)
fqdn=$(hostname -f)
## select what to backup ############################################
# => 0/1: deactivate or activate the backup
# => /srv/xxx: path to save the backup
# => asdf.tar.bz2: file to save the backup
# => sysdir.txt: text file with the directories to backup
# => rsync: rsync is set the file will rsynced to the backupsrv
system=(1 '/local/backup/system' 'system.$fqdn.$datetime.tar.gz' './systemdir.txt' 'rsync')
# => 0/1: deactivate or activate the backup
# => /srv/xxx: path to save the backup
# => asdf.tar.bz2: file to save the backup
# => /web: directory to backup
# => rsync: rsync is set the file will rsynced to the backupsrv
webroot=(1 '/local/backup/webroot' 'web.$fqdn.$datetime.tar.gz' '/web' 'rsync')
# => 0/1: deactivate or activate the backup
# => /srv/xxx: path to save the backup
# => user: sql user (root)
# => password: sql password
# => rsync: rsync is set the file will rsynced to the backupsrv
mysql=(1 '/local/backup/mysql' 'root' 'qwe123' 'rsync')
# => 0/1: deactivate or activate the backup
# => /srv/xxx: path to save the backup
# => asdf.tar.bz2: file to save the backup
# => user: sql user (root)
# => rsync: rsync is set the file will rsynced to the backupsrv
pgsql=(0 '/local/backup/pgsql' 'pgsql.$fqdn.$datetime.sql.gz' 'postgres' 'rsync')
# => 0/1: rsync to the specified server
# => username: username for rsync
# => hostname: specified backup server host
# => /path: path to save the backup ($fqdn is added)
rsync=(1 'username' 'hostname' '/backup')
rsync_args="--archive"
## delete the files after <xx> days #################################
delete_files=20
## global variables #################################################
export LANG="en_US.utf8"
PARAM=$1
# FUNCTIONS
#####################################################################
function delete() {
find $1 -type f -mtime +$delete_files | xargs rm 2>/dev/null
}
function log() {
echo $1 >> $log_file
}
function backuprsync() {
if [[ ${rsync[0]} == 1 && ${1} == "rsync" ]]; then
log "($datetime) rsync to server ---------------------------"
rsync ${rsync_args} $2 ${rsync[1]}@${rsync[2]}:${rsync[3]}/${fqdn} >> $log_file
log "end rsync to server -----------------------------------"
fi
}
# MAIN
#####################################################################
## system ###########################################################
if [[ ${system[0]} == 1 && ${PARAM} == "system" ]]; then
# cleanup system backup
delete ${system[1]}
# start system backup
log "($datetime) systembackup: ---------------------------------"
log "backup following files:"
dir_backup=$(cat ${system[3]})
log "$dir_backup"
# compress the files
cd /
tar cvpzf "${system[1]}/${system[2]}" $dir_backup
# rsync if it is set
backuprsync ${system[4]} ${system[1]}/${system[2]}
log "end systembackup ------------------------------------------"
fi
## webroot ##########################################################
if [[ ${webroot[0]} == 1 && ${PARAM} == "webroot" ]]; then
# cleanup webroot backup
delete ${webroot[1]}
# start webroot backup
log "($datetime) webrootbackup: --------------------------------"
log "backup following dir:"
dir_backup=$(ls -la ${webroot[3]})
log "$dir_backup"
# compress the files
cd ${webroot[3]}
tar cvpzf "${webroot[1]}/${webroot[2]}" *
# rsync if it is set
backuprsync ${webroot[4]} ${webroot[1]}/${webroot[2]}
log "end webrootbackup -----------------------------------------"
fi
## mysql ############################################################
if [[ ${mysql[0]} == 1 && ${PARAM} == "mysql" ]]; then
# cleanup mysql backup
delete ${mysql[1]}
# start mysql backup
log "($datetime) mysqlbackup -----------------------------------"
mysqlshow -u ${mysql[2]} --password=${mysql[3]} | tr -d '|' | tr -d '+' | tr -d ' ' | tr -d '-' > /tmp/sql-backup-list.tmp
list=/tmp/sql-backup-list.tmp
ig=3
xr_lines=$(wc -l $list | awk '{ print $1 }')
while [ "$ig" -le $xr_lines ]; do
ig=$(expr $ig + 1)
item=$(awk "NR == $ig {print}" $list)
mysqldump --add-drop-table --allow-keywords -q -a -c -u ${mysql[2]} --password=${mysql[3]} $item | gzip > ${mysql[1]}/$item.sql.gz
echo $item >> $dir_backup
done
# Remove a temp file
rm -f /tmp/sql-backup-list.tmp
# rsync if it is set
backuprsync ${mysql[4]} ${mysql[1]}
log "end mysqlbackup -------------------------------------------"
fi
## pgsql ############################################################
if [[ ${pgsql[0]} == 1 && ${PARAM} == "pgsql" ]]; then
# cleanup pgsql backup
delete ${pgsql[1]}
# start pgsql backup
log "($datetime) pgsqlbackup -----------------------------------"
pg_dumpall -U${pgsql[3]} | gzip > "${pgsql[1]}/${pgsql[2]}.gz"
# rsync if it is set
backuprsync ${pgsql[4]} ${pgsql[1]}
log "end pgsqlbackup -------------------------------------------"
fi
Send your comment by mail.