Close
    logo                                         

    ubt2204srv-lvm

    Ubuntu server 22.04 lvm base installation

    v23-04-27

    This 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 values
    machine:
    hostname: ubt2204srv-lvm
    domainname: cskylab.net
    localadminusername: kos
    localadminpassword: "NoFear21"
    timezone: "UTC"
    networkinterface: enp1s0
    ipaddress: 192.168.82.9
    netmask: 24
    gateway4: 192.168.82.1
    searchdomainnames:
    - cskylab.net
    nameservers:
    - 192.168.82.1
    ## Setup directory where configuration files will be injected
    setupdir: "/etc/csky-setup"
    systemlocale: "C.UTF-8"
    systemkeyboard: "us"
    restic:
    ## Restic password is mandatory to access repository
    password: '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 repositories
    aws_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 values
    sudo 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 values
    sudo 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_snap
    sudo cs-lvmserv.sh -m snap-create -d /srv/mydir

    Remove snapshot:

    # Remove snapshot of data service "/srv/mydir"
    # in default volume group
    sudo 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 group
    sudo 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-pools
    sudo cs-lvmserv.sh -m trim-space

    Repair LVM thin-pool

    1. Unmount all LVM in the volume group

    2. List LVM to see vg and lv names

    sudo lvscan
    1. Deactivate volume group
    sudo lvchange -an vg
    1. Repair thin-pool
    sudo lvconvert --repair vg/tpool
    1. Activate volume group
    sudo lvchange -ay vg
    1. 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-environment
    export 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 repository
    sudo 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/mydir
    sudo 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 repository
    sudo 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 snapshot
    sudo 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/restic
    sudo 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/latest
    sudo 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\:00
    sudo 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 mydir
    sudo cs-restic.sh -m restic-forget -t "mydir" -r "{{ .restic.repo }}"

    Utilities

    Passwords and secrets

    Generate passwords and secrets with:

    # Screen
    echo $(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 snapshot
    find /tmp/restic/snapshots/latest/ -type d -iname "*foo*" # find directories under latest snapshot
    find /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 files
    find . -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 partitions
    sudo fdisk -l
    • Execute fdisk in interactive mode:
    # fdisk interactive mode (Example for device /dev/sdc)
    sudo fdisk /dev/sdc
    Welcome 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): m
    Help:
    GPT
    M enter protective/hybrid MBR
    Generic
    d delete a partition
    F list free unpartitioned space
    l list known partition types
    n add a new partition
    p print the partition table
    t change a partition type
    v verify the partition table
    i print information about a partition
    Misc
    m print this menu
    x extra functionality (experts only)
    Script
    I load disk layout from sfdisk script file
    O dump disk layout to sfdisk script file
    Save & Exit
    w write table to disk and exit
    q quit without saving changes
    Create a new label
    g create a new empty GPT partition table
    G create a new empty SGI (IRIX) partition table
    o create a new empty DOS partition table
    s create a new empty Sun partition table
    Command (m for help):
    • Enter g to create a new empty GPT partition table:
    Command (m for help): g
    Created 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): n
    Partition 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 support exfat file system.)

    • Enter p to display the new partition table:
    Command (m for help): p
    Disk /dev/sdc: 29.84 GiB, 32023511040 bytes, 62545920 sectors
    Disk model: USB Flash Drive
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: FB0E06DB-479A-C842-A775-657D4D52BA41
    Device Start End Sectors Size Type
    /dev/sdc1 2048 62545886 62543839 29.8G Linux filesystem
    Command (m for help):
    • Enter w to save the changes:
    Command (m for help): w
    The 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 partitions
    sudo fdisk -l
    # Find disk UUID
    sudo 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 interactively
    sudo 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 startup
    sudo 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 startup
    sudo 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 directory
    sudo mkdir /media/data
    sudo 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 directory
    sudo mkdir /media/data
    sudo chown -R {{ .machine.localadminusername }}:{{ .machine.localadminusername }} /media/data

    USB Disk unmount:

    • To unmount from /mediaand get the disk ready to unplug:
    # Unmount disk interactively
    sudo umount /media

    Reference

    Scripts

    cs-volgroup

    Purpose:
    Disk volume group management.
    Use this script to create and delete volume groups
    from one or several disk devices.
    Each volume group will have a thin-pool associated to hold
    data 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 status
    sudo cs-volgroup.sh -l
    # Create volume group with default variable values
    sudo 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}TasksBlock / Description
    [create]Create volume group
    Prepare devicesWipe signatures and erase partition tables for all specified devices.
    Create physical volumesCreate physical volumes for all specified devices.
    Create volume groupCreate volume group with all specified devices.
    Create thin-poolCreate a unique thin-pool in volume group to hold thin volumes.
    [delete]Delete volume group
    Delete Volume GroupDelete volume group and all logical volumes inside.
    Wipe devicesWipe signatures and erase partition tables for all specified devices.
    Update /etc/fstabRemove mounts for all logical volumes in volume group.
    [list-status]Display disk and volume group status
    Display statusDisks, 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 independent
    thin logical volumes suitable for snapshot operations.
    A volume group and thin-pool must have been created before using the
    script "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 status
    and discard all changes.
    [trim-space] - Free space inside thin-pools
    discarding 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 status
    sudo 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_snap
    sudo cs-lvmserv.sh -m snap-create -d /srv/mydir
    # Remove snapshot of data service "/srv/mydir"
    # in default volume group
    sudo cs-lvmserv.sh -m snap-remove -d /srv/mydir
    # Merge snapshot of data service "/srv/mydir"
    # in default volume group
    sudo cs-lvmserv.sh -m snap-merge -d /srv/mydir
    # Delete thin logical volume data service "/srv/mydir"
    # in default volume group
    sudo cs-lvmserv.sh -m delete -d /srv/mydir
    # Free space inside thin-pools
    sudo cs-lvmserv.sh -m trim-space

    Tasks performed:

    ${execution_mode}TasksBlock / Description
    [create]Create LVM data service
    Preserve original /etc/fstabFile /etc/fstab is copied to /etc/fstab.naked if not exist.
    Create thin logical volumeCreate 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 filesystemFormat the filesystem as specified in command_to_formatdata_path` option.
    Mount logical volumeCreate 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/fstabAdd mount line to file /etc/fstab and mount all.
    Set directory ownershipChange 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 volumeIf mounted, unmount logical volume from directory specified in data_path option.
    Delete thin logical volumeDelete thin logical volume from volume group.
    Update /etc/fstabRemove mount line in file /etc/fstab .
    Delete service directoryIf empty, remove directory specified in data_path option.
    [snap-create]Create snapshot
    Create snapshotCreate and activate thin snapshot.
    Mount snapshotMount snapshot read-only mode in directory /mnt/<dataservice>_snap.
    [snap-remove]Remove snapshot
    Unmount snapshotIf mounted, unmount snapshot from /mnt/<dataservice>_snap.
    Remove snapshotDelete thin snapshot from volume group.
    Delete snapshot directoryIf empty, remove /mnt/<dataservice>_snap directory
    [snap-merge]Merge snapshot
    Merge SnapshotRoll back logical volume to snapshot status.
    Merge will be delayed until next volume activation.
    Unmount snapshotIf mounted, unmount snapshot from /mnt/<dataservice>_snap.
    Unmount logical volumeIf mounted, unmount logical volume from data_path (Typically /srv/xxx).
    Deactivate and activate logical volumeReactivate logical volume to complete merge and remove snapshot.
    Mount /etc/fstab volumesMount all /etc/fstab volumes.
    Delete snapshot directoryIf empty, remove /mnt/<dataservice>_snap .
    [trim-space]Trim filesystem
    Trim all filesystemsDiscard unused blocks on all mounted filesystems (fstrim -a).
    [list-status]Display LVM and filesystem status
    [ListStatus]List StatusList 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 by
    thin-logical volumes created with the script "cs-lvmserv.sh".
    Snapshots will be created automatically for rsync-to copies
    and 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}TasksBlock / Description
    [rsync-to]RSync data TO remote directory
    Create SnapshotCreate snapshot from data service LV and mount it in /mnt/<dataservice>_snap
    Execute rsync TORSync data from snapshot TO directory specified in remote_data_path option.
    Remove SnapshotRemove snapshot and mount directory when rsync has finished.
    [rsync-from]RSync data FROM remote directory
    Execute rsync FROMRSync 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 by
    thin-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 options
    sudo 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 directory
    sudo cs-restic.sh -m restic-mount -r "s3:https://minio-standalone.cskylab.com/restic-test/"

    Tasks performed:

    ${execution_mode}TasksBlock / Description
    [restic-bck][restic-res]Set restic ssh config parameters
    Prepare ssh config parametersGenerate file /root/.ssh/config with appropriate parameters for restic sftp connections.
    [restic-bck]Restic backup
    Create SnapshotCreate snapshot from data service LV and mount it to /mnt/<dataservice>_snap
    Execute Restic backupBackup data from snapshot to restic repository specified in RESTIC_REPOSITORY variable.
    Remove SnapshotUnmount and remove snapshot when Restic backup is finished.
    [restic-forget]Restic repository maintenance
    Maintain Restic repositoryMaintain Restic repository with policy defined in forget_options variable.
    [restic-list][restic-bck] [restic-forget]Display snapshots in Restic repository
    List snapshots in repositoryList snapshots and display statistics for the repository.
    [restic-res]Restic restore
    Execute Restic restoreRestore data from snapshot to data service directory (must be empty) specified in data_path option.
    [repo-init]Create Restic repository
    Initialize Restic repositoryInitialize 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 ownershipIf RESTIC_REPOSITORY is a local directory, change ownership to local sudoer.
    [restic-mount]Restic repository maintenance
    Mount Restic repositoryMount 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 & Deploy
    operations. 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}TasksBlock / Description
    [net-config]Network configuration
    Deploy /etc/hostnameConfiguration file hostname must exist in setup directory.
    Deploy /etc/hostsConfiguration file hosts must exist in setup directory.
    Deploy /etc/netplan/01-netcfg.yamlConfiguration file 01-netcfg.yaml must exist in setup directory.
    Disable cloud-initFlag that signals that cloud-init should not run.
    Change systemd-resolvedChange configuration of file /etc/resolv.conf.
    Try Netplan configurationExecute netplan try to test new network configuration.
    RebootReboot with confirmation message.
    [install]Install and update packages
    Update installed packagesUpdate package repositories, perform dist-upgrade and autoremove
    Generate localesDeploy file locale.gen if present in setup directory and execute locale-gen.
    Install chrony time syncChrony time synchronization (https://chrony.tuxfamily.org)
    Install restic backupRestic restic is a fast, efficient and secure backup program.(https://restic.net/)
    [install][config]Deploy config files and execute configuration tasks
    Set timezoneSet time zone from time_zone variable.
    Set localeSet locale from system_locale variable.
    Set keyboardSet keyboard layout from system_keyboard variable.
    Deploy sudoers fileDeploy sudoers configuration file domadminsudo (Must be present in setup directory).
    UFW firewall configurationUFW enabled with ssh allowed.
    Change local passwordsIf file kos-passis present in setup directory. (Template tpl-kos-pass provided).
    Deploy ssh authorized_keysIf file authorized_keysis present in setup directory. (Template tpl-authorized_keys provided).
    Deploy ssh id_rsa keysIf files id_rsa and id_rsa.pub are present in setup directory. (Templates tpl-id_rsa* provided).
    Generate id_rsaIf doesn't exist for root and sudoer user.
    Deploy ca-certificatesIf files with name pattern ca-*.crt are present.
    Deploy machine certificateIf files with name pattern hostname.crt and hostname.keyare present.
    Deploy crontab filesIf files with name pattern cron-cs-* are present in setup directory.
    [install]Reboot after install
    RebootReboot 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 configuration
    files to the remote machine, and calling the script 'cs-deploy.sh' to run from
    inside 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}TasksBlock / Description
    [ssh-sudoers]Inject ssh key and sudoers file
    Perform ssh-copy-idInsert your public key to be authorized in ssh authentication.
    Deploy sudoers fileDeploy sudoers configuration file domadminsudo (Must be present in setup directory).
    [inject][deploy]Copy config files and deploy scripts
    Prepare setup directory in remote machineRemove setup directory if exist and create empty new one with permissions.
    Inject configuration filesSCP configuration files from configuration management into machine setup directory.
    Deploy scripts to /usr/local/sbinDelete 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 machineRun 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 Help
    Examples:
    # 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:

    TasksDescription
    Perform ssh connectionPasswordless 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}TasksBlock / Description
    [install]Install apps and services
    Task 1Task 1 description as commented in code.
    Task 2Task 2 description as commented in code.
    Task nTask n description as commented in code.
    [remove]Remove apps and services
    Task 1Task 1 description as commented in code.
    Task 2Task 2 description as commented in code.
    Task nTask n description as commented in code.
    [update][install]Update and reconfigure apps and services
    Task 1Task 1 description as commented in code.
    Task 2Task 2 description as commented in code.
    Task nTask n description as commented in code.
    [list-status][install] [update][remove]Display status information
    Display hostname and variablesShow hostame and content of variables used in the script
    Display report messageDisplay 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.