#!/bin/sh SVR=$1 DAUSR=$2 DARESEL=$3 BASE=/backuppool/${SVR}/dag-0 DABACKUPTEMPLATE=/backuppool/template/backup_options.list.default LOGFILE=/var/log/gen_da_backup.log VER=0.0.3 ########################################################################################### # # eisen: # - je hebt een remote backupserver die middels ssh keys op een da back kan inloggen # - op je backup server heb je een rsync backup van je da server met minimaal de paden # - /home/, /var/named/, /var/www/, /etc/, /usr/local/directadmin/. # - dit script gaat er vanuit dat je servers in /backuppool//dag-0/ de directory # structuur van je laatste rsync backup staat. # # /backuppool/template/backup_options.list.default test content: # # autoresponder # database # database_data # database_data_aware # dns # domain # email # email_data # email_data_aware # emailsettings # forwarder # ftp # ftpsettings # list # subdomain # vacation # # aanroepen zoals bv: ./gen_da_backup.sh server102 kees01 reseller01 # na afloop staat in /backuppool/server102/dag-0/home/admin/admin_backups/ een bestand # gen_user.reseller01.kees01.tar.gz die je kunt uploaden naar je DA server in de /home/admin/admin_backups/ # directory en dan (als je de eigenaar op 'admin' hebt gezet) via het controlpanel als admin # weer kunt herstellen. (Vergeet de 'gen_' prefix niet te verwijderen. Die is er alleen om te voorkomen # dat een echte da backup wordt overschreven). # # Tip: globaal controleren of alle bestanden worden meegenomen kan via: # $ diff -rq 2>/dev/null | grep 'Only in' # ########################################################################################### function quit { exit 1; } usage() { echo -e "***********************************************************************" echo -e "** Generate DA Backup\nUsage: $0 " echo -e "-----------------------------------------------------------------------" echo -e "** Make sure you've rsync'd everything we need data from" echo -e "***********************************************************************" quit; } function doLog () { echo -e "`date`: GEN_DA_BACKUP: $1" >> ${LOGFILE} } function copyFile () { if [ -f $1 ]; then cp -Rp $1 $2 doLog "Copied file '$1' to '$2'"; elif [ -d $1 ]; then cp -Rp $1 $2 doLog "Copied directory '$1' recursively to '$2'"; else doLog "WARNING: Directory or File '$1' does not exist, not copied"; fi } function moveFile () { if [ -f $1 ]; then if [ ! -f $2 ]; then mv $1 $2 doLog "Moving '$1' to '$2'"; else doLog "ERROR: Target '$2' already exists, not overwriting" fi else doLog "WARNING: Directory or File '$1' does not exist, not moved"; fi } # Check supplied parameters if [[ -z $DARESEL ]]; then DARESEL=admin fi if [[ -z $SVR || -z $DAUSR ]]; then usage fi function b_Init { if [ -f ${LOGFILE} ]; then now=$(stat -c %Y ${LOGFILE}) moveFile ${LOGFILE} ${LOGFILE}.${now} fi doLog "********************************************************************************" doLog "* Generate DA Backupfiles *"; doLog "* Version: ${VER} *"; doLog "********************************************************************************" # setup tmp directory mkdir -p ${BASE}/home/admin/admin_backups/${DAUSR}/ cd "${BASE}/home/admin/admin_backups/${DAUSR}/" # create some directories and copy required files for da mkdir -p backup imap domains } function b_DomainsContent { # domain content. You might want to skip this for fast recovery and rsync the contents after the restore copyFile ${BASE}/home/${DAUSR}/domains/ ${BASE}/home/admin/admin_backups/${DAUSR}/ } function b_Passwords { # password cat ${BASE}/etc/shadow|grep ${DAUSR} | awk -F':' '{print $2}' > ${BASE}/home/admin/admin_backups/${DAUSR}/backup/.shadow } function b_GatherSettings { # gather some da stuff in the backup dir. This needs work cd ${BASE}/home/admin/admin_backups/${DAUSR}/backup/ # files owned by apache -> that should not happen so f*ck it. touch ${BASE}/home/admin/admin_backups/${DAUSR}/backup/apache_owned_files.list # copy backup template for DA to figure out what to restore copyFile ${DABACKUPTEMPLATE} ${BASE}/home/admin/admin_backups/${DAUSR}/backup/backup_options.list # copy part of da-user configs for i in bandwidth.tally crontab.conf ticket.conf user.conf user.history user.usage; do copyFile ${BASE}/usr/local/directadmin/data/users/${DAUSR}/${i} ${BASE}/home/admin/admin_backups/${DAUSR}/backup/ done # create some mail dirs, put stuff in it. # need to add missing stuff like webmail, roundcube, w/e mkdir email_data history email_data/squirrelmail email_data/webmail copyFile ${BASE}/var/www/html/squirrelmail/data/${DAUSR}.pref email_data/squirrelmail # history copyFile ${BASE}/usr/local/directadmin/data/users/${DAUSR}/history/* history # Copy & rename things like certs and configs. Get the zone, handlers, san_config etc. for each domain for d in `cat ${BASE}/usr/local/directadmin/data/users/${DAUSR}/domains.list`; do mkdir -p "${d}/email/data/imap" mkdir -p "${d}/email/data/webmail" mkdir -p "${d}/email/data/squirrelmail" mkdir -p "${d}/email/majordomo/" mkdir -p "${d}/email/reply/" # domain cert stuff copyFile ${BASE}/usr/local/directadmin/data/users/${DAUSR}/domains/${d}.conf ${d}/domain.conf copyFile ${BASE}/usr/local/directadmin/data/users/${DAUSR}/domains/${d}.usage ${d}/domain.usage copyFile ${BASE}/usr/local/directadmin/data/users/${DAUSR}/domains/${d}.cacert ${d}/domain.cacert copyFile ${BASE}/usr/local/directadmin/data/users/${DAUSR}/domains/${d}.cert ${d}/domain.cert copyFile ${BASE}/usr/local/directadmin/data/users/${DAUSR}/domains/${d}.key ${d}/domain.key copyFile ${BASE}/usr/local/directadmin/data/users/${DAUSR}/domains/${d}.subdomains ${d}/subdomain.list # ftp goodies copyFile ${BASE}/usr/local/directadmin/data/users/${DAUSR}/domains/${d}.ftp ${d}/ftp.conf cat ${BASE}/usr/local/directadmin/data/users/${DAUSR}/ftp.passwd | grep ${d} > ${d}/ftp.passwd # http goodies copyFile ${BASE}/usr/local/directadmin/data/users/${DAUSR}/domains/${d}.handlers ${d}/domain.handlers copyFile ${BASE}/usr/local/directadmin/data/users/${DAUSR}/domains/${d}.san_config ${d}/domain.san_config copyFile ${BASE}/usr/local/directadmin/data/users/${DAUSR}/domains/${d}.mime.types ${d}/domain.mime.types # named/bind goodies copyFile ${BASE}/var/named/${d}.db ${d}/${d}.db # email goodies copyFile ${BASE}/etc/virtual/${d}/* ${d}/email/ #email parameters. Still missing some spamassassin settings (high score etc) cat ${BASE}/usr/local/directadmin/data/users/${DAUSR}/user.conf | grep 'catchall' > ${d}/email/email.conf echo -e "\n" > ${d}/email/data/roundcube.xml echo -e "num_emails=1" > ${d}/email/data/imap/.direct_imap_backup for ve in $(ls ${BASE}/var/www/html/squirrelmail/data/ | grep "@${d}.pref"); do copyFile ${BASE}/var/www/html/squirrelmail/data/${ve} ${BASE}/home/admin/admin_backups/${DAUSR}/backup/${d}/email/data/squirrelmail/ done done b_Passwords } function b_Imap { copyFile ${BASE}/home/${DAUSR}/imap ${BASE}/home/admin/admin_backups/${DAUSR}/ } function b_Tarit { # generate tar.gz files /bin/tar czfp ${BASE}/home/admin/admin_backups/${DAUSR}/backup/home.tar.gz -C ${BASE}/home/${DAUSR}/ '.bash_logout' '.bash_profile' '.bashrc' '.cshrc' '.login' '.login_conf' '.mail_aliases' '.mailrc' '.php' '.profile' '.razor' '.rhosts' '.shrc' '.spamassassin' 'Maildir' /bin/tar czfp ${BASE}/home/admin/admin_backups/${DAUSR}/user.${DARESEL}.${DAUSR}.tar.gz -C ${BASE}/home/admin/admin_backups/${DAUSR} backup -C ${BASE}/home/${DAUSR}/ domains imap } function b_Cleanup { # cleanup... if moveFile ${BASE}/home/admin/admin_backups/${DAUSR}/user.${DARESEL}.${DAUSR}.tar.gz ${BASE}/home/admin/admin_backups/gen_user.${DARESEL}.${DAUSR}.tar.gz; then rm -rf ${BASE}/home/admin/admin_backups/${DAUSR} doLog "Cleanup done." else doLog "Not cleaning up" fi } b_Init b_DomainsContent b_GatherSettings b_Imap b_Tarit b_Cleanup