 Linux Backups
  Linux Backups
Linux Backups
2016-03 updated, rickatech 
Suggestions and tips for performing routine file and database backups. Best practices for and retiring/winding down Linux servers.
Winding Down and Archiving
At some point a critical linux server has been running important services for many years, but it is finally not practical to upgrade its OS and keep supporting it. To archive the system, there should only be a few file related areas to copy to archive storage, then the linux machine disk can be erased, recycled.
  - identify network accessible external storage 
  - backup to folders
    /var
    /etc
    /home
    /root
    /custom (e.g. public, opt, ... whereever you may have created
             a custom directory containing critical files)
  - do check to see what files can’t be read by only root on external storage
    du -h --max-depth 1 
Routine Backups
# crontab -l
  05  01  * * * sh /archive/sith_db_daily.sh mysql;
                sh /archive/sith_b_daily.sh wikidb;
                ls -lh /archive/sith | mail -s "sith db backups" itstaff@foobar.com
  50  01  * * * sh /archive/sith_files_daily.sh mediawiki-1.5.0;
                ls -lh /archive/sith | mail -s "sith file backups" itstaff@foobar.com#!/bin/bash
### 2006-09-30 Frederick Shaul
### MySQL database backup rotation.
### Performs backup and leaves staggered backups files
###   - last 7 day
###   - week ago backup from 2 Saturdays past
### Future?
###   - month ago backup from 2 months (first Saturday) past
###   - year ago backup from 2 years (first Saturday of first month) past
EXIT_CODE=0
if [ $1 ]
then
	DB=$1
	NAME="sith-$DB"         # include host name
	SUFFIX=sql
	BACKUP=/archive/sith    # target directory
	WDAY=`date +%w`
	DAILY=daily$WDAY
	# foobar20060929_daily3.sql, rotate 7 daily and weekly backup names
	LAST=`date +%Y%m%d`_$DAILY
	if [ $WDAY -eq 6 ]
	then
		# rename last Saturday's backup so it will kept longer than a week
		for file in $BACKUP/$NAME*_$DAILY*.$SUFFIX
		do
			mv ${file} ${file%$DAILY.$SUFFIX}weekly.$SUFFIX
		done
	else
		rm -f $BACKUP/$NAME*$DAILY.$SUFFIX
	fi
	if [ $EXIT_CODE -eq 0 ]
	then
		BNAME=$BACKUP"/"$NAME"_"$LAST"."$SUFFIX
		/usr/bin/mysqldump $DB -u root --opt > $BNAME
	fi
else
        echo "Usage: $0 [database] ..."
        echo "  "
fi#!/bin/bash
### 2006-10-10 Frederick Shaul
### file backup rotation.
### Performs backup and leaves staggered backups files
###   - last 7 day
###   - week ago backup from 2 Saturdays past
### Future?
###   - month ago backup from 2 months (first Saturday) past
###   - year ago backup from 2 years (first Saturday of first month) past
EXIT_CODE=0
if [ $1 ]
then
	NAME=sith-$1
	SUFFIX=tar.gz
	BACKUP=/archive/sith    # target directory
	WDAY=`date +%w`
	DAILY=daily$WDAY
	# foobar20060929_daily3.tar.gz, rotate 7 daily and weekly backup names
	LAST=`date +%Y%m%d`_$DAILY
	if [ $WDAY -eq 6 ]
	then
		# rename last Saturday's backup so it will kept longer than a week
		for file in $BACKUP/$NAME*_$DAILY*.$SUFFIX
		do
			mv ${file} ${file%$DAILY.$SUFFIX}weekly.$SUFFIX
		done
	else
		rm -f $BACKUP/$NAME*$DAILY.$SUFFIX
	fi
	if [ $EXIT_CODE -eq 0 ]
	then
		if [ "$1" = "mediawiki-1.5.0" ]
		then
			echo "sith wiki ..."
			rsync -a -v -x --delete /wiki/mediawiki-1.5.0 /archive/sith
	                BNAME=$BACKUP"/"$NAME"_"$LAST"."$SUFFIX
	                tar cvzf $BNAME /wiki/mediawiki-1.5.0
		else
        		echo "Usage: $0 [file-group] ..."
        		echo "  "
			echo "       mediawiki-1.5.0 | ..."
		fi
	fi
else
        echo "Usage: $0 [file-group] ..."
        echo "  "
	echo "       mediawiki-1.5.0 | ..."
fi