• Linux
  • May 2024
    M T W T F S S
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
  • Meta

config-bak

A little over 4 years ago, I wrote a script to backup my configuration files in case I made a change to them that didn’t work out or accidentally deleted them. My first rendition of the script was quite linear and consisted individual blocks of code for each file I was backing up and it grew to be quite large. But it worked, and I used it in that for for a couple of years. Later on, I put each of those individual routines into functions. It was still quite linear. Recently, I reviewed the script and noticed that most of these functions were identical, the only variations were the configuration files themselves.

After taking a close look at the code, I determined that, with only a few exceptions, most of the files to be backed up were either in the root of my home directory or under the .config directory. I created functions to back up files for each case. There were still some exceptions, such as configuratins that might have different names or locations, depending ont the version, desktop environment, or operating system. I wrote functions for those special cases. Now the script would call a more generic function and pass the path and file name to it, or one of the specific functions for the special cases.

Then I started seeing similarities in the special cases and figured that in most of them, I could use the generic functions using the partiuluar parameters for each case. That left only a small handful of files that didn’t fit either generic case. I have a program whose configuration file is in a hidden folder in the root of the home directory and another file that’s a couple levels down in my .local directory. For these special cases, I created another function that places the backup file directly in the backup folder without any directory name.

Finally, there was my dump of my Cinnamon keybindings which I keep in my .config folder. It’s not a configuration file, per se, but it’s important enough to keep a backup. It’s really the only “special case” file I currently have, so it has it own function to handle it. For the most part, it operates much the same as the other functions, but if the system is running the Cinnamon desktop enviroment, and keybinding dump doesn’t exist in the .config folder, it will create the dump file and copy it to the backup folder.

Over time, I’ve improved the appearance of the script’s output. As the script backs up the new or changed files, it displays the pertinent path and the filename, precede by an ASCII arrow (===>). It looks nice and it lets me know what’s been backed up.

Of course, there is a companion script to restore the configuration files from the local backup. Now that I’ve stremlined the backup script, I’m wondering if I can make the restoration script more modular, eliminating many of the individual functions. A precursory look at the restoration script seems to indicate that I can model it after the backup script. That’s a project for the near future.

Script work

What have I been doing in the last 6 months? Mostly, I’ve been scripting. I’ve written a few new scripts, and modified or tweaked nearly all of my earlier scripts. To paraphrase the poet, Paul Valéry, “A script is never finished, it’s only abandoned.”

Yesterday, I reworked my script for customizing some the basic configuration files common to most of my systems. There are three steps in customizing my .bashrc. First, I put some ASCII-art type text at the top to identify myself. Then I run a sed script to change some of the settings, and, finally, I append a few lines to the end to integrate with my Github repositiories and to run utilities like Neofetch.

The original process I came up with for the first task seemed kind of convoluted. I used cat to concatenate my header file and the .bashrc in a temporary file, ran the sed script, and then used cat to append another file to the end. Finally, I renamed the temporary file to .bashrc.

After a couple of hours of online research and testing, I finally found what I hope is a better method. I worked out a sed command to insert the contents of the header file to the beginning of the .bashrc. This worked rather well. The other two steps were unchanged.

Lately, I’ve been using sed files to modify default configuration files for my editors, terminals, and other applications. For some app installations I use here-docs in the installation scripts, especially if there’s no default config file.

In the past few months I’ve also been incorporating getopts into many of my installation scripts in order to easily use command line arguments to select options and what actions the script takes. I’ve made use of that functionality in some utility scripts as well. To make it easier to write new scripts using getopts, I created a template file.

Tinkering

I’ve been doing a lot of tinkering lately, mostly with scripts., but there have been a couple side projects. I created antiX and MX Linux VMs in Gnome Boxes. I haven’t had any luck in getting virt-manager to work even though they use mostly the same dependencies. Gnome Boxes works well for what I do with VMs but I would like to explore the options I would get from virt-manger.

