I'm just posting the script I use to automate setup of my Ubuntu 20.04 to use as hardened repository.
The script will ask you what is the disk you want to format as XFS file system and the password for the account to create.
As it, it can process only 1 disk.
Don't hesitate to say me if something is wrong or can be optimized
Code: Select all
#!/bin/bash
#This script is used to setup Ubuntu 20.04 Repository
#-Update system
#-Install perl and xfsprogs prerequesites
#-Format disk with xfs and size=4096 reflink=1 crc=1
#-Create mount point
#-Mount the disk
#-Update /etc/fstab to auto mount the disk
#-Create new account and assign right on mount point
#-Add account to sudo group
#Variable Initialization
#Error log path
ErrLogFileNamePath=UbuntuSetupErr.log
#Mount point for xfs disk
RepositoryPath=/repoXFS
#User to create and use in veeam as single credentials use
UserName=veeamrepo
## Server update ##
echo "###### Updating paquet list ######" | tee $ErrLogFileNamePath
apt-get update 2>> $ErrLogFileNamePath
echo "###### Updating applications ######" | tee -a $ErrLogFileNamePath
apt-get upgrade -y 2>> $ErrLogFileNamePath
## Prerequesites installation ##
#xfsprogs
echo "###### Installing xfsprogs package ######" | tee -a $ErrLogFileNamePath
apt-get install xfsprogs -y 2>> $ErrLogFileNamePath
#Perls modules
echo "###### Installing perl modules ######" | tee -a $ErrLogFileNamePath
apt-get install libsoap-lite-perl -y 2>> $ErrLogFileNamePath
## Create File system disk ##
#List disk to user
echo "###### Listing disk ######" | tee -a $ErrLogFileNamePath
fdisk -l
#Ask user the disk name to initialize and format
echo "What is the disk name to initialize (ex:sdb)"
read DiskName
#GPT initialization and create partition
echo "###### Initializing disk ######" | tee -a $ErrLogFileNamePath
#g : GPT table
#n : new partition
#w : write partition table
fdisk /dev/$DiskName << EOF 2>> $ErrLogFileNamePath
g
n
w
EOF
#Create XFS filesystem
echo "###### Formatting xfs filesystem ######" | tee -a $ErrLogFileNamePath
mkfs.xfs /dev/${DiskName}1 -b size=4096 -m reflink=1,crc=1 2>> $ErrLogFileNamePath
## Create and manage mount point ##
#Create repository folder
echo "###### Creating $RepositoryPath ######" | tee -a $ErrLogFileNamePath
mkdir $RepositoryPath 2>> $ErrLogFileNamePath
#Modify /etc/fstab to mount disk
echo "###### Updating /etc/fstab to mount /dev/${DiskName}1 ######" | tee -a $ErrLogFileNamePath
echo "/dev/${DiskName}1 $RepositoryPath xfs defaults 0 0" >> /etc/fstab 2>> $ErrLogFileNamePath
#Mount disk
echo "###### Mounting /dev/${DiskName}1 to $RepositoryPath ######" | tee -a $ErrLogFileNamePath
mount /dev/${DiskName}1 2>> $ErrLogFileNamePath
#Create repository veeam service account
echo "###### Creating user $UserName ######" | tee -a $ErrLogFileNamePath
useradd -d /home/$UserName -m $UserName 2>> $ErrLogFileNamePath
#Set password for repository veeam service account
echo "Enter password for $UserName user"
passwd $UserName
#Set owner and permission on mount point
echo "###### Putting $UserName as owner on $RepositoryPath ######" | tee -a $ErrLogFileNamePath
chown $UserName:$UserName $RepositoryPath 2>> $ErrLogFileNamePath
echo "###### Giving permission only to $UserName user on $RepositoryPath (700) ######" | tee -a $ErrLogFileNamePath
chmod 700 $RepositoryPath 2>> $ErrLogFileNamePath
#Add repository veeam service account to sudo group
echo "###### Add $UserName to sudo group ######" | tee -a $ErrLogFileNamePath
usermod -aG sudo $UserName 2>> $ErrLogFileNamePath
echo "###### Open $ErrLogFileNamePath to be sure there is no error and restart the server. ######"
echo "###### Be sure to remove $UserName from sudo group after backup repository registration in Veeam with the command : deluser $UserName sudo ######" | tee -a $ErrLogFileNamePath