forked from szepeviktor/debian-server-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone-server-debconf.sh
171 lines (145 loc) · 4.29 KB
/
clone-server-debconf.sh
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
#
# Clone a server by reinstalling packages and copying settings.
#
exit 0
# Save on the "donor"
apt-get install -y debconf-utils
mkdir /root/clone; cd /root/clone/
# Create etc-blacklist.txt from these
echo "/etc/modprobe.d/
/etc/network/interfaces
/etc/fstab
/etc/mdadm/mdadm.conf
/etc/udev/
/etc/hostname
/etc/hosts
/etc/resolv.conf
/etc/mailname
/etc/courier/me" | xargs -I % find % -type f > etc-blacklist.txt
ls /etc/ssh/ssh_host_*_key* >> etc-blacklist.txt
debconf-get-selections > debconf.selections
dpkg --get-selections > packages.selection
tar --exclude-from=etc-blacklist.txt \
-vczf server.tar.gz debconf.selections packages.selection etc-blacklist.txt /etc/*
## Data dirs
# /home
# /root
# /opt
# /srv
# /usr/local
# /var
# /var/lib/mysql
# /var/cache
# /var/mail
# /var/spool
# /var/tmp
# @TODO Recreate dirs - owner, perms
## Special handling dirs
# /var/mail
# /var/lib/mysql
# @TODO Exclude /var/lib/dpkg and /var/lib/apt
# Restore on the "clone"
# Check hardware
clear; fdisk -l /dev/[svx]d?
cat /proc/mdstat
pvdisplay && lvs
ifconfig
# Clean package cache
apt-get update
apt-get upgrade
apt-get clean
rm -vrf /var/lib/apt/lists/*
apt-get clean
apt-get autoremove --purge -y
# Move data
cd
#ssh-keygen -t ecdsa -f /root/.ssh/id_ecdsa -N ""; cat /root/.ssh/id_ecdsa.pub
#scp -P ${PORT} -r ${DONOR}:clone .
cd clone/
tar -vxf server.tar.gz
# Compare kernels
clear; dpkg -l | grep -E "^\S+\s+linux-image"
grep "^linux-image" packages.selection
# Remove systemd??
apt-get update -qq
apt-get install -qq sysvinit-core sysvinit-utils
cp -v /usr/share/sysvinit/inittab /etc/inittab
echo -e 'Package: *systemd*\nPin: origin ""\nPin-Priority: -1' > /etc/apt/preferences.d/systemd.pref
# Schedule removal of systemd
echo "PATH=/usr/sbin:/usr/bin:/sbin:/bin
@reboot root apt-get purge -qq --auto-remove systemd >/dev/null;rm -f /etc/cron.d/withoutsystemd" > /etc/cron.d/withoutsystemd
# Normalize OS
../debian-image-normalize.sh
# Install packages needed for cloning
apt-get install -qq apt-transport-https apt-utils dselect debsums
# Inspect changes in /etc
while read -r F; do diff -u "$F" "${F/etc/root/clone/etc}"; done < etc-blacklist.txt
# Restore /etc
chmod -c 0755 ./etc
mv -vf /etc/ /root/etc && mv -vf ./etc /
# Restore blacklisted files to /etc
xargs -I % cp -vf /root% % < etc-blacklist.txt
# Recreate homes
sed -ne 's/^\(\S\+\):x:1[0-9][0-9][0-9]:.*$/\1/p' /etc/passwd | xargs -n1 mkhomedir_helper
#tar -C /home/ -xvf ./homes.tar.gz
apt-get update -qq
# Process blacklisted files
#alias editor="nano"
# Kernel modules
ls -l /etc/modprobe.d/
# Network
editor /etc/network/interfaces
editor /etc/resolv.conf
# Disk
editor /etc/fstab
editor /etc/mdadm/mdadm.conf
# Devices
ls -l /etc/udev/
# Hostname:
read -r -e -p "Host name? " H
hostname "$H"
echo "$H" > /etc/hostname
echo "$H" > /etc/mailname
editor /etc/hosts
mkdir /etc/courier; echo "$H" > /etc/courier/me
# Check hardware again
clear; fdisk -l /dev/[svx]d?
cat /proc/mdstat
pvdisplay && lvs
ifconfig
# Restore packages
dselect update
clear; debconf-set-selections < debconf.selections
# @FIXME Question type: error
# Reconfigure already installed packages
dpkg-reconfigure -f noninteractive dash
dpkg-reconfigure openssh-server && service ssh restart
dpkg-reconfigure -f noninteractive locales
rm -f /etc/timezone
dpkg-reconfigure -f noninteractive tzdata && service rsyslog restart
#dpkg-reconfigure -f noninteractive unattended-upgrades
# Package blacklist
grep -E "^(open-vm-tools|linux-image|mdadm|lvm|grub|systemd)" packages.selection
dpkg -l | grep -E "^\S+\s+(open-vm-tools|linux-image|mdadm|lvm|grub|systemd)"
# Install packages
dpkg --clear-selections; dpkg --dry-run --set-selections < packages.selection
# Match these with the currently installed packages
editor packages.selection
dpkg --clear-selections; dpkg --set-selections < packages.selection
# Manual install
apt-get dselect-upgrade
apt-get install -f
# Check package intergrity and SSH
debsums -c
systemctl status
dpkg -l | grep "openssh-server" || echo 'no SSH !!!'
netstat -anp | grep "ssh" || echo 'no SSH !!!'
# LOG IN NOW WITHOUT LOGGING OUT
## Restore data dirs and special-handling directories
cd /usr/local/src/
rm -rf debian-server-tools
git clone https://github.com/szepeviktor/debian-server-tools.git
chown -R virtual:virtual /var/mail/*
cat /etc/courier/me
# Check services from server.yml