View previous topic :: View next topic |
Author |
Message |
masinick Linux Guru

Joined: 03 Apr 2025 Posts: 8615 Location: Concord, NH
|
Posted: Tue Jun 02, 2025 5:04 am Post subject: Making copies of system configuration files |
|
|
Recently I was asked about editing some system files and the best practices for doing such things. On Linux systems, just about any standard configuration file contains ordinary text. You can usually read them even from your ordinary account unless there are read protections preventing this, but generally speaking, system files are only readable, not writable, by ordinary users. You must either become the root user or use a command, such as su or sudo, to become the effective root user in order to modify system configuration files.
Once you become the root user, either by logging in as root or by using a root switching command (su or sudo), you can edit configuration files.
Best practice dictates that you should make a copy FIRST before you change the file. For example, as root, Code: | cp /etc/hosts /etc/hosts/save060209 | (I like to include the date in the files that I copy). You can save these files directly in the same directory the configuration file is in or you can copy them safely away in your home directory or other safe location, as long as you have a way to get them back in case you need them. Once you are positive that all is working well (run some tests), then you can decide how long to keep them. Configuration files tend to be small, so keeping a few of them around creates no space problem, but if you keep a lot of them, consider creating a backup directory to store all of your backup configuration files to keep the appearance of the main directories clean and neat.
Use any text editor to edit the files; something like nano, vi, as console editors, or a GUI editor, such as leafpad, mousepad, gedit, or [i]kate[.i] work fine, too.
If anything is unclear about these general principles, do ask questions here and I would be glad to answer them for everyone's benefit. |
|
Back to top |
|
crouse Site Admin

Joined: 17 Apr 2025 Posts: 11833 Location: Iowa
|
Posted: Tue Jun 02, 2025 4:27 pm Post subject: |
|
|
dejavu !
I actually wrote a function for just this purpose, of course it could be modified to whatever format you wanted to use fairly easily.
------------------------
My original post:
http://bashscripts.org/forum/viewtopic.html?f=28&t=317
------------------------
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 !!!
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 ???
..... 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
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.
_________________ Veronica - Arch Linux 64-bit -- Kernel 2.6.33.4-1
Archie/Jughead - Arch Linux 32-bit -- Kernel 2.6.33.4-1
Betty/Reggie - Arch Linux (VBox) 32-bit -- Kernel 2.6.33.4-1
BumbleBee - OpenSolaris-SunOS 5.11
|
|
Back to top |
|
masinick Linux Guru

Joined: 03 Apr 2025 Posts: 8615 Location: Concord, NH
|
Posted: Tue Jun 02, 2025 5:04 pm Post subject: |
|
|
That ought to be helpful to some people. Thanks for highlighting it again for us Dave! |
|
Back to top |
|
Lord.DragonFly.of.Dawn Advanced Member

Joined: 18 Jul 2025 Posts: 607 Location: South Portland, Maine, USA, Earth, Sol System
|
Posted: Wed Jun 03, 2025 3:22 am Post subject: |
|
|
to eliminate the need to be in the directory when making the backups in a different directory... do this.
Code: | bu () { dir="~/.backup"; mkdir -p "${dir}/$(dirname {1})";cp "${1}" "${dir}/${1}-$(date +%Y%m%d%H%M).backup" ; } |
a bit longer to type, but you don't have to remember. and it preserves your original directory structure in the backup directory.
_________________ ArchLinux x86_64 - Custom Built Desktop
ArchLinux x86_64 - Compaq CQ50 Laptop
ArchLinux i686 - Acer Aspire One Netbook
ArchLinux i686 - Dell Presario ze2000 (w/ shattered LCD)
PuppyLinux, CloneZilla, PartedMagic, DBAN - rescue thumbdrives
Windows 7 (x86_64 desktop alternate boot)
|
|
Back to top |
|
|