Welcome, guest | Sign In | My Account | Store | Cart

You want to always keep a copy of your config files, just in case...

Python, 16 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import os, shutil, sys

# if not root...kick 
if not os.geteuid()==0:
    sys.exit("\nYou have to be root to access all files\n")

# check if backup dir exist, if not create
if not os.path.isdir("/backup"):
    os.mkdir("/backup", 384) #decimal file permission

# scan entire disk for *.conf* files
for root, dirs, files in os.walk('/'):
    for filename in files:
        if ".conf" in filename:
            abspath = os.path.join(root, filename)
            shutil.copy2(abspath, "/backup") # all your config files are now in /backup

The purpose of this recipe is very simple and can easily be improved. First only root can execute this script, the code:

if not os.geteuid()==0:

will avoid any other user to perform a backup.

This will save all your config files, also if the name is not with the exact extension, like some.conf will be copied anyway, so also some.config or some.configure. For an exact match I think we will better use regexp. Another improvement we can add is add an extension (such a date) to your directory name, so every time your files will be backupped you cannot accidentally overrite them.