about summary refs log tree commit diff stats
path: root/core/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'core/scripts')
-rw-r--r--core/scripts/backup-system.sh331
1 files changed, 151 insertions, 180 deletions
diff --git a/core/scripts/backup-system.sh b/core/scripts/backup-system.sh
index 9e1ed2f..ad037ef 100644
--- a/core/scripts/backup-system.sh
+++ b/core/scripts/backup-system.sh
@@ -2,8 +2,9 @@
 
 ROOT_DIR=
 DEST_DIR=/root/backup
-PORT_PKG="${DEST_DIR}/crux"
-PORT_PRT="${DEST_DIR}/ports"
+DEST_SYS="${DEST_DIR}/system"
+PORT_PKG="${DEST_SYS}/packages"
+PORT_PRT="${DEST_SYS}/ports"
 DATA_CNF="${DEST_DIR}/conf"
 DATA_USR="${DEST_DIR}/user"
 DATA_SRV="${DEST_DIR}/srv"
@@ -20,164 +21,16 @@ ConfirmOrExit ()
                 echo "Aborting - you entered $CONFIRM"
                 exit
                 ;;
-            *) echo "Please enter only y or n"
-        esac
-    done
-    echo "You entered $CONFIRM. Continuing ..."
-}
-
-mkbk_coll_pkg() {
-    # backup binary packages per collection
-    col=$1
-    # make backup collection directory
-    mkdir ${PORT_PKG}/${col}
-    # for each package listed in col_name.pkg
-    while read line; do
-        # if binary package don't exist try to build
-        if [ ! -f /usr/ports/packages/${line} ]; then
-            echo "Building package: ${line};\n"
-            name=$(echo ${line} | cut -d "#" -f 1)
-            $sudo prt-get update -fr ${name}
-        fi
-
-        # if binary package exist copy to destination
-        if [ -f /usr/ports/packages/${line} ]; then
-            echo "Backing up package: ${line}"
-            echo ${line} >> ${DEST_DIR}/backup.pkg
-            cp /usr/ports/packages/${line} ${PORT_PKG}/${col}/
-        else
-            echo "Package not found: ${line}"
-            echo ${line} >> ${DEST_DIR}/${col}-notfound.pkg
-        fi
-    done < $DEST_DIR/${col}.pkg
-}
-
-mkbk_coll_ports() {
-    # backup collection ports
-    col=$1
-
-    tar --xattrs -zcpf $PORT_PRT/${col}.tar.gz \
-        --directory=$ROOT_DIR/usr/ports/${col} \
-        --exclude=.git/ \
-}
-
-mkbk_metadata() {
-
-    # archive pkgutils data
-    tar --xattrs -zcpf $DATA_CNF/pkg-db.tar.gz \
-        /var/lib/pkg/db
-
-    # must be using gwak instead of sed, xargs and echo
-    prt-get listinst -v | sed -s s/" "/#/g | xargs -i echo {}.pkg.tar.gz > ${DEST_DIR}/installed.pkg
-
-    # make list and copy installed core packages
-    prt-get printf "%i %p %n#%v-%r.pkg.tar.gz\n" | grep "yes /usr/ports/core" | cut -d " " -f 3 > ${DEST_DIR}/core.pkg
-
-    prt-get printf "%i %p %n#%v-%r.pkg.tar.gz\n" | grep "yes /usr/ports/opt" | cut -d " " -f 3 > $DEST_DIR/opt.pkg
-
-    prt-get printf "%i %p %n#%v-%r.pkg.tar.gz\n" | grep "yes /usr/ports/contrib" | cut -d " " -f 3 > $DEST_DIR/contrib.pkg
-
-    prt-get printf "%i %p %n#%v-%r.pkg.tar.gz\n" | grep "yes /usr/ports/xorg" | cut -d " " -f 3 > $DEST_DIR/xorg.pkg
-
-    prt-get printf "%i %p %n#%v-%r.pkg.tar.gz\n" | grep -v "yes /usr/ports/core" | grep -v "yes /usr/ports/opt" | grep -v "yes /usr/ports/contrib" | grep -v "yes /usr/ports/xorg" | grep "yes " | cut -d " " -f 3 > $DEST_DIR/other.pkg
-
-}
-
-mkbk_etc_conf() {
-
-    tar --xattrs -zcpf $DATA_CNF/etc.tar.gz \
-        --directory=$ROOT_DIR/etc \
-        .
-
-    tar --xattrs -zcpf $DATA_CNF/usr_etc.tar.gz \
-        --directory=$ROOT_DIR/usr/etc \
-        .
-}
-
-mkbk_srv_www() {
-
-    # backup web data first stop php and nginx
-
-    for pkg_www in ${ROOT_DIR}/srv/www/*; do
-        if [[ ! $(ls ${pkg_www} | grep -v "backup_deploy") = "" ]]; then
-            pkg_back="${DATA_SRV}/www"
-            if [ ! -d ${pkg_back} ]; then
-                mkdir -p ${pkg_back}
-            fi
-            bck_file="${pkg_back}/$(basename ${pkg_www}).tar.gz"
-            exc="${pkg_www}/backup_deploy"
-            tar --exclude ${exc} --xattrs -zcpf ${bck_file} ${pkg_www}
-        fi
-    done
-}
-
-mkbk_srv_pgsql() {
-
-    # backup database data first dump all databases
-
-    pkg_back="${DATA_SRV}/pgsql"
-    if [ ! -d ${pkg_back} ]; then
-        mkdir -p ${pkg_back}
-    fi
-    pg_dumpall -U postgres | gzip > ${pkg_back}/cluster_dump.gz
-
-    tar --xattrs -zcpf "${pkg_back}/pgsql-conf.tar.gz" \
-        ${ROOT_DIR}/srv/pgsql/data/pg_hba.conf \
-        ${ROOT_DIR}/srv/pgsql/data/pg_ident.conf \
-        ${ROOT_DIR}/srv/pgsql/data/postgresql.conf
-}
-
-mkbk_srv_gitolite() {
-
-    # backup gitolite repositories
-
-    pkg_back="${DATA_SRV}/gitolite"
-    if [ ! -d ${pkg_back} ]; then
-        mkdir -p ${pkg_back}
-    fi
-
-    tar --xattrs -zcpf "${pkg_back}/gitolite.tar.gz" \
-        --directory=${ROOT_DIR}/srv/gitolite \
-        .
-}
-
-mkbk_user_metadata() {
-
-    for dir in /home/*; do
-        if [ "${dir}" != "/home/lost+found" ]; then
-            user=$(basename $dir)
-            tar --xattrs -zcpf "${DATA_USR}/meta-${user}.tar.gz" \
-                $dir/.bash_profile \
-                $dir/.bashrc \
-                $dir/.config \
-                $dir/.gitconfig \
-                $dir/.gnupg \
-                $dir/.irssi \
-                $dir/.lynxrc \
-                $dir/.mutt \
-                $dir/.netrc \
-                $dir/.profile \
-                $dir/.spectrwm.conf \
-                $dir/.ssh \
-                $dir/.tmux.conf \
-                $dir/.vim \
-                $dir/.vimrc \
-                $dir/.xinitrc
-
-            # encript data
-            #gpg --output "${DATA_USR}/meta-${user}.tar.gz.gpg" \
-                #    --encrypt --recipient user@host \
-                #    "${DATA_USR}/meta-${user}.tar.gz"
-
-            tar --xattrs -zcpf "${DATA_USR}/gitolite-${user}.tar.gz" \
-                $dir/gitolite-admin
-        fi
-    done
+	*) echo "Please enter only y or n"
+esac
+done
+echo "You entered $CONFIRM. Continuing ..."
 }
 
 print_data () {
     echo "ROOT_DIR=${ROOT_DIR}"
     echo "DEST_DIR=${DEST_DIR}"
+    echo "DEST_SYS=${DEST_SYS}"
     echo "PORT_PKG=${PORT_PKG}"
     echo "PORT_PRT=${PORT_PRT}"
     echo "DATA_CNF=${DATA_CNF}"
@@ -205,11 +58,13 @@ while [ "$1" ]; do
             DEST_DIR=$2
 
             # Destination directory
-            PORT_PKG="${DEST_DIR}/crux"
-            PORT_PRT="${DEST_DIR}/ports"
-            DATA_CNF="${DEST_DIR}/conf"
-            DATA_USR="${DEST_DIR}/user"
-            DATA_SRV="${DEST_DIR}/srv"
+	    DEST_SYS="${DEST_DIR}/system"
+	    PORT_PKG="${DEST_SYS}/packages"
+	    PORT_PRT="${DEST_SYS}/ports"
+	    DATA_CNF="${DEST_DIR}/conf"
+	    DATA_USR="${DEST_DIR}/user"
+	    DATA_SRV="${DEST_DIR}/srv"
+
             shift ;;
         -h|--help)
             print_help
@@ -231,60 +86,176 @@ mkdir -p ${DATA_CNF}
 mkdir -p ${DATA_USR}
 mkdir -p ${DATA_SRV}
 
-# Light backup data
-mkbk_metadata
-mkbk_etc_conf
+# Backup system settings
+tar --xattrs -zcpf $DATA_CNF/etc.tar.gz \
+    --directory=$ROOT_DIR/etc \
+    .
 
+tar --xattrs -zcpf $DATA_CNF/usr_etc.tar.gz \
+    --directory=$ROOT_DIR/usr/etc \
+    .
+
+# User Meta Data
 while true
 do
-    echo -n "Backup user metadata ? Please confirm (y or n) :"
+    echo "Backup User Metadata ?"
+    echo "Please confirm (y or n): "
     read CONFIRM
     case $CONFIRM in
         n|N|no|NO|No) break ;;
         y|Y|YES|yes|Yes)
             echo "Accept - you entered $CONFIRM"
-            mkbk_user_metadata
+	    for dir in /home/*; do
+		if [ "${dir}" != "/home/lost+found" ]; then
+		    user=$(basename $dir)
+		    tar --xattrs -zcpf "${DATA_USR}/meta-${user}.tar.gz" \
+			$dir/.bash_profile \
+			$dir/.bashrc \
+			$dir/.config \
+			$dir/.gitconfig \
+			$dir/.gnupg \
+			$dir/.irssi \
+			$dir/.lynxrc \
+			$dir/.mutt \
+			$dir/.netrc \
+			$dir/.profile \
+			$dir/.spectrwm.conf \
+			$dir/.ssh \
+			$dir/.tmux.conf \
+			$dir/.vim \
+			$dir/.vimrc \
+			$dir/.xinitrc
+
+		    # encript data
+		    #gpg --output "${DATA_USR}/meta-${user}.tar.gz.gpg" \
+			#    --encrypt --recipient user@host \
+			#    "${DATA_USR}/meta-${user}.tar.gz"
+
+		    tar --xattrs -zcpf "${DATA_USR}/gitolite-${user}.tar.gz" \
+			$dir/gitolite-admin
+		fi
+	    done
             break
             ;;
         *) echo "Please enter only y or n"
     esac
 done
 
+# Server Data
 while true
 do
-    echo -n "Backup web services data (/srv) ? Please confirm (y or n) :"
+    echo "Backup Server Data ?"
+    echo "Please confirm (y or n): "
     read CONFIRM
     case $CONFIRM in
         n|N|no|NO|No) break ;;
         y|Y|YES|yes|Yes)
             echo "Accept - you entered $CONFIRM"
-            mkbk_srv_www
-            mkbk_srv_pgsql
-            mkbk_srv_gitolite
+
+	    # backup web data first stop php and nginx
+	    for pkg_www in ${ROOT_DIR}/srv/www/*; do
+		if [[ ! $(ls ${pkg_www} | grep -v "backup_deploy") = "" ]]; then
+		    pkg_back="${DATA_SRV}/www"
+		    if [ ! -d ${pkg_back} ]; then
+			mkdir -p ${pkg_back}
+		    fi
+		    bck_file="${pkg_back}/$(basename ${pkg_www}).tar.gz"
+		    exc="${pkg_www}/backup_deploy"
+		    tar --exclude ${exc} --xattrs -zcpf ${bck_file} ${pkg_www}
+		fi
+	    done
+
+	    # backup database data first dump all databases
+	    pkg_back="${DATA_SRV}/pgsql"
+	    if [ ! -d ${pkg_back} ]; then
+		mkdir -p ${pkg_back}
+	    fi
+	    pg_dumpall -U postgres | gzip > ${pkg_back}/cluster_dump.gz
+
+	    tar --xattrs -zcpf "${pkg_back}/pgsql-conf.tar.gz" \
+		${ROOT_DIR}/srv/pgsql/data/pg_hba.conf \
+		${ROOT_DIR}/srv/pgsql/data/pg_ident.conf \
+		${ROOT_DIR}/srv/pgsql/data/postgresql.conf
+
+
+	    # backup gitolite repositories
+	    pkg_back="${DATA_SRV}/gitolite"
+	    if [ ! -d ${pkg_back} ]; then
+		mkdir -p ${pkg_back}
+	    fi
+
+	    tar --xattrs -zcpf "${pkg_back}/gitolite.tar.gz" \
+		--directory=${ROOT_DIR}/srv/gitolite \
+		.
+
             break
             ;;
         *) echo "Please enter only y or n"
     esac
 done
 
-
+# Port System
 while true
 do
-    echo -n "Backup port system ? Please confirm (y or n) :"
+    echo "Backup Port System ?" 
+    echo "Please confirm (y or n) :"
     read CONFIRM
     case $CONFIRM in
         n|N|no|NO|No) break ;;
         y|Y|YES|yes|Yes)
             echo "Accept - you entered $CONFIRM"
-            mkbk_coll_ports "core"
-            mkbk_coll_pkg "core"
-            mkbk_coll_ports "opt"
-            mkbk_coll_pkg "opt"
-            mkbk_coll_ports "contrib"
-            mkbk_coll_pkg "contrib"
-            mkbk_coll_ports "xorg"
-            mkbk_coll_pkg "xorg"
-            mkbk_coll_pkg "other"
+
+	    # archive pkgutils data
+	    tar --xattrs -zcpf $DEST_SYS/pkg-db.tar.gz \
+		/var/lib/pkg/db
+
+	    # archive ports data
+	    tar --xattrs -zcpf $DEST_SYS/etc_ports.tar.gz \
+		    --directory=/etc/ports \
+		    .	
+
+	    METADATA=${DEST_SYS}/meta-data
+	    mkdir -p $METADATA
+
+	    # must be using gwak instead of sed, xargs and echo
+	    prt-get listinst -v | sed -s s/" "/#/g | xargs -i echo {}.pkg.tar.gz > ${METADATA}/all_installed.pkg
+
+	    for filename in /etc/ports/*.git; do
+	    	source $filename
+
+		echo "Backing up collection: $NAME"
+		# create list of installed packages 
+		prt-get printf "%i %p %n#%v-%r.pkg.tar.gz\n" | grep "yes /usr/ports/${NAME}" | cut -d " " -f 3 > ${METADATA}/${NAME}-installed.pkg
+
+		# backup collection ports
+		tar --xattrs -zcpf $PORT_PRT/${NAME}-ports.tar.gz \
+		--directory=$ROOT_DIR/usr/ports/${NAME} \
+		--exclude=.git/ \
+		.
+
+		# backup collection packages
+		while read line; do
+			if [ ! -f /usr/ports/packages/${line} ]; then
+				echo "Building package: ${line};\n"
+				PORT_NAME=$(echo ${line} | cut -d "#" -f 1)
+				sudo prt-get update -fr -if -is ${PORT_NAME}
+				(cd /usr/ports/${NAME}/${PORT_NAME} \
+				       	&& sudo pkgmk -uf)
+			fi
+
+			if [ -f /usr/ports/packages/${line} ]; then
+				echo "Backing up package: ${NAME}/${line}"
+				echo ${line} >> ${METADATA}/backup.pkg
+				#cp /usr/ports/packages/${line} ${PORT_PKG}/${col}/
+				tar rvf ${PORT_PKG}/${NAME}.tar \
+					--directory=/usr/ports/packages \
+					${line}
+			else
+				echo "Package $PORT_NAME not found: ${line}"
+				echo ${line} >> ${METADATA}/${NAME}-notfound.pkg
+			fi
+		done < ${METADATA}/${NAME}-installed.pkg
+	    done
             break
             ;;
         *) echo "Please enter only y or n"