As I’ve said, I’ve mostly been messing around with scripts. I decided to push a couple more scripts to my GitHub repository. I have a script to rename the hostname on a system. It changes it in /etc/hostname and in /etc/hosts, then checks to make sure they’ve been successfully changed and they are identical. The other script sets the RESUME environmental variable. Occasionally during an update I’ll get an error message stating that the initramfs will attempt to resume from /dev/sdax and display the UUID of the swap partition. The scripts extracts the correct UUID and sets the RESUME variable. I’ve needed to run this script several times, so I though it might be useful enough to share.

I went through my installation scripts and for many of those that use PPAs, I created functions to install and to completely remove the programs, to include the repository information in /etc/apt/sources.d. Recently, I made some modifications to the script I use to install and update the Bat utility. The script is completely modular with each portion of the script contained in its own function. There were a couple updates to the utility recently and my systems updated flawlessly. Now I’m considering some color into the the displayed information.

The script that installs my modified configuration files to new systems got a bit of an overhaul. I did some testing with my Debian-based systems to find a way to have my `~/bin` directory appear in the PATH. I had been adding it to the PATH in the .bashrc but when I’d log in through a TTY or in an SSH session, it would appear twice because it would be called by both the .profile and the .bash rc.

I sort of got around that by having my new-config script copy a .bash_proile to the home directory if it was a Debian-based distribution. The file sources the .profile. This worked quite will for LMDE. The results on my laptop running Debian Buster were mixed. On this laptop I have the choice of four terminal emulators. XTerm, UXTerm, and Thai XTerm were installed out of the box and I added Terminator. Only in Thai XTerm was the .profile sourced. Neither XTerm nor UXTerm showed my private bin directory in the PATH. In Terminator, I had to go into Preferences and set it to open as a login shell. It’s a bit frustrating that the .bash_profile didn’t work exactly has I’d hoped, but I can live with since I rarely use XTerm or UXTerm. I generally use Terminator on this laptop.

The various scripts I have using Youtube-dl were tweaked. I discovered (or rediscovered) some useful options to add the scripts. In the part of the script that selects the “best” format for the download, I filtered out some of the extraneous information to get a less verbose output.

One script that received some major modification was my  `ip-info` script. First, I added code to extract the SSID and signal strength for a wireless connection. Then I made it modular by breaking it into functions. While testing it, I found that a couple system with Broadcom Wifi cards couldn’t display the signal strength. I also noticed that when I ran the script in a TTY on one of those systems, `iwconfig` would produce error messages that I could not redirect to `/dev/null`.

I finally decided that the best way to avoid those errors was to only run the `iwconfig` command if a problem Broadcom card was not present. If there is a WiFi network device, the script gets the field of the lspci output where the model name of a Broadcom card should appear. If that field matches one of the problem cards, it sets the WIFI_SIG variable to “Not available.” Otherwise, the signal strength data is extracted and displayed.

A while back I wrote a script to scan through the first 50 address on my local network and display the addresses that could be pinged. Just having the addresses was not enough so I added code to get the hostnames as well. Then i noticed that Windows machines didn’t show up. After some research, I found a way to get those hostnames. Today I ran the script from some other computers and found I didn’t have all the dependencies installed on them. After more research and testing, I found that my original solution and it’s packages weren’t really necessary. I found another solution that only required a couple packages to be installed and no modification of the `/etc/nsswitch.conf` file. I modified the script to check for the necessary packages and install them if they weren’t already installed. The script now works on all of my Linux systems.

Recently, someone on the EzeeTalk forum suggested that I change my HP Mini-110 Netbook to boot into console mode since I generally only used it for writing scripts. I did that it’s working out well. I’m haven’t been writing scripts but I’ve been using it as an administration console to SSH into my other machines to check system status, run update scripts, and the like. I’m considering replacing the current 160 GB hard drive with a 128 GB SSD I have laying around which will further enhance the performance. As a console, I wouldn’t need much disk space. Well, I’m going to do it, I just haven’t gotten around to it.

