aboutsummaryrefslogtreecommitdiff
path: root/unix/utils/root.sh
blob: a7807bc73cb608c6b35921f19a3144030a3383fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash

# Created By: srdusr
# Created On: Mon 19 Feb 2025 14:18:00 PM CAT
# Project: Backup and restore system files to/from home extras (system dotfiles) directory

# Dependencies: None
# NOTE: The backups will be stored in the ~/extras directory, preserving the original file structure. Run as sudo or be prompted for password
# Example usage:
#       To backup a specific file: root.sh --backup /some_directory/some_file.conf
#       To restore a specific file: root.sh --restore ~/extras/some_directory/some_file.conf
#       To restore all files: root.sh --restore
#

# Use $SUDO_USER to get the original user when run with sudo, or fall back to the current user if not
BASE_DIR="/home/${SUDO_USER:-$(whoami)}/extras"

if [ "$EUID" -eq 0 ] && [ "$SUDO_USER" = "" ]; then
    echo "You are running this script directly as root, not through sudo!"
    exit 1
fi

if [ "$EUID" -ne 0 ]; then
    echo "Elevating to sudo..."
    exec sudo "$0" "$@" # Re-run the script with sudo
fi

# Create directories if they do not exist
create_directory() {
    local dir=$1
    if [ ! -d "$dir" ]; then
        echo "Creating directory: $dir"
        mkdir -p "$dir"
    else
        echo "Directory already exists: $dir"
    fi
}

# Backup files
backup_to_extras() {
    local src=$1

    # Ensure the file or directory exists
    if [ -e "$src" ]; then
        # Strip the leading / from src to avoid double slashes
        local stripped_src="${src#/}"

        # Determine the destination path
        dest_dir="$BASE_DIR/$(dirname "$stripped_src")" # Get the directory part of the source
        dest_file="$BASE_DIR/$stripped_src"             # Get the full destination file path

        # Debug: Print paths
        echo "Source file: $src"
        echo "Destination directory: $dest_dir"
        echo "Destination file: $dest_file"

        # Create the necessary directories in extras if they don't exist
        create_directory "$dest_dir"

        # Backup the file to extras
        echo "Backing up $src to $dest_file"
        cp -p "$src" "$dest_file"

        # Set permission to user
        chown "$SUDO_USER:$SUDO_USER" "$dest_file"

        echo "Backup of $src completed."
    else
        echo "Error: The file or directory '$src' does not exist."
    fi
}

# Restore files
restore_from_extras() {
    local src=$1

    # Ensure the file or directory exists in extras
    if [ -e "$src" ]; then
        # Strip the leading / from src to avoid double slashes
        local stripped_src="${src#/}"

        # Determine the destination path
        dest_dir="/$(dirname "$stripped_src")" # Get the directory part of the source
        dest_file="/$stripped_src"             # Get the full destination file path

        # Debug: Print paths
        echo "Source file: $src"
        echo "Destination directory: $dest_dir"
        echo "Destination file: $dest_file"

        # Create the necessary directories in the system if they don't exist
        create_directory "$dest_dir"

        # Backup the file if it exists before restoring
        if [ -e "$dest_file" ]; then
            echo "File $dest_file exists, creating a backup..."
            mv "$dest_file" "$dest_file.bak"
            echo "Backup created at $dest_file.bak"
        fi

        # Restore the file from extras
        echo "Restoring $src to $dest_file"
        cp -p "$BASE_DIR/$stripped_src" "$dest_file"

        # Set permissions for the restored file
        chmod 644 "$dest_file"

        echo "Restore of $src completed."
    else
        echo "Error: The file or directory '$src' does not exist in extras."
    fi
}

# Restore all files from extras
restore_all_from_extras() {
    echo "Restoring all files and directories from extras..."

    # Loop over all files and directories in BASE_DIR and restore them
    find "$BASE_DIR" -type f | while read -r file; do
        restore_from_extras "$file"
    done

    echo "Restore completed."
}

# Backup system files based on user input
if [ "$1" == "--backup" ]; then
    if [ "$2" = "" ]; then
        echo "Error: Please specify the file or directory to backup."
        exit 1
    fi

    # Perform the backup
    echo "Backing up system files to extras..."
    backup_to_extras "$2"
    echo "Backup completed."

    # Restore system files based on user input
elif [ "$1" == "--restore" ]; then
    if [ "$2" = "" ]; then
        # If no specific file is provided, restore everything
        restore_all_from_extras
    else
        # Restore a specific file or directory
        echo "Restoring system files from extras..."
        restore_from_extras "$2"
        echo "Restore completed."
    fi

else
    echo "Invalid option. Use '--backup' to backup or '--restore' to restore."
fi