You are not logged in.

#1 2007-01-16 06:10:26

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

a .bashrc function to make quick backups of files.

Since I'm always messing with the config files, being somewhat new to ArchLinux, I was wanting an easy way to make intelligent backup copies of my files ... QUICKLY.

To the rescue, the .bashrc file

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 ? smile  )  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 wink  The command is simply bu followed by either the full path and filename, or if your already in the files directory, just bu FILENAME

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

[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....... wink   Makes it easy to keep track of when you made a backup and makes it easy to do too. wink

Want to remove all the .backup files ??? 

rm -f *.backup

  ( I suppose you could alias that as well if you really wanted too lol )..... make sure nothing else is labeling their backups with the same .backup extension or you might erase something you didn't intend too ! wink  Hope it comes in handy for someone else besides myself wink

Offline

#2 2007-01-16 06:32:31

twiistedkaos
Member
Registered: 2006-05-20
Posts: 666

Re: a .bashrc function to make quick backups of files.

crouse wrote:

Since I'm always messing with the config files, being somewhat new to ArchLinux, I was wanting an easy way to make intelligent backup copies of my files ... QUICKLY.

To the rescue, the .bashrc file

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 ? smile  )  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 wink  The command is simply bu followed by either the full path and filename, or if your already in the files directory, just bu FILENAME

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

[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....... wink   Makes it easy to keep track of when you made a backup and makes it easy to do too. wink

Want to remove all the .backup files ??? 

rm -f *.backup

  ( I suppose you could alias that as well if you really wanted too lol )..... make sure nothing else is labeling their backups with the same .backup extension or you might erase something you didn't intend too ! wink  Hope it comes in handy for someone else besides myself wink

seems like a nice idea, but useless to me, most editors have a built in backup function that creates a backup everytime you save / edit a file.

Offline

#3 2007-01-16 07:59:55

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: a .bashrc function to make quick backups of files.

interesting ... what about a function to get rid of all the '~' files ...

would rather have backup put key files somewhere safe ie in .backup or /backup

just my thoughts nice function btw  8)

thanks for sharing


Mr Green

Offline

#4 2007-01-16 08:15:43

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: a .bashrc function to make quick backups of files.

Mr Green wrote:

would rather have backup put key files somewhere safe ie in .backup or /backup

just my thoughts nice function btw  8)

thanks for sharing

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.

Offline

#5 2007-01-16 08:40:11

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: a .bashrc function to make quick backups of files.

thanks yes full path to /dir ... emmm if your backing up root files then I would say /backup ...

Thanks for your help  wink


Mr Green

Offline

#6 2007-01-16 17:13:40

skymt
Member
Registered: 2006-11-27
Posts: 443

Re: a .bashrc function to make quick backups of files.

This version should back up to ~/.backup even if the file isn't in the current directory:

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

By the way, this isn't just for bash. It should also work in zsh and (maybe?) ksh.

Offline

Board footer

Powered by FluxBB