Register
It is currently Tue Jul 29, 2014 10:47 am

a .bashrc function to make quick backups of files.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Mon Jan 15, 2007 11:50 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 663
Location: Des Moines, Iowa
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.

Code:
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 ......as shown below, or you can cd into the directory the file resides in and just call the file....as 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

Code:
[root@VistaKillerTwo ~]# bu /etc/fstab
[root@VistaKillerTwo ~]# 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
---

[root@VistaKillerTwo etc]# bu fstab
[root@VistaKillerTwo 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 ???
Code:
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 ;)


Top
 Profile WWW  
 PostPosted: Tue Jan 16, 2007 2:10 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 663
Location: Des Moines, Iowa
Some might rather have backup put key files somewhere else ie in .backup or /backup


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

This would put all files into /backup/

Code:
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.


Top
 Profile WWW  
 PostPosted: Tue Jan 16, 2007 5:29 am   
User avatar

Joined: Mon Jul 03, 2006 8:58 pm
Posts: 52
Location: Rochester, NY
Here's a twist to so you can maintain your backed-up file in the same directory structure in your .backup directory
Code:
bu ()
{
    if [ "`dirname $1`" == "." ]; then
        mkdir -p ~/.backup/`pwd`;
        cp $1 ~/.backup/`pwd`/$1-`date +%Y%m%d%H%M`.backup;
    else
        mkdir -p ~/.backup/`dirname $1`;
        cp $1 ~/.backup/$1-`date +%Y%m%d%H%M`.backup;
    fi
}


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.

Code:
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
/home/brion

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.


Top
 Profile WWW  
 PostPosted: Mon May 17, 2010 1:54 pm   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
It would be great to make it also as a backup utility for directories as well.

bud


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP