Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Simple Bash script for syncing Linux user directory to an external drive.

NotificationsYou must be signed in to change notification settings

ditig-com/rsync-backup-script

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

For more information on the script, see the article on theRsync Backup Script.

Script File

Find the script here:rsync-backup-script.sh.

Step-by-Step Explanation of the Script

Preliminaries

  1. Set strict error handling:
    The set -euo pipefail ensures:

    • -e: The script exits if any command returns a non-zero exit code.
    • -u: Exiting with an error if an undefined variable is used.
    • -o pipefail: The script fails if any command in a pipeline fails.
  2. Define script metadata:
    __ScriptVersion="1.2" defines the version of the script.

  3. Define a usage function:
    The usage function describes how to run the script and explains the options:

    • -h|help: Ensures the backup destination is mounted.
    • -v|version: Displays the script version.

Set Key Variables

  • backuppath: The directory to back up ($HOME).
  • mountpoint: The backup destination (/media/user/backup).
  • date: Current date inYYYY-MM-DD format.
  • time: Current time inHH:MM:SS format.

Main Logic

  1. Check if the backup destination is mounted:
    The script usesmountpoint -q $mountpoint to verify if the backup destination is mounted.

    • If mounted:
      Proceed with the backup process.
    • If not mounted:
      Display an error message:
      "$mountpoint is not mounted", styled in bold, and exit with code 6.
  2. Clean up old logs:
    Check if any backup log files exist at the destination (backup*.txt).

    • If none are found: Print"No backup file(s) found!".
    • If found: Prompt for confirmation (y to delete,n to cancel) and delete selected logs.
  3. Perform the backup using rsync:
    The script uses rsync with the following options:

    • -a: Archive mode (preserves symbolic links, permissions, etc.).
    • -v: Verbose output.
    • -r: Recursively sync directories.
    • --delete: Remove files from the destination that no longer exist in the source.
    • --progress: Show progress for each file.
    • --stats: Display transfer statistics.
    • --itemize-changes: List changes made during the sync.
    • --exclude '.cache/': Exclude the .cache/ directory.
    • Output fromrsync is piped totee, saving a log file namedbackup-log_<date>-<time>.txt at the backup destination.
  4. Display a success message:
    Use styled and formatted text (\033 sequences) to indicate success.Specify the synced source ($backuppath) and destination ($mountpoint).

  5. Print the latest file written to the backup destination:
    Usefind to locate the most recently written file and display its timestamp.

  6. Wait, clear screen, and exit:

    • Pause for 15 seconds (sleep 15).
    • Clear the terminal (clear).
    • Exit with code0 to signal success.

Error Handling

If the backup destination ($mountpoint) is not mounted:

  • Print an error message in bold:"$mountpoint is not mounted".
  • Exit with error code6 (No such device or address).

About

Simple Bash script for syncing Linux user directory to an external drive.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp