ubt2204srv-lvm
Ubuntu server 22.04 lvm base installation
v23-04-27This template contains the configuration files needed to run a basic installation of Ubuntu Server 22.04 LTS with LVM storage services.
It is intended to create configuration files for physical or virtual machines when storage and data protection is needed.
Template override parameters
File _values-tpl.yaml
contains template configuration parameters and their default values:
## _values-tpl.yaml## cskygen template default values file#_tplname: ubt2204srv-lvm_tpldescription: Ubuntu server 22.04 LVM base installation_tplversion: 23-04-27## Values to override### Machine related configuration valuesmachine:hostname: ubt2204srv-lvmdomainname: cskylab.netlocaladminusername: koslocaladminpassword: "NoFear21"timezone: "UTC"networkinterface: enp1s0ipaddress: 192.168.82.9netmask: 24gateway4: 192.168.82.1searchdomainnames:- cskylab.netnameservers:- 192.168.82.1## Setup directory where configuration files will be injectedsetupdir: "/etc/csky-setup"systemlocale: "C.UTF-8"systemkeyboard: "us"restic:## Restic password is mandatory to access repositorypassword: 'NoFear21'## Restic repositories can be located in local paths, sftp paths and s3 buckets## Local path example:# repo: '/srv/restic/mydir'## S3 example:# repo: 's3:https://backup.cskylab.net/restic/mydir'## sftp example:repo: 'sftp:kos@hostname.cskylab.net:/media/data/restic/mydir'## S3 Bucket access and secret keys must be specified for S3 located repositoriesaws_access: 'restic_rw'aws_secret: 'iZ6Qpx1WiqmXXoXKxBxhiCMKWCsYOrgZKr'
Prerequisites
- Physical or virtual machine with two or more disks:
- Disk 1: Ubuntu server 22.04 installed up and running.
- Additional disks: Free and ready to be managed by LVM.
- Package
openssh-server
must be installed to allow remote ssh connections.
How-to guides
Inject & Deploy configuration
To install and configure the machine, open a terminal from the machine configuration directory in the management repository, and perform the following configuration steps:
1. SSH Authentication and sudoers file
# Run csinject.sh in [ssh-sudoers] execution mode./csinject.sh -k
NOTE: If IP address has not been previously set in cloud-init or net-config, use
-r IPaddress
until network configuration files are deployed.
This step injects ssh key and sudoers file into the machine.
Required before other configuration options. Its purpose is to allow automated and passwordless logins by using ssh protocol.
If ssh key has not been injected before, you must provide the password for username {{ .machine.localadminusername }}@{{ .machine.hostname }}
twice:
- First one to install ssh key (ssh-copy-id).
- Second one to deploy the sudoers file.
2. Network configuration
# Run csinject.sh to inject & deploy configuration in [net-config] deploy mode./csinject.sh -d -m net-config
NOTE: If IP address has not been previously set in cloud-init or net-config, use
-r IPaddress
until network configuration files are deployed.
This step deploys network configuration files that allow the machine to operate with specific IP address and hostname. Cloud-init configuration will be disabled from the next start.
Reboot is recommended when finished.
3. Install packages, updates and configuration tasks
# Run csinject.sh to inject & deploy configuration in [install] deploy mode./csinject.sh -d -m install
This step performs:
- Package installation
- Updates
- Configuration files deployment
- Configuration tasks
Required to run at least once in order to complete proper configuration. Reboot is recommended when finished.
4. Configuration tasks
# Run csinject.sh to inject & deploy configuration in [config] deploy mode (default)./csinject.sh -d
When configuration needs to be changed, this mode redeploys all configuration files into the machine, executing again all configuration tasks.
5. Connect and operate
# Run csconnect.sh to establish a ssh session with sudoer (admin) user./csconnect.sh
To run scripts and operate from inside the machine, establish an ssh connection with administrator (sudoer) user name {{ .machine.localadminusername }}@{{ .machine.hostname }}
.
Storage services
Manage disk volume groups
Usage:
sudo cs-volgroup.sh -h
List status:
sudo cs-volgroup.sh -l
Create volume group + LVM thin pool:
# Create volume group with default variable valuessudo cs-volgroup.sh -m create
If only one volume group is going to be created, you can edit the script and update in section Variable initialization
the values for volume group name vg_name
and block device names block_dev_names
.
Otherwise, you can specify these values in the following way:
# Create volume group with name "ssd"# from disk devices "/dev/sdb /dev/sdc /dev/sdd"sudo cs-volgroup.sh -m create -d "/dev/sdb /dev/sdc /dev/sdd" -v ssd
Delete volume group + LVM thin pool:
# Delete volume group with default variable valuessudo cs-volgroup.sh -m delete# Delete volume group with name "ssd"# from disk devices "/dev/sdb /dev/sdc /dev/sdd"sudo cs-volgroup.sh -m delete -d "/dev/sdb /dev/sdc /dev/sdd" -v ssd
Before deleting volume groups, you must delete the LV's inside.
Manage Thin Provisioning LVM data services
Usage:
sudo cs-lvmserv.sh -h
List status:
sudo cs-lvmserv.sh -l
Create LV data service (Thin logical volume + directory mount):
# Create thin logical volume data service "/srv/mydir"# in default volume group.sudo cs-lvmserv.sh -m create -d /srv/mydir
Create snapshot:
# Create snapshot of data service "/srv/mydir"# in default volume group.# Snapshot will be mounted read-only in /tmp/mydir_snapsudo cs-lvmserv.sh -m snap-create -d /srv/mydir
Remove snapshot:
# Remove snapshot of data service "/srv/mydir"# in default volume groupsudo cs-lvmserv.sh -m snap-remove -d /srv/mydir
Merge snapshot (Rollback to snapshot status):
# Merge snapshot of data service "/srv/mydir"# in default volume groupsudo cs-lvmserv.sh -m snap-merge -d /srv/mydir
Delete LV data service:
# Delete thin logical volume data service "/srv/mydir"# in default volume group.sudo cs-lvmserv.sh -m delete -d /srv/mydir
Free space of unused blocks inside thin-pools:
# Free space inside thin-poolssudo cs-lvmserv.sh -m trim-space
Repair LVM thin-pool
Unmount all LVM in the volume group
List LVM to see vg and lv names
sudo lvscan
- Deactivate volume group
sudo lvchange -an vg
- Repair thin-pool
sudo lvconvert --repair vg/tpool
- Activate volume group
sudo lvchange -ay vg
- Mount LVM and check data
To learn more see the following procedure: https://smileusd.github.io/2018/10/12/repair-thinpool/
Rsync data replication
Note: Prior to operate rsync with a remote host, you must insert the root public key for ssh authentication and passwordless login as sudoer user in the remote host. From a console inside the machine you must run
sudo ssh-copy-id {{ .machine.localadminusername }}@hostname.domain.com
Usage:
sudo cs-rsync.sh -h
RSync data from local directory TO remote directory (snapshot automatically created and removed):
# RSync data service "/srv/mydir"# TO same remote directory at host "hostname.cskylab.net"sudo cs-rsync.sh -m rsync-to -d /srv/mydir \-t hostname.cskylab.net
RSync data FROM remote directory to local directory:
# RSync data service "/srv/mydir"# FROM remote directory "/srv/mydir" at host "hostname.cskylab.net"sudo cs-rsync.sh -m rsync-from -d /srv/mydir \-t hostname.cskylab.net
Restic data backup and restore
The script cs-restic.sh
is designed as a wrapper to execute restic in LVM data services with snapshot operations.
Usage:
sudo cs-restic.sh -h
Credentials for default Restic environment and S3 bucket if used, are stored in the following variables:
# restic-environmentexport RESTIC_REPOSITORY="{{ .restic.repo }}"export RESTIC_PASSWORD="{{ .restic.password }}"export AWS_ACCESS_KEY_ID="{{ .restic.aws_access }}"export AWS_SECRET_ACCESS_KEY="{{ .restic.aws_secret }}"
Note: Prior to operate restic with a remote host in sftp mode, you must insert the root public key for ssh authentication and passwordless login as sudoer user in the remote host. From a console inside the machine you must run
sudo ssh-copy-id {{ .machine.localadminusername }}@hostname.domain.com
Create repository:
To create Restic repository (Directory must exist):
# Create repositorysudo cs-restic.sh -m repo-init -r "{{ .restic.repo }}"
Backup LVM data service:
Data services must have been previously created with cs-lvmserv.sh
. When making backup of a data service, the script automatically creates a snapshot and removes it when finished.
To backup a data service directory:
# Backup data service /srv/mydirsudo cs-restic.sh -m restic-bck -d /srv/mydir \-r "{{ .restic.repo }}"
List snapshots in repository:
To list snapshots in a repository:
# List snapshots in repositorysudo cs-restic.sh -m restic-list -r "{{ .restic.repo }}"
Restore snapshot:
Data service directory must be empty. Otherwise you should use restic-mount and rsync data from mount point.
Restic restore does not show progress information in console. To see progress, open another terminal and run sudo du -sh /srv/mydir
To restore from specific snapshot E.G.:(2a3dff53):
# Restore from specific snapshotsudo cs-restic.sh -m restic-res 2a3dff53 -d /srv/mydir \-r "{{ .restic.repo }}"
Mount restic repository snapshot:
Restic repository can be locally mounted, by default in /tmp/restic
directory, in order to manually explore and copy files or to use the script cs-rsync.sh -m rsync-from
to synchronize data from any snapshot in the repository to local data service directory.
To mount restic repository:
# Mount repository in local directory /tmp/resticsudo cs-restic.sh -m restic-mount -r "{{ .restic.repo }}"
After a restic-mount operation, console will be freezed while repository is mounted. To access the contents of the repository, open another terminal and refer to the locally mounted directory /tmp/restic
Use of cs-rsync from mount point:
You can use the script cs-rsync.sh
to restore a complete snapshot from mount point.
Examples:
# RSync data service "/srv/mydir"# FROM Restic snapshot /tmp/restic/snapshots/latestsudo cs-rsync.sh -m rsync-from -r "/tmp/restic/snapshots/latest" \-d /srv/mydir# RSync data service "/srv/mydir"# FROM Restic snapshot /tmp/restic/snapshots/2021-04-03T21\:39\:08+02\:00sudo cs-rsync.sh -m rsync-from -r "/tmp/restic/snapshots/2021-04-03T21\:39\:08+02\:00" \-d /srv/mydir
Maintain Restic repository with forget option:
The default forget option provided in the script is --keep-hourly 6 --keep-daily 31 --keep-weekly 5 --keep-monthly 13 --keep-yearly 10
. You can provide a specific forget option (-f) with an specific tag (-t).
It's not recommended to run restic prune with regular backups, since it can be a time-consuming process and it should be planned in a way that it doesn’t interfere with regular backups.
# Forget snapshots in repository with default forget option and tag mydirsudo cs-restic.sh -m restic-forget -t "mydir" -r "{{ .restic.repo }}"
Utilities
Passwords and secrets
Generate passwords and secrets with:
# Screenecho $(head -c 512 /dev/urandom | LC_ALL=C tr -cd 'a-zA-Z0-9' | head -c 16)# File (without newline)printf $(head -c 512 /dev/urandom | LC_ALL=C tr -cd 'a-zA-Z0-9' | head -c 16) > RESTIC-PASS.txt
Change the parameter head -c 16
according with the desired length of the secret.
Abridged ‘find’ command examples
When looking for files or directories, you can use the find
command like in the following examples:
# Basic case-insensitive commands# -------------------------------find /tmp/restic/snapshots/latest/ -type f -iname "*foo*" # find files under latest snapshotfind /tmp/restic/snapshots/latest/ -type d -iname "*foo*" # find directories under latest snapshotfind /tmp/restic/snapshots/ -type f -name "foo.txt" # find a specific file in all snapshots# find files with different extensions# ------------------------------------find . -type f \( -name "*.c" -o -name "*.sh" \) # *.c and *.sh filesfind . -type f \( -name "*cache" -o -name "*xml" -o -name "*html" \) # three patterns# find files that don't match a pattern (-not)# --------------------------------------------find . -type f -not -name "*.html" # find all files not ending in ".html"# Execute ls -lah with files found# --------------------------------find $PWD -type f -iname "*foo*" -exec ls -lah {} \;# find files bigger than a size# -----------------------------find $PWD -type f -size +4G -exec ls -lah {} \; # find all files bigger than 4GB
USB disk operations
Disk formatting and partitioning:
- Plug USB disk and find the block device:
# List all disk devices and partitionssudo fdisk -l
- Execute fdisk in interactive mode:
# fdisk interactive mode (Example for device /dev/sdc)sudo fdisk /dev/sdcWelcome to fdisk (util-linux 2.34).Changes will remain in memory only, until you decide to write them.Be careful before using the write command.Command (m for help):
- Enter
m
to get a list of all available commands:
Command (m for help): mHelp:GPTM enter protective/hybrid MBRGenericd delete a partitionF list free unpartitioned spacel list known partition typesn add a new partitionp print the partition tablet change a partition typev verify the partition tablei print information about a partitionMiscm print this menux extra functionality (experts only)ScriptI load disk layout from sfdisk script fileO dump disk layout to sfdisk script fileSave & Exitw write table to disk and exitq quit without saving changesCreate a new labelg create a new empty GPT partition tableG create a new empty SGI (IRIX) partition tableo create a new empty DOS partition tables create a new empty Sun partition tableCommand (m for help):
- Enter
g
to create a new empty GPT partition table:
Command (m for help): gCreated a new GPT disklabel (GUID: FB0E06DB-479A-C842-A775-657D4D52BA41).Command (m for help):
- Enter
n
to create a new partition and accept default values:
Command (m for help): nPartition number (1-128, default 1):First sector (2048-62545886, default 2048):Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-62545886, default 62545886):Created a new partition 1 of type 'Linux filesystem' and of size 29.8 GiB.Command (m for help):
Note: You can change the partition type if needed, using option
t change a partition type
. (Example:11 Microsoft basic data
to supportexfat
file system.)
- Enter
p
to display the new partition table:
Command (m for help): pDisk /dev/sdc: 29.84 GiB, 32023511040 bytes, 62545920 sectorsDisk model: USB Flash DriveUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: gptDisk identifier: FB0E06DB-479A-C842-A775-657D4D52BA41Device Start End Sectors Size Type/dev/sdc1 2048 62545886 62543839 29.8G Linux filesystemCommand (m for help):
- Enter
w
to save the changes:
Command (m for help): wThe partition table has been altered.Calling ioctl() to re-read partition table.Syncing disks.
Format partition & create file system:
- To format partition and create the file system, run:
# Format partition & create file system (Example for partition /dev/sdc1)sudo mkfs.ext4 -L USBDisk /dev/sdc1
Note: You can use analog commands (i.e., mkfs.exfat or mkfs.ntfs) to create other types of file systems.
Repair file system:
You can use the fsck
command to repair corrupted file systems.
- To check and repair the filesystem, unmount the partition and run:
# Check and repair the file system (Example for partition /dev/sdc1)sudo fsck.ext4 /dev/sdc1
Note: You can use analog commands (i.e., fsck.exfat or fsck.ntfs) to check or repair other types of file systems.
USB Disk mount:
Disk must be previously partitioned and file system created.
To mount a USB disk in /media
directory:
- Find the disk and its UUID
# List all disk devices and partitionssudo fdisk -l# Find disk UUIDsudo blkid
Output example for /dev/sdc1:
Output/dev/XXX: xxxxxxxxxxxxxxxxxxxxxxxxx/dev/sdc1: UUID="74530e5a-836c-4cf1-963a-2743b296349b" TYPE="ext4" PARTUUID="e7d880eb-f6a6-8648-bb80-21d8dc72b6e0"
- To mount interactively for the current session use the mount command (Example for /dev/sdc1):
# Mount disk interactivelysudo mount /dev/sdc1 /media
- To mount at system startup if present, you must update /etc/fstab in the following way (Example for UUID=...):
# Update fstab to mount disk at startupsudo echo "UUID=74530e5a-836c-4cf1-963a-2743b296349b /media auto nosuid,nodev,nofail 0 0" | sudo tee -a /etc/fstab# Mount interactively if not mounted at startupsudo mount -a
Create data directory:
Note: As a best practice, you should create a data directory with sudoer user ownership for remote access, in the mounted file system to be referenced in scripts instead of the mounting point. (i.e. You should reference
/media/data
instead of/media
).
- To create data directory and set ownership:
# Create data directorysudo mkdir /media/datasudo chown -R {{ .machine.localadminusername }}:{{ .machine.localadminusername }} /media/data
Create data directory:
Note: As a best practice, you should create a data directory with sudoer user ownership for remote access, in the mounted file system to be referenced in scripts instead of the mounting point. (i.e. You should reference
/media/data
instead of/media
).
- To create data directory and set ownership:
# Create data directorysudo mkdir /media/datasudo chown -R {{ .machine.localadminusername }}:{{ .machine.localadminusername }} /media/data
USB Disk unmount:
- To unmount from
/media
and get the disk ready to unplug:
# Unmount disk interactivelysudo umount /media
Reference
Scripts
cs-volgroup
Purpose:Disk volume group management.Use this script to create and delete volume groupsfrom one or several disk devices.Each volume group will have a thin-pool associated to holddata services that will be created with the script "cs-lvmserv.sh".Usage:sudo cs-volgroup.sh [-l] [-m <execution_mode>] [-d <block_dev_names>][-v <vg_name>] [-h] [-q]Execution modes:-l [list-status] - List volume group and disk status.-m <execution_mode> - Valid modes are:[create] - Create volume group and thin pool.[delete] - Delete volume group and erase disks.Options and arguments:-d <block_dev_names> - Disk device names to be added into volume group.(Array quoted list and space separated values)(Optional. Default value)-v <vg_name> - Volume group name. (Optional. Default value)-h Help-q Quiet (Nonstop) execution.Examples:# List current statussudo cs-volgroup.sh -l# Create volume group with default variable valuessudo cs-volgroup.sh -m create# create volume group with name "ssd"# from disk devices "/dev/sdb /dev/sdc /dev/sdd"sudo cs-volgroup.sh -m create -d "/dev/sdb /dev/sdc /dev/sdd" -v ssd
Tasks performed:
${execution_mode} | Tasks | Block / Description |
---|---|---|
[create] | Create volume group | |
Prepare devices | Wipe signatures and erase partition tables for all specified devices. | |
Create physical volumes | Create physical volumes for all specified devices. | |
Create volume group | Create volume group with all specified devices. | |
Create thin-pool | Create a unique thin-pool in volume group to hold thin volumes. | |
[delete] | Delete volume group | |
Delete Volume Group | Delete volume group and all logical volumes inside. | |
Wipe devices | Wipe signatures and erase partition tables for all specified devices. | |
Update /etc/fstab | Remove mounts for all logical volumes in volume group. | |
[list-status] | Display disk and volume group status | |
Display status | Disks, physical volumes (PV's) and volume groups (VG's) status. |
cs-lvmserv
Purpose:LVM data services.Use this script to create or delete directory data services with independentthin logical volumes suitable for snapshot operations.A volume group and thin-pool must have been created before using thescript "cs-volgroup.sh".Usage:sudo cs-lvmserv.sh [-l] [-m <execution_mode>][-d <data_path>] [-v <vg_name>] [-h] [-q]Execution modes:-l [list-status] - List status of existing logical volumes.-m <execution_mode> - Valid modes are:[create] - Create data service.(Thin logical volume + directory mount)[delete] - Delete data service.(Thin logical volume + directory mount)[snap-create] - Create snapshot and mount to read-only directory.[snap-remove] - Remove snapshot and unmount from read-only directory.[snap-merge] - Roll back logical volume to snapshot statusand discard all changes.[trim-space] - Free space inside thin-poolsdiscarding unused blocks on all mounted filesystems.Options and arguments:-d <data_path> - Data service directory path.(Thin LV will be mounted on it)-v <vg_name> - Volume group name. (Default value)-h Help-q Quiet (Nonstop) execution.Examples:# List logical volume statussudo cs-lvmserv.sh -l# Create thin logical volume data service "/srv/mydir"# in default volume group.sudo cs-lvmserv.sh -m create -d /srv/mydir# Create snapshot of data service "/srv/mydir"# in default volume group.# Snapshot will be mounted read-only in /tmp/gitlab-postgresql_snapsudo cs-lvmserv.sh -m snap-create -d /srv/mydir# Remove snapshot of data service "/srv/mydir"# in default volume groupsudo cs-lvmserv.sh -m snap-remove -d /srv/mydir# Merge snapshot of data service "/srv/mydir"# in default volume groupsudo cs-lvmserv.sh -m snap-merge -d /srv/mydir# Delete thin logical volume data service "/srv/mydir"# in default volume groupsudo cs-lvmserv.sh -m delete -d /srv/mydir# Free space inside thin-poolssudo cs-lvmserv.sh -m trim-space
Tasks performed:
${execution_mode} | Tasks | Block / Description |
---|---|---|
[create] | Create LVM data service | |
Preserve original /etc/fstab | File /etc/fstab is copied to /etc/fstab.naked if not exist. | |
Create thin logical volume | Create thin logical volume in volume group exclusive for the data service. | |
The whole thin pool volume size will be assigned for every thin LV data service. | ||
Create filesystem | Format the filesystem as specified in command_to_format data_path` option. | |
Mount logical volume | Create directory and mount data service as specified in data_path option. (Typically /srv/xxx). | |
If directory exists previously, the content will be copied to LVM before mount operation. | ||
Update /etc/fstab | Add mount line to file /etc/fstab and mount all. | |
Set directory ownership | Change ownership as specified by user_owner and group_owner variables. | |
If directory exists previously, ownership will be preserved. | ||
[delete] | Delete LVM data service | |
Unmount logical volume | If mounted, unmount logical volume from directory specified in data_path option. | |
Delete thin logical volume | Delete thin logical volume from volume group. | |
Update /etc/fstab | Remove mount line in file /etc/fstab . | |
Delete service directory | If empty, remove directory specified in data_path option. | |
[snap-create] | Create snapshot | |
Create snapshot | Create and activate thin snapshot. | |
Mount snapshot | Mount snapshot read-only mode in directory /mnt/<dataservice>_snap . | |
[snap-remove] | Remove snapshot | |
Unmount snapshot | If mounted, unmount snapshot from /mnt/<dataservice>_snap . | |
Remove snapshot | Delete thin snapshot from volume group. | |
Delete snapshot directory | If empty, remove /mnt/<dataservice>_snap directory | |
[snap-merge] | Merge snapshot | |
Merge Snapshot | Roll back logical volume to snapshot status. | |
Merge will be delayed until next volume activation. | ||
Unmount snapshot | If mounted, unmount snapshot from /mnt/<dataservice>_snap . | |
Unmount logical volume | If mounted, unmount logical volume from data_path (Typically /srv/xxx). | |
Deactivate and activate logical volume | Reactivate logical volume to complete merge and remove snapshot. | |
Mount /etc/fstab volumes | Mount all /etc/fstab volumes. | |
Delete snapshot directory | If empty, remove /mnt/<dataservice>_snap . | |
[trim-space] | Trim filesystem | |
Trim all filesystems | Discard unused blocks on all mounted filesystems (fstrim -a). | |
[list-status] | Display LVM and filesystem status | |
[ListStatus] | List Status | List logical volumes and filesystems status. |
cs-rsync
Note: Prior to operate rsync with a remote host, you must insert the root public key for ssh authentication and passwordless login as sudoer user in the remote host. From a console inside the machine you must run
sudo ssh-copy-id {{ .machine.localadminusername }}@hostname.domain.com
Purpose:RSync copies for LVM data services.Use this script to perform RSync operations with data services supported bythin-logical volumes created with the script "cs-lvmserv.sh".Snapshots will be created automatically for rsync-to copiesand removed when finished.Usage:sudo cs-rsync.sh [-m <execution_mode>] [-d <data_path>] [-v <vg_name>][-r <remote_data_path>] [-t <hostname.domain.com>] [-h] [-q]Execution modes:-m <execution_mode> - Valid modes are:[rsync-to] - RSync data from local directory TO remote directory.[rsync-from] - RSync data FROM remote directory to local directory.Options and arguments:-d <data_path> - Local data service directory path.-v <vg_name> - Volume group name. (Default value)-r <remote_data_path> - Remote directory path. (Default is same as local)-t <hostname.domain.com> - Backup host (Default value)-h Help-q Quiet (Nonstop) execution.Examples:# RSync data service "/srv/mydir"# TO same remote directory at host "hostname.cskylab.net"sudo cs-rsync.sh -m rsync-to -d /srv/mydir \-t hostname.cskylab.net# RSync data service "/srv/mydir"# FROM remote directory "/srv/mydir" at host "hostname.cskylab.net"sudo cs-rsync.sh -m rsync-from -d /srv/mydir \-t hostname.cskylab.net
Tasks performed:
${execution_mode} | Tasks | Block / Description |
---|---|---|
[rsync-to] | RSync data TO remote directory | |
Create Snapshot | Create snapshot from data service LV and mount it in /mnt/<dataservice>_snap | |
Execute rsync TO | RSync data from snapshot TO directory specified in remote_data_path option. | |
Remove Snapshot | Remove snapshot and mount directory when rsync has finished. | |
[rsync-from] | RSync data FROM remote directory | |
Execute rsync FROM | RSync data FROM directory specified in remote_data_path option. |
cs-restic
Purpose:Restic backup for LVM data services.Use this script to perform Restic operations with data services supported bythin-logical volumes created with the script "cs-lvmserv.sh".Snapshots will be created automatically for backups and removed when finished.Usage:sudo cs-restic.sh [-m <execution_mode>] [-d <data_path>] [-v <vg_name>] [-t <tag_name>][-r <RESTIC_REPOSITORY>] [-i <restic_snap_id>][-p <RESTIC_PASSWORD>] [-a <AWS_ACCESS_KEY_ID>][-k <AWS_SECRET_ACCESS_KEY>] [-f <forget_options>] [-h] [-q]Execution modes:-m <execution_mode> - Valid modes are:[restic-bck] - Backup to local or remote repository.[restic-list] - List snapshots in repository.[restic-mount] - Mount repository to /mnt directory.[restic-res] - Restore from repository to directory data service.(Directory must be empty).[repo-init] - Initialize repository (Directory or bucket must exist).[restic-forget] - Maintain repository and remove snapshots with forget option.Options and arguments:-d <data_path> - Local data service directory path. (Mandatory)-v <vg_name> - Volume group name. (Default value)-t <tag_name> - Tag name for the snapshot. (Default value)-r <RESTIC_REPOSITORY> - Restic repository (Default value)-i <restic_snap_id> - Snapshot ID to restore (Default latest)-p <RESTIC_PASSWORD> - Restic password (Default value)-a <AWS_ACCESS_KEY_ID> - S3 access key (Default value)-k <AWS_SECRET_ACCESS_KEY> - S3 secret key (Default value)-f <forget_options> - Options to execute (Default value)-h Help-q Quiet (Nonstop) execution.Examples:# Backup data service "/srv/mydir"# TO local repository "/srv/restic/gitlab-postgresql/"sudo cs-restic.sh -m restic-bck -d /srv/mydir \-r "/srv/restic/gitlab-postgresql/"# Backup data service "/srv/mydir"# TO remote sftp repository "sftp:kos@backup.cskylab.com:/srv/restic/gitlab-postgresql/"sudo cs-restic.sh -m restic-bck -d /srv/mydir \-r "sftp:kos@backup.cskylab.com:/srv/restic/gitlab-postgresql/"# Backup data service "/srv/mydir" tagged "gitlab-postgresql"# TO shared repository in MinIO bucket "s3:https://minio-standalone.cskylab.com/restic-test/"sudo cs-restic.sh -m restic-bck -d /srv/mydir \-t gitlab-postgresql -r "s3:https://minio-standalone.cskylab.com/restic-test/"# List snapshots in repository "s3:https://minio-standalone.cskylab.com/restic-test/"sudo cs-restic.sh -m restic-list -r "s3:https://minio-standalone.cskylab.com/restic-test/"# Restore data service "/srv/mydir"# FROM latest snapshot in repository "/srv/restic/gitlab-postgresql"sudo cs-restic.sh -m restic-res -d /srv/mydir \-r "/srv/restic/gitlab-postgresql"# Create repository in directory "/srv/restic/gitlab-postgresql"sudo cs-restic.sh -m repo-init -r "/srv/restic/gitlab-postgresql"# Maintain repository "/srv/restic/gitlab-postgresql" applying host, tag and forget optionssudo cs-restic.sh -m restic-forget -r "/srv/bck/gitlab-postgresql" \-t "gitlab-postgresql" -f "--keep-hourly 6 --keep-daily 31 --keep-weekly 5 --keep-monthly 13 --keep-yearly 10"# Mount repository "/srv/bck/gitlab-postgresql"# in /mnt directorysudo cs-restic.sh -m restic-mount -r "s3:https://minio-standalone.cskylab.com/restic-test/"
Tasks performed:
${execution_mode} | Tasks | Block / Description |
---|---|---|
[restic-bck][restic-res] | Set restic ssh config parameters | |
Prepare ssh config parameters | Generate file /root/.ssh/config with appropriate parameters for restic sftp connections. | |
[restic-bck] | Restic backup | |
Create Snapshot | Create snapshot from data service LV and mount it to /mnt/<dataservice>_snap | |
Execute Restic backup | Backup data from snapshot to restic repository specified in RESTIC_REPOSITORY variable. | |
Remove Snapshot | Unmount and remove snapshot when Restic backup is finished. | |
[restic-forget] | Restic repository maintenance | |
Maintain Restic repository | Maintain Restic repository with policy defined in forget_options variable. | |
[restic-list][restic-bck] [restic-forget] | Display snapshots in Restic repository | |
List snapshots in repository | List snapshots and display statistics for the repository. | |
[restic-res] | Restic restore | |
Execute Restic restore | Restore data from snapshot to data service directory (must be empty) specified in data_path option. | |
[repo-init] | Create Restic repository | |
Initialize Restic repository | Initialize Restic repository in directory (Must be empty) specified in RESTIC_REPOSITORY variable. | |
[restic-bck][restic-forget] [repo-init] | Change ownership of local restic repository | |
Change repository ownership | If RESTIC_REPOSITORY is a local directory, change ownership to local sudoer. | |
[restic-mount] | Restic repository maintenance | |
Mount Restic repository | Mount Restic repository via FUSE in directory specified in restic_mount_point (Default /tmp/restic ). |
cs-deploy
Purpose:Machine installation and configuration deployment.This script is usually called by csinject.sh when executing Inject & Deployoperations. Exceptionally, it can also be run manually from inside the machine.Usage:sudo cs-deploy.sh [-m <execution_mode>] [-h] [-q]Execution modes:-m <execution_mode> - Valid modes are:[net-config] - Network configuration. (Reboot when finished).[install] - Package installation, updates and configuration tasks (Reboot when finished).[config] - Redeploy config files and perform configuration tasks (Default mode).Options and arguments:-h Help-q Quiet (Nonstop) execution.Examples:# Deploy configuration in [net-config] mode:sudo cs-deploy.sh -m net-config# Deploy configuration in [install] mode:sudo cs-deploy.sh -m install# Deploy configuration in [config] mode:sudo cs-deploy.sh -m config
Tasks performed:
${execution_mode} | Tasks | Block / Description |
---|---|---|
[net-config] | Network configuration | |
Deploy /etc/hostname | Configuration file hostname must exist in setup directory. | |
Deploy /etc/hosts | Configuration file hosts must exist in setup directory. | |
Deploy /etc/netplan/01-netcfg.yaml | Configuration file 01-netcfg.yaml must exist in setup directory. | |
Disable cloud-init | Flag that signals that cloud-init should not run. | |
Change systemd-resolved | Change configuration of file /etc/resolv.conf . | |
Try Netplan configuration | Execute netplan try to test new network configuration. | |
Reboot | Reboot with confirmation message. | |
[install] | Install and update packages | |
Update installed packages | Update package repositories, perform dist-upgrade and autoremove | |
Generate locales | Deploy file locale.gen if present in setup directory and execute locale-gen . | |
Install chrony time sync | Chrony time synchronization (https://chrony.tuxfamily.org) | |
Install restic backup | Restic restic is a fast, efficient and secure backup program.(https://restic.net/) | |
[install][config] | Deploy config files and execute configuration tasks | |
Set timezone | Set time zone from time_zone variable. | |
Set locale | Set locale from system_locale variable. | |
Set keyboard | Set keyboard layout from system_keyboard variable. | |
Deploy sudoers file | Deploy sudoers configuration file domadminsudo (Must be present in setup directory). | |
UFW firewall configuration | UFW enabled with ssh allowed. | |
Change local passwords | If file kos-pass is present in setup directory. (Template tpl-kos-pass provided). | |
Deploy ssh authorized_keys | If file authorized_keys is present in setup directory. (Template tpl-authorized_keys provided). | |
Deploy ssh id_rsa keys | If files id_rsa and id_rsa.pub are present in setup directory. (Templates tpl-id_rsa* provided). | |
Generate id_rsa | If doesn't exist for root and sudoer user. | |
Deploy ca-certificates | If files with name pattern ca-*.crt are present. | |
Deploy machine certificate | If files with name pattern hostname.crt and hostname.key are present. | |
Deploy crontab files | If files with name pattern cron-cs-* are present in setup directory. | |
[install] | Reboot after install | |
Reboot | Reboot with confirmation message. |
cs-inject
Note: This script runs from the "DevOps Computer", opening a terminal from the machine configuration directory in the management repository,.
Purpose:Inject & Deploy configuration files into remote machine.This script runs from the management (DevOps) computer, copying all configurationfiles to the remote machine, and calling the script 'cs-deploy.sh' to run frominside the remote machine if 'deploy' mode [-d] is selected.Usage:./csinject.sh [-k] [-i] [-d] [-m <deploy_mode>] [-u <sudo_username>] [-r <remote_machine>] [-h] [-q]Execution modes:-k [ssh-sudoers] - install ssh key and sudoers file into the machine. Required before other actions.-i [inject] - Inject only. Inject configuration files into the machine for manual deployment.-d [deploy] - Inject & Deploy configuration. Calls 'cs-deploy.sh' to run from inside the machine.Options and arguments:-m <deploy_mode> - Deploy mode passed to 'cs-deploy.sh'. Valid modes are:[net-config] - Network configuration. (Reboot when finished).[install] - Package installation, updates and configuration tasks (Reboot when finished).[config] - Redeploy config files and perform configuration tasks (Default mode).-u <sudo_username> - Remote administrator (sudoer) user name (Default value).-r <remote_machine> - Machine hostname or IPAddress (Default value).-h Help-q Quiet (Nonstop) execution.Examples:# Copy ssh key and sudoers file into the machine:./csinject.sh -k# Inject & Deploy configuration in [net-config] mode:./csinject.sh -dm net-config# Inject & Deploy configuration in [install] mode:./csinject.sh -dm install# Inject & Deploy configuration in [config] mode (default):./csinject.sh -d
Tasks performed:
${execution_mode} | Tasks | Block / Description |
---|---|---|
[ssh-sudoers] | Inject ssh key and sudoers file | |
Perform ssh-copy-id | Insert your public key to be authorized in ssh authentication. | |
Deploy sudoers file | Deploy sudoers configuration file domadminsudo (Must be present in setup directory). | |
[inject][deploy] | Copy config files and deploy scripts | |
Prepare setup directory in remote machine | Remove setup directory if exist and create empty new one with permissions. | |
Inject configuration files | SCP configuration files from configuration management into machine setup directory. | |
Deploy scripts to /usr/local/sbin | Delete old cs-*.sh scripts inside /usr/local/sbin and copy new ones from setup directory. | |
[deploy] | Run cs-deploy from inside the machine | |
Execute cs-deploy.sh inside the machine | Run cs-deploy.sh script inside the machine in mode specified by deploy-mode variable`. | |
cs-connect
Note: This script runs from the "DevOps Computer", opening a terminal from the machine configuration directory in the management repository,.
Purpose:SSH remote connection.Use this script to remote login into the machine and establish a ssh session.Usage:csconnect.sh [-u <sudo_username>] [-r <remote_machine>] [-h]Options and arguments:-u <sudo_username> - Remote user name (Default value).-r <remote_machine> - Machine hostname or IPAddress (Default value).-h HelpExamples:# Connect to the machine with default values./csconnect.sh# Connect to IPAddress with specific user./csconnect.sh -u sudo_username -r 192.168.2.99
Tasks performed:
Tasks | Description |
---|---|
Perform ssh connection | Passwordless ssh connection with timeout. |
cs-helloworld
Purpose:Sequential block script model.Use this script as a model or skeleton to write other configuration scripts.Usage:sudo cs-helloworld.sh [-l] [-m <execution_mode>] [-n <name>] [-h] [-q]Execution modes:-l [list-status] - List current status.-m <execution_mode> - Valid modes are:[install] - Install.[remove] - Remove.[update] - Update and reconfigure.Options and arguments:-n <name> - Name of the person to report status.(Optional in list-status. Default value)-h Help-q Quiet (Nonstop) execution.Examples:# Mode "install":sudo cs-helloworld.sh -m install# Mode "remove":sudo cs-helloworld.sh -m remove# Mode "list-status":sudo cs-helloworld.sh -l# Mode "list-status" with special name to report:sudo cs-helloworld.sh -l -n Bond
Tasks performed:
${execution_mode} | Tasks | Block / Description |
---|---|---|
[install] | Install apps and services | |
Task 1 | Task 1 description as commented in code. | |
Task 2 | Task 2 description as commented in code. | |
Task n | Task n description as commented in code. | |
[remove] | Remove apps and services | |
Task 1 | Task 1 description as commented in code. | |
Task 2 | Task 2 description as commented in code. | |
Task n | Task n description as commented in code. | |
[update][install] | Update and reconfigure apps and services | |
Task 1 | Task 1 description as commented in code. | |
Task 2 | Task 2 description as commented in code. | |
Task n | Task n description as commented in code. | |
[list-status][install] [update][remove] | Display status information | |
Display hostname and variables | Show hostame and content of variables used in the script | |
Display report message | Display report message with "some surprise" |
License
Copyright © 2021 cSkyLab.com ™
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.