a .bashrc function to make quick backups of files.
Page 1 of 1

Author:  crouse [ Mon Jan 15, 2007 11:50 pm ]
Post subject:  a .bashrc function to make quick backups of files.

Running ArchLinux, I like to mess with system configs alot, and i was wanting an easy way to make intelligent backup copies of my files ... QUICKLY.

To the rescue, the .bashrc file !!! :D

Insert this function into your .bashrc file to make quick work of backing up files.

bu () { cp $1 ${1}-`date +%Y%m%d%H%M`.backup ; }

it works pretty simply, say you want to make a backup copy of your /etc/fstab file ...... (now who DOESN'T need a backup copy of that I ask you ? :) ) You can supply the full path /etc/fstab shown below, or you can cd into the directory the file resides in and just call the shown even FURTHER below ;) The command is simply bu followed by either the full path and filename, or if your already in the files directory, just bu FILENAME

[[email protected] ~]# bu /etc/fstab
[[email protected] ~]# cd /etc/

-rw-r--r--  1 root root   595 Jan 15 02:54 fstab
-rw-r--r--  1 root root   595 Jan 15 23:46 fstab-200701152346.backup

[[email protected] etc]# bu fstab
[[email protected] etc]# ls -la

-rw-r--r--  1 root root   595 Jan 15 02:54 fstab
-rw-r--r--  1 root root   595 Jan 15 23:46 fstab-200701152346.backup
-rw-r--r--  1 root root   595 Jan 15 23:48 fstab-200701152348.backup

The backup copy is listed by Year/Month/Day/Hour/Minute ...... and if your REALLY crazy, you could add in seconds....... ;) Makes it easy to keep track of when you made a backup and makes it easy to do too. ;)

Want to remove all the .backup files ???
rm -f *.backup
..... make sure nothing else is labeling their backups with the same .backup extension or you might erase something you didn't intend too ! ;) Hope it comes in handy for someone else besides myself ;)

Author:  crouse [ Tue Jan 16, 2007 2:10 am ]
Post subject: 

Some might rather have backup put key files somewhere else ie in .backup or /backup

You could do that pretty easily too...
bu () { cp $1 /backup/${1}-`date +%Y%m%d%H%M`.backup ; }

This would put all files into /backup/

bu () { cp $1 ~/.backup/${1}-`date +%Y%m%d%H%M`.backup ; }

This would put all the files into ~/.backup (in your home directory)

this would require that you are IN the directory of the file you want to backup. You would move the file with the bu FILENAME ...... you could not use the /path/filename method like i listed above.

Author:  BrionS [ Tue Jan 16, 2007 5:29 am ]
Post subject: 

Here's a twist to so you can maintain your backed-up file in the same directory structure in your .backup directory
bu ()
    if [ "`dirname $1`" == "." ]; then
        mkdir -p ~/.backup/`pwd`;
        cp $1 ~/.backup/`pwd`/$1-`date +%Y%m%d%H%M`.backup;
        mkdir -p ~/.backup/`dirname $1`;
        cp $1 ~/.backup/$1-`date +%Y%m%d%H%M`.backup;

In this case, creating a backup of /etc/fstab will place it in ~/.backup/etc/fstab and creating a backup of 'somefile' in the current directory will place it in the current working folder in .backup.

bash-3.2$ bu /etc/fstab
bash-3.2$ ls -l ~/.backup/etc

-rw-r--r-- 1 brion brion 714 2007-01-16 06:32 fstab-200701160632.backup

bash-3.2$ pwd

bash-3.2$ touch file
bash-3.2$ bu file
bash-3.2$ ls -l ~/.backup/home/brion/

-rw-r--r-- 1 brion brion 0 2007-01-16 06:33 file-200701160633.backup

bash-3.2$ ls -l ~/.backup

drwxr-xr-x 2 brion brion 96 2007-01-16 06:32 etc
drwxr-xr-x 3 brion brion 72 2007-01-16 06:33 home

In addition, you can safely delete any/all files in the ~/.backup directory since this script is the only thing that creates/modifies it. You can even 'rm -fr' the ~/.backup directory and the script will re-create it as necessary.

Author:  afflictedd2 [ Mon May 17, 2010 1:54 pm ]
Post subject:  Re: a .bashrc function to make quick backups of files.

It would be great to make it also as a backup utility for directories as well.


Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group •