#!/bin/bash # # This backup script will backup selected files and folders in this manner: # 1. Check if the backup-folder is valid for backing up to. # 2. If there is already an existing backup, back that up into $tempdir/backup_{MDY}.tar.bz2. # 3. Move all old .tar.bz2 backups from the backup-folder to $tempdir. # 4. Delete the content of the old backup-folder. # 5. Move back all .tar.bz2 (old and new backups) from $tempdir to the backup-folder. # 6. Create directory structure from $mkdr. # 7. Copy files ($File) and directories ($Dirs) to the backup-folder. # 8. Check if any errors occurd and notify if so. UNDERSTOOD="no" # Change to yes to make this program functional (no not delete)! path="/mnt/hdd6/backups"; # The path to where you want to store the backups (No ending slash!). # This script will delete its content, so be warned! tempdir="/tmp" # Temporary folder to use (must be without ending slash, existing and writeable!). errorlog=".errorlog"; # The filename of the errorlog (will be created if nonexistant). # Edit the directories to be created in this order: mkdr=" boot etc etc/X11 etc/conf.d usr usr/X11R6 usr/X11R6/bin var var/spool var/spool/cron var/spool/cron/crontabs"; # Edit your files and folders here (do not use any wildcards!): ver=$(uname -r); # Kernel version. File=" /boot/System.map-$ver /boot/kernel-$ver /boot/config-$ver /boot/initrd-1024x768 /etc/X11/XF86Config /etc/X11/XF86Config.example /etc/X11/XftConfig /etc/conf.d/local.start /etc/dnsdomainname /etc/gentoo-release /etc/fstab /etc/group /etc/issue /etc/issue.logo /etc/make.conf /etc/motd /etc/ntp.conf /etc/passwd /etc/profile /etc/rc.conf /etc/resolv.conf /etc/sudoers /usr/X11R6/bin/startx /var/spool/cron/crontabs/root"; # Whole directories to back-up. Dirs=" /boot/grub"; # ------------------------------------------------------------------- # - DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING! - # - I CLAIM NO RESPONSIBILITY FOR ANY OF YOUR MISTAKES! - # ------------------------------------------------------------------- version="1.0"; email="agilo3@gmail.com"; # Functions. domount() { umount /boot 2>/dev/null; # Making sure it's umounted. mount /boot 2>/dev/null; if [ $? != 0 ]; then echo "I was unable to mount /boot (is /etc/fstab set up correctly?)." >> ~/$errorlog; retval=1; else umount=1; fi } dodelete() { rm -rf $path/* 2>/dev/null; if [ $? != 0 ]; then echo "Could not delete the content of \"$path\"!"; exit 1; fi; } # Check to see if the user knows what he is doing. if [ $UNDERSTOOD == "no" ]; then echo -e "\n\nIt seems you have not yet configured this program."; echo -e "This program will not work correctly if not set up properly and I" \ "take no responsibility for any damage caused to you due to misconfiguration!\n\n"; echo -e "To enable this program, please change \$UNDERSTOOD to \"yes\" in '"$(basename $0)"'\n"; exit 1; fi # Arguments available to the program: if [ $# -gt 1 ]; then echo -e "Too many arguments.\n"; exit 1; fi for i in $1; do case "$i" in -ym | -my) # Yes to all questions and mount /boot. domount; yes=1;; -y | --yes) # Yes to all questions. yes=1;; -m | --mount) # Mount /boot. domount;; -h | --help) # Show usage. echo -e "Usage: $(basename $0) [OPTION]\nMake a backup of important files.\n\n" \ " -y, --yes\t\tAnswer \"Yes\" to all questions.\n" \ " -m, --mount\t\tMount /boot.\n" \ " -h, --help\t\tDisplay this help and exit.\n" \ " -v, --version\t\tOutput version information and exit.\n" \ "\n\nBackup utility v$version created by Agilo ($email)."; exit;; -v | --version) # Show version information. echo -e "Backup utility version $version.\nCreated by Agilo ($email).\n"; exit;; *) # Everything else. echo -e "Invalid argument!\nSee \"--help\".\n"; exit;; esac done # Check if the user is root. if [ $UID -ne 0 ]; then echo -e "You must run $(basename $0) as root.\n"; exit 1; fi # Check if the folder is valid. if [ $(echo $path | sed -r "s/^\/(.*)\/$/true/") == "true" ]; then invalid=1; fi if [ ! -e $path ] && [ ! -d $path ] && [ ! -r $path ] && [ ! -w $path ]; then invalid=1; fi if [ $invalid ] && [ $invalid == 1 ]; then echo -e "Error: the backup directory must be valid (existing and writeable)!\n\n" \ "\nPlease edit the file '"$(basename $0)"' and change the path \"\$path\" is pointing to."; echo -e "Note: there must be no ending slash!\n"; exit 1; fi; # Backup the backup folder's content in a .tar.bz2 file. echo -e "\n[=] Backing up backup folder's content."; if [ ! $(tar --version | sed -r "s/^(.*)GNU(.*)$/GNU/") == "GNU" ]; then echo -e "Skipping: you must have GNU's 'tar' to create an archive (otherwise; edit this script).\n" sleep 3; else mv $path/*.tar.bz2 $tempdir 2>/dev/null if [ $(ls $tempdir/backup_`date +'%m%d%Y'`.tar.bz2 2>/dev/null) ] && [ $(ls $tempdir/backup_`date +'%m%d%Y'`.tar.bz2) == "backup_$(date +'%m%d%Y').tar.bz2" ]; then rm backup_$(date +'%m%d%Y').tar.bz2 2>/dev/null if [ $? != 0 ]; then echo "Unable to delete old backup archive." >> ~/$errorlog; retval=1; fi fi $(cd $path && tar -cjf $tempdir/backup_$(date +'%m%d%Y').tar.bz2 * 2>/dev/null) if [ $? != 0 ]; then echo "Unable to backup the backup folder's content." >> ~/$errorlog; retval=1; else restore=1; fi fi # Delete old backup folder content. echo -ne "\n[-] Deleting old backup folder content.\nContinue? [y/n] "; if [ $yes ] && [ $yes == "1" ]; then echo -n "y"; dodelete; else read del; if [ $del == "y" ]; then dodelete; else # All other input will result in abort. echo -e "\nAborted.\n"; if [ $umount ] && [ $umount == "1" ]; then umount /boot; fi exit; fi fi; # Copy back old archives from the temporary folder. if [ $restore ] && [ $restore == "1" ]; then mv $tempdir/*.tar.bz2 $path 2>/dev/null chmod 700 $path/*.tar.bz2 fi # Create the directories. echo -e "\n[=] Creating directories."; for i in $mkdr; do echo "[+] $path/$i"; mkdir $path/$i >/dev/null 2>>~/$errorlog if [ $? != 0 ]; then retval=1; fi; done # Copy the directories. echo -e "\n[=] Copying the directories."; for i in $Dirs; do echo "[>] $i -> $path$(echo $i | awk -F"`basename $i`" '{print $1}')"; cp -R $i $path$(echo $i | awk -F"`basename $i`" '{print $1}') 2>>~/$errorlog if [ $? != 0 ]; then retval=1; fi done # Copy the files. echo -e "\n[=] Copying the files."; for i in $File; do echo "[>] $i -> $path$(echo $i | awk -F"`basename $i`" '{print $1}')"; cp $i $path$(echo $i | awk -F"`basename $i`" '{print $1}') 2>>~/$errorlog if [ $? != 0 ]; then retval=1; fi; done # Check if an error has occurd and, if so, notify the user. if [ $retval ] && [ $retval == "1" ]; then echo -e "---\n$(date)\n\n\n\n" >> ~/$errorlog 2>/dev/null; echo -e "\n\n\n\nErrors have occurd. Please see ~/$errorlog\n\n"; exit 1; fi # Done. Notify user. echo -e "\nBackup of files completed successfully.\n\n"; if [ $umount ] && [ $umount == "1" ]; then umount /boot; fi