When I have to do a reinstall, sometimes I have to install from scratch – doing a clean install is just sometimes necessary. My configurations are priceless to me and after my reinstall I restore them from a backup copy. Here’s how I quickly add my configurations to an include file and back them up on a regular basis with a couple of tar helpers.
Basic tar Command
A good number of GUI programs can do this (you can read about the ones I’ve looked at here), but they seem to make the process more complicated, so I went back to tar and created a couple helper scripts. For those new to tar, the basic archive command is:
tar -cvpzf <archive-name>.tar.gz /folder/file /aplain/folder
Include/Exclude Files
If you put the archive command in a bash script, you can use it later. Files and folders can be added to the command, but consider using include and exclude files:
#!/bin/bash tar --exclude-from=/<location-of>/exclude.txt -cvpzf \ --file-from=<location-of>/include.txt <backup-name>.tgz
Include/exclude files contain the path on a line of what or what-not to backup. The include file should include the full path and cannot use regexps but the exclude fine can:
# Trashes not necessary /home/*/.local/share/Trash/files /home/*/.Trash
Add Paths to Include/Exclude Quickly
To add paths to the include/exclude files, readlink can be used. Here are a couple bash scripts to add to your include/exclude files.
For includes:
#!/bin/bash
# bca (backup-cfg-add) - add file/folders to the backup-cfg include file
incfile=/home/todd/.bin/root/backup/include.txt
# Program name from it's filename
prog=${0##*/}
# Text color variables
bldblu='\e[1;34m' # blue
bldred='\e[1;31m' # red
txtund=$(tput sgr 0 1) # underline
txtrst='\e[0m' # text reset
pass=${bldblu}*${txtrst}
warn=${bldred}!${txtrst}
# Add file/folder/link to list
if [[ -z "$@" ]]; then
echo " $prog <file/folder/link> - add files/folders/links to backup-cfgs' include file"
exit
fi
fullpath=$(readlink -f "$@")
# Test if link is valid
if [ ! -e "$fullpath" ]; then
echo -e "$warn File "$@" doesn't exist."
exit
fi
# Add entry
echo "$fullpath" >> "$incfile"
echo -e "$pass Added ${txtund}$fullpath${txtrst} in backup include file"
# Sort entries
sort -u "$incfile" -o "$incfile"
Then to add a file, folder, or link:
bca /etc/fstab # or cd /etc bca fstab
For excludes:
#!/bin/bash
# bce (backup-config-exclude) - add files to be excluded in backup
excfile=/home/todd/.bin/root/backup/exclude.txt
# Program name from it's filename
prog=${0##*/}
# Text color variables
txtbld=$(tput bold) # bold
txtund=$(tput sgr 0 1) # underline
bldblu='\e[1;34m' # blue
bldred='\e[1;31m' # red
bldwht='\e[1;37m' # white
txtrst='\e[0m' # text reset
info=${bldwht}*${txtrst}
pass=${bldblu}*${txtrst}
warn=${bldred}!${txtrst}
# Add file/folder/link to list
if [[ -z "$@" ]]; then
echo " $prog '<path/*/files...>' - add exclusions to backup configuration"
exit
fi
echo -e "$info bce doesn't check valid paths because the exclude file can contain regexps."
echo -e "$warn Be sure the path is correct! (e.g '/mnt/win/*')"
echo -en "${txtbld}?${txtrst} Add this to the exclude file? (${txtbld}y${txtrst}/${txtbld}n${txtrst}): "
read question
if [[ $question == [Yy] ]]; then
echo "$@" >> $excfile
echo -e "$pass Added ${txtund}"$@"${txtrst} to the backup-cfg exclude file" else
exit
fi
Creating notes of backups can be done by:
#!/bin/bash # bcn (backup-cfg-notes) add notes to file in backup directory # Author: Gen2ly # Label computer, distro, type and date pc=$HOSTNAME distro=arch type=configs date=`date "+%F"`-`date "+%r"` # Where to backup target="/home/todd/.backup" notename="backup-notes.txt" # Append note echo "$date: "$@"" >> $target/$notename
emach ~/.backup: Added dhcp cacher config emach ~/.backup: bcn Added font config emach ~/.backup: cat backup-notes.txt 2009-08-14-02:52:23 PM: Added dhcp cacher config 2009-08-14-02:52:33 PM: Added font config
The Backup Script
This is the backup script I use to backup my configs, names the backup by hostname and date, and removes old backups:
#!/bin/bash
# backup-cfg - backup configurations with tar
# Backup name
machine=$HOSTNAME
distro=arch
type=configs
date=`date "+%F"`
backupname=$machine-$distro-$type-$date
# Backup destination
backdest="/opt/backup/pc-emach"
# Files contailing information to include and exclude
bcdir="/home/todd/.bin/root/backup"
include_file="$bcdir"/bc-include.txt
exclude_file="$bcdir"/bc-exclude.txt
# Verify that the target directory exists.
if [ ! -d $backdest ]; then
echo " Backup directory does not exist, creating;"
mkdir $backdest; else
echo " Backup directory exists."
fi
# Delete backups older than a month
if [[ -n "$(find "$backdest" -mtime +30)" ]]; then
find "$backdest" -mtime +30 -exec rm {} \;
echo -e " Backups older than two months deleted"
fi
# Unmount other drives/partitions
#umount -a
# Backup
tar --exclude-from=$exclude_file --files-from=$include_file -cvpzf \
$backdest/$backupname.tar.gz
Consider putting it in a cron job to get regular backups.
o/
Very good article!
Thank you.