DEV Community

Chandu J S
Chandu J S

Posted on • Originally published at chandujs.dev

๐Ÿ—ƒ๏ธ Backup your .dotfiles with a simple bash script

We will be creating a git repository with a script that backup your required dotfiles.

1. Create a folder for the backup



mkdir my-dotfiles


Enter fullscreen mode Exit fullscreen mode

2. Initialize git



git init


Enter fullscreen mode Exit fullscreen mode

3. Create file list

Create a file named backup.conf at the root of the folder.
In this file we need to list out all the files & folders we need to backup. eg:



~/.gitconfig
~/.profile
~/.vimrc
~/.xinitrc
~/.zshrc


Enter fullscreen mode Exit fullscreen mode

Our backup script will loop through the items and copy everything into respective folders.

4. Create script

Create a file named backup.sh at the root of the folder.
This is our script file. Copy the content below and paste it inside the file.



#!/bin/sh

# This script copy files mentioned inside `backup.conf` to the root of the project.

# file to look for the paths to backup.
backupPaths="./backup.conf"
# home directory path.
homeDirectory=~
# same line identifier to echo in the same line.
sameLine="\e[1A\e[K"

echo "๐Ÿ›‘ Clearing configurations directory..."
# removing the folder with exsiting contents. we have git version anyway!
rm -rf configurations
# creating it again for backup.
mkdir configurations
sleep 1
echo -e "$sameLineโœ… Configurations directory cleared."
sleep 1

echo -e "$sameLine๐Ÿ Starting backup..."
sleep 1

# looping through the list & avoiding the empty spaces
sed '/^[ \t]*$/d' $backupPaths | while read filePath; do
  echo -e "$sameLineโณ Copying: $filePath"

  # find & replace for ~ with home path
  findThis="~/"
  replaceWith="$homeDirectory/"
  originalFile="${filePath//${findThis}/${replaceWith}}"

  # copying the files
  cp --parents --recursive $originalFile ./configurations
  sleep 0.05
done

git add .

echo -e "$sameLine๐ŸŽ‰ Backup finished! You can review & commit your changes."


Enter fullscreen mode Exit fullscreen mode

5. Make the script executable



chmod +x ./backup.sh


Enter fullscreen mode Exit fullscreen mode

6. Commit all changes



git add .
git commit -m "initial commit"


Enter fullscreen mode Exit fullscreen mode

7. Run script



./backup.sh


Enter fullscreen mode Exit fullscreen mode

From now on, you can just run the script file whenever you need. After you run the backup script, all the files & folders mentioned inside the backup.conf file will be copied into configurations folder. Commit and push the changes to your remote repository.

๐Ÿ›‘ Make sure you are not adding any confidential files or folders to backup.conf file. eg: .ssh or private keys.

Checkout below repository to see complete code. There is a install.sh script in the root which is made for Arch Linux systems. Feel free to fork the repository and make changes according to your needs.

๐Ÿง Linux Configurations

๐Ÿ’พ Backup Configurations

  • Make the backup.sh file executable with chmod +x ./backup.sh first.
  • Backup configurations by executing backup.sh file.
  • Files & folders that needs to be backed up should be mentioned inside backup.conf file.
  • Once backup is complete, commit the changes.
  • If you want to restore the backup, instead of just copy paste everything back, run the backup script. After that you can use file diff to compare the changes and pick manually.

๐Ÿ’ซ Install Packages

  • Make the install.sh file executable with chmod +x ./install.sh first.
  • Bulk install packages by executing install.sh file.
  • Packages needs to be installed should be listed inside install_third_party.conf & install_trusted.conf files.
  • Services needs to be enabled should be listed inside services.conf file.
  • Its better to restart the PC after script finished running.
  • 3rd party packages are installed through pikaur AUR helper.

โšก๏ธ Notes





Top comments (0)