Skip to content

Instantly share code, notes, and snippets.

@mcxiaoke
Forked from ryankurte/backup.env
Created November 20, 2022 12:02
Show Gist options
  • Save mcxiaoke/bca0e5ed7b4fa83a5ebc329deba68abf to your computer and use it in GitHub Desktop.
Save mcxiaoke/bca0e5ed7b4fa83a5ebc329deba68abf to your computer and use it in GitHub Desktop.

Revisions

  1. @ryankurte ryankurte revised this gist Mar 18, 2020. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -11,4 +11,9 @@ cp backup.env backup.exclude /etc/
    cp backup.service backup.timer /etc/systemd/system

    # Install backup service
    systemctl enable backup.service
    systemctl enable backup.timer

    # Start timer
    systemctl start backup.timer

  2. @ryankurte ryankurte revised this gist Mar 18, 2020. 4 changed files with 30 additions and 4 deletions.
    3 changes: 3 additions & 0 deletions backup.env
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,9 @@ RESTIC_REPOSITORY=/media/large1/backups/restic
    # Restic password file
    RESTIC_PASSWORD_FILE=/root/.restic.pass

    # Exclude file for rsync --exclude-from
    EXCLUDE_FILE=/etc/backup.exclude

    # Directories to back up
    BACKUP_DIRS="/home;/etc"

    8 changes: 8 additions & 0 deletions backup.exclude
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    .cargo/registry
    .toolchain
    target
    go/pkg
    go/src
    .cache
    .mozilla

    9 changes: 5 additions & 4 deletions backup.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,6 @@
    # Place in /usr/sbin/backup.sh
    # Enable with `systemctl enable backup.timer`
    # Check status with `systemctl status backup.service`

    set -e

    # Check variables
    @@ -27,12 +26,14 @@ if [ -Z "$BACKUP_DIRS" ]; then
    exit 1
    fi

    if [! -Z "$EXCLUDE_FILE" ]; then
    EXCLUDES="--exclude-from=$EXCLUDE_FILE"
    fi


    # Split dirs using semicolons
    DIRS=$(echo $BACKUP_DIRS | tr ";" "\n")

    # rsync excludes
    EXCLUDES="--exclude=.cargo/registry --exclude=.toolchains --exclude=target --exclude=go/pkg --exclude=go/src --exclude=.cache --exclude=.mozilla"


    function do_rsync() {

    14 changes: 14 additions & 0 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    #!/bin/bash
    # Helper script to install the backup service

    # Copy tool
    cp backup.sh /usr/sbin/

    # Copy configuration
    cp backup.env backup.exclude /etc/

    # Copy units
    cp backup.service backup.timer /etc/systemd/system

    # Install backup service
    systemctl enable backup.timer
  3. @ryankurte ryankurte revised this gist Mar 18, 2020. 3 changed files with 53 additions and 19 deletions.
    16 changes: 16 additions & 0 deletions backup.env
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    # Systemd environment file for backup.sh daily backup service
    # Place in /etc/backup.env

    # Destination for rsync file system clone
    BACKUP_DEST=/media/large1/backups/raw

    # Destination for restic repository
    RESTIC_REPOSITORY=/media/large1/backups/restic

    # Restic password file
    RESTIC_PASSWORD_FILE=/root/.restic.pass

    # Directories to back up
    BACKUP_DIRS="/home;/etc"


    3 changes: 3 additions & 0 deletions backup.service
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,12 @@ Description=Daily backup service
    [Service]
    Type=oneshot
    User=root

    EnvironmentFile=/etc/backup.env
    ExecStart=/usr/bin/nice -n 19 /usr/bin/ionice -c2 -n7 /usr/sbin/backup.sh
    RemainAfterExit=true

    [Install]
    WantedBy=multi-user.target


    53 changes: 34 additions & 19 deletions backup.sh
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,49 @@
    #!/bin/bash
    # Local backup script, adapted from https://webworxshop.com/centralised-backups-with-restic-and-rsync/
    # Place in /usr/sbin/backup.sh
    # Enable with `sudo systemctl enable backup.timer`
    # View status with `sudo systemctl list-timers --all` and `sudo systemctl status backup.service`
    # Enable with `systemctl enable backup.timer`
    # Check status with `systemctl status backup.service`

    set -e

    # Backup file destination
    BACKUP_DEST=/media/large1/backups
    # Check variables
    if [ -z "$BACKUP_DEST" ]; then
    echo "BACKUP_DEST must be set"
    exit 1
    fi

    if [ -z "$RESTIC_REPOSITORY" ]; then
    echo "RESTIC_REPOSITORY must be set"
    exit 1
    fi

    if [ -z "$RESTIC_PASSWORD_FILE" ]; then
    echo "RESTIC_PASSWORD_FILE must be set"
    exit 1
    fi

    if [ -Z "$BACKUP_DIRS" ]; then
    echo "BACKUP_DIRS must be set"
    exit 1
    fi


    DIRS=$(echo $BACKUP_DIRS | tr ";" "\n")

    # rsync excludes
    EXCLUDES="--exclude=.cargo/registry --exclude=.toolchains --exclude=target --exclude=go/pkg --exclude=go/src --exclude=.cache --exclude=.mozilla"

    # Set restic globals
    export RESTIC_REPOSITORY=$BACKUP_DEST/restic
    export RESTIC_PASSWORD_FILE=/root/.restic.pass


    function do_rsync() {

    # Write start info to log file
    echo "Starting rsync backup job $1 to $BACKUP_DEST/raw$1 at $(date '+%Y-%m-%d %H:%M:%S')..."
    echo "Starting rsync backup job $1 to $BACKUP_DEST$1 at $(date '+%Y-%m-%d %H:%M:%S')..."

    # Ensure backup directory exists
    mkdir -p $BACKUP_DEST/raw$1
    mkdir -p $BACKUP_DEST$1

    # Execute rsync
    /usr/bin/rsync -aP --delete $EXCLUDES $1 $BACKUP_DEST/raw
    /usr/bin/rsync -aP --delete $EXCLUDES $1 $BACKUP_DEST

    # Write end-info to log file
    echo "Rsync job finished at $(date '+%Y-%m-%d %H:%M:%S')."
    @@ -36,12 +53,9 @@ function do_rsync() {

    echo "Starting rsync clone"

    # rsync home directories
    do_rsync /home

    # rsync configuration
    do_rsync /etc

    for D in $DIRS; do
    do_rsync $D
    done

    # copy apt list
    dpkg --get-selections > $BACKUP_DEST/packages.list
    @@ -54,8 +68,9 @@ restic check


    # Execute backups
    restic backup $BACKUP_DEST/raw/home
    restic backup $BACKUP_DEST/raw/etc
    for D in $DIRS; do
    restic backup $BACKUP_DEST/$D
    done


    # Clean up old snapshots
  4. @ryankurte ryankurte revised this gist Mar 18, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions backup.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    #!/bin/bash
    # Local backup script, adapted from https://webworxshop.com/centralised-backups-with-restic-and-rsync/
    # Place in /usr/sbin/backup.sh
    # Enable with `sudo systemctl enable backup.timer`
    # View status with `sudo systemctl list-timers --all` and `sudo systemctl status backup.service`

    set -e

  5. @ryankurte ryankurte created this gist Mar 18, 2020.
    15 changes: 15 additions & 0 deletions backup.service
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    # Systemd unit for backup.sh service
    # Place in /etc/systemd/system/backup.service

    [Unit]
    Description=Daily backup service

    [Service]
    Type=oneshot
    User=root
    ExecStart=/usr/bin/nice -n 19 /usr/bin/ionice -c2 -n7 /usr/sbin/backup.sh
    RemainAfterExit=true

    [Install]
    WantedBy=multi-user.target

    65 changes: 65 additions & 0 deletions backup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    #!/bin/bash
    # Local backup script, adapted from https://webworxshop.com/centralised-backups-with-restic-and-rsync/
    # Place in /usr/sbin/backup.sh

    set -e

    # Backup file destination
    BACKUP_DEST=/media/large1/backups

    # rsync excludes
    EXCLUDES="--exclude=.cargo/registry --exclude=.toolchains --exclude=target --exclude=go/pkg --exclude=go/src --exclude=.cache --exclude=.mozilla"

    # Set restic globals
    export RESTIC_REPOSITORY=$BACKUP_DEST/restic
    export RESTIC_PASSWORD_FILE=/root/.restic.pass


    function do_rsync() {

    # Write start info to log file
    echo "Starting rsync backup job $1 to $BACKUP_DEST/raw$1 at $(date '+%Y-%m-%d %H:%M:%S')..."

    # Ensure backup directory exists
    mkdir -p $BACKUP_DEST/raw$1

    # Execute rsync
    /usr/bin/rsync -aP --delete $EXCLUDES $1 $BACKUP_DEST/raw

    # Write end-info to log file
    echo "Rsync job finished at $(date '+%Y-%m-%d %H:%M:%S')."

    echo "Done"
    }

    echo "Starting rsync clone"

    # rsync home directories
    do_rsync /home

    # rsync configuration
    do_rsync /etc


    # copy apt list
    dpkg --get-selections > $BACKUP_DEST/packages.list


    echo "Starting restic backup"

    # Check database
    restic check


    # Execute backups
    restic backup $BACKUP_DEST/raw/home
    restic backup $BACKUP_DEST/raw/etc


    # Clean up old snapshots
    restic forget -d 7 -w 4 -m 6 -y 2


    # Re-check the database
    restic check

    11 changes: 11 additions & 0 deletions backup.timer
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    # Systemd timer for backup.sh service
    # Place in /etc/systemd/system/backup.timer

    [Unit]
    Description=Daily backup service

    [Timer]
    OnCalendar=*-*-* 4:00:00

    [Install]
    WantedBy=timers.target