My Config Files

Over the past few months, I’ve seen a few ways to keep backups of my important configuration files. The idea of setting up a git bare repository interesting and I’ve seen a couple of ways to implement it but I haven’t done it yet.

What I have done is create a folder in a directory that get synced to all of my Linux computers to hold copies of my configuration files and I wrote a script to back them up to that directory on my main system. I consider the configuration files on my main system to be the master copies. Where there I variations I save them with an appended file name. For instance, I have three different versions of nano on my network, one for Mint 18.3, 19.1, and LMDE 3. Each of them supports different options and syntax highlighting.

Yesterday, I created a script to copy my modified configuration files into my home directory. The script is menu-based so it only copies the files I want. If I need to copy more than one file, it continues to offer me choices until I choose the option to quit.

I wrote functions to handle each configuration file option and made allowances for different versions of a file or special situations. In the function that copies the .nanorc, it checks the version of nano on the computer and copies the appropriate .nanorc file.

At the moment I two systems running Debian-based distributions, Linux Mint Debian Edition and MX-Linux. On both of these systems I’ve had problems with making sure my ~/bin directory was added to my path if I had logged in to the GUI login. Previously, I had gone into the terminal’s preferences and had it run as a login shell.

The function that handles the .bashrc checks /etc/os-release to see if the distribution is based on Debian or Ubuntu. If it’s Debian, after it has copied the .bashrc file, it appends it with two lines to check for the existence of ~/bin and add it to the path if necessary.

I haven’t thoroughly tested it yet but I’m certain it will work properly. I’ll proofread it and I might still tweak it a bit. Sometimes I see things differently when I put it aside for a while.

Conky config page updated

I think I’ve done enough with my conky configuration and installation for now. I successfully installed and configured conky on two Peppermint 7 Linux installations, on a virtual machine on my main desktop and on a PC I’d built a few years ago. I did have to do a little editing of the .conkyrc file on each. In the VM installation, I had to comment out the line for the second CPU core since there was only one core. It displayed an error about trying to access a core that didn’t exist. On the PC, I had to edit the Network section because the system saw the network interface as enp2s6 rather than eth0. The card was a Netgear FA311 Fast Ethernet card. I don’t know if the device name is a peculiarity with the card or with Peppermint.

Another thing I notice with my script in Peppermint was that the time was slightly to the left of center. In my installations with Ubuntu and Mint, it was always perfectly centered. I was able to fix the problem by changing the ${alignc 35} at the beginning of the penultimate line of the script to ${alignc}. I suspect that the 35 was some sort of an offset of some sort. Removing the 35 from the line on a Mint installation moved the time to a bit right of center. That’s something to be aware of, I guess.

I had installed conky on the VM yesterday and when I started the VM today, conky displayed as expected so adding the conky.desktop file to ~/.config/autostart/ is all that’s needed to launch it at boot-up. After installing it on the PC today, I opened the Autostart GUI and confirmed that conky was listed.

Satisfied that the installation commands were correct, I restructured the My Conky Configuration page. Under the Installation heading, I listed the steps to install conky from the command line. I put the commands in bold text. You can highlight and copy to the clipboard. To paste them in the terminal use the Crtl+Shift+v key combination. Before the actual installation steps, I include commands to check for battery, CPU, and network interface information.

I kept some of the old installation information in case anyone wants to do it that way. Be aware that menus may be different depending on the distro you’re using.

After the script itself, I created a Notes heading that includes the notes I’ve made for each update. It’s kind of a changelog, I guess.

I’ve abandoned the idea of creating a bash script for the installation. There are only three commands and it’s easy enough to copy and paste them into the terminal.

In my instructions, I used gedit but you can use whatever editor you feel comfortable with. A graphical editor such as gedit works well if you need to do much cutting and pasting like when you need to change eth0 to something else on several lines. I like to use nano from the command line for small changes.