A convenient Bash function to backup configuration file

For sysadmins, it’s a good practice to backup configuration files before changing them. We normally do it via cp. eg:

cp named.conf named.conf.bak
cp named.conf named.conf.orig

I personally like to use the date time as the file name suffix, so that I can tell from the file name when I did that backup. Also, with this method, multiple version of the config file can be kept.

Because I use this method frequently, I wrote a Bash function to do this:

b(){
        for f in "$@"
                do cp -a "$f" "$f".$(date +%Y%m%d%H%M)
        done
}

Add it to my ~/.bash_profile . And run

source ~/.bash_profile

Then every time I need to backup a file, I just need to do this

b /etc/mail/sendmail.mc
#this will create a backup file named like /etc/mail/sendmail.mc.201105301420
cd /var/named/chroot/etc/
b named.conf #relative file name also works
b /etc/postfix # you can also backup a whole directory

In fact, its usage is not limited to configuration file, you can use it to backup whatever you like.

This entry was posted in Programming, System Administration and tagged , . Bookmark the permalink.

Leave a Reply