Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


ChatGPT – Rsync Backup Script. Is it the perfect script?
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

ChatGPT – Rsync Backup Script. Is it the perfect script?

nqservicesnqservices Member
edited December 2022 in General

Hi,

Asked ChatGPT to code a script that uses rsync to backup a Ubuntu server to a remote host and keep 7 daily, 4 weekly, and 12 monthly backups.

Below is the result. What do you think? Is it “perfect”? Or anyone spot an error or can make better than IA 😉 ?

Thanks!

#!/bin/bash

# Set the backup destination
DESTINATION="[email protected]:/path/to/backup/directory"

# Set the source directory to be backed up
SOURCE="/path/to/source/directory"

# Set the base name for the backup directories
BASE_NAME="backup"

# Set the maximum number of daily, weekly, and monthly backups to keep
MAX_DAILY_BACKUPS=7
MAX_WEEKLY_BACKUPS=4
MAX_MONTHLY_BACKUPS=12

# Set the day of the week to use for weekly backups (0=Sunday, 1=Monday, etc.)
WEEKLY_BACKUP_DAY=6

# Set the day of the month to use for monthly backups
MONTHLY_BACKUP_DAY=1

# Get the current date and time
DATE=$(date +%Y-%m-%d_%H-%M-%S)

# Determine the type of backup (daily, weekly, or monthly) based on the current date
if [ $(date +%u) -eq $WEEKLY_BACKUP_DAY ]; then
  # Weekly backup
  BACKUP_TYPE="weekly"
elif [ $(date +%d) -eq $MONTHLY_BACKUP_DAY ]; then
  # Monthly backup
  BACKUP_TYPE="monthly"
else
  # Daily backup
  BACKUP_TYPE="daily"
fi

# Create the destination directory for the backup
BACKUP_DIR="$BASE_NAME/$BACKUP_TYPE/$DATE"

# Run the backup
rsync -av --delete "$SOURCE" "$DESTINATION/$BACKUP_DIR"

# Remove old backups
# Daily backups
find "$DESTINATION/$BASE_NAME/daily" -maxdepth 1 -type d -mtime +$MAX_DAILY_BACKUPS -exec rm -rf {} \;

# Weekly backups
find "$DESTINATION/$BASE_NAME/weekly" -maxdepth 1 -type d -mtime +$(($MAX_WEEKLY_BACKUPS * 7)) -exec rm -rf {} \;

# Monthly backups
find "$DESTINATION/$BASE_NAME/monthly" -maxdepth 1 -type d -mtime +$(($MAX_MONTHLY_BACKUPS * 31)) -exec rm -rf {} \;

Comments

  • DPDP Administrator, The Domain Guy

    I'm surprised that you didn't ask ChatGPT:

    1. If the script was "perfect"
    2. If ChatGPT was able to spot any errors
    3. If other ChatGPT AImates could make something better

    :smiley:

  • @DP said:
    I'm surprised that you didn't ask ChatGPT:

    1. If the script was "perfect"
    2. If ChatGPT was able to spot any errors
    3. If other ChatGPT AImates could make something better

    :smiley:

    Do not want a bias opinion... ;)

    Thanked by 2seenu Advin
  • please use proper code tags to format the script and make it better readable.

    while the solution seems quite correct it is a rather horrible, basic way of doing so.
    it fully defeats the purpose of using rsync in the first place (only syncing changes) by putting each backup into a seperate folder and therefore doing a full backup each day.

    the solution to your problem is something like rdiff-backup. maybe ask the bot about it ;-)

    Thanked by 1chitree
  • nqservicesnqservices Member
    edited December 2022

    @Falzo said:
    please use proper code tags to format the script and make it better readable.

    I have tryied... to make code
    but it did not work... Any advice?

  • DPDP Administrator, The Domain Guy

    @Falzo said: please use proper code tags to format the script and make it better readable.

    Sorted 👌

    Thanked by 3nqservices Ympker Falzo
  • @DP said:

    @Falzo said: please use proper code tags to format the script and make it better readable.

    Sorted 👌

    Thanks!

  • I'd strongly suggest looking into rsnapshot, which is a wrapper script for rsync that does a great job of handling daily/weekly/monthly backups in a space efficient way. I've been using it for many years.

  • ArkasArkas Moderator

    Darn it. It might replace my less significant other the way this thing is going.

  • @Falzo said:
    please use proper code tags to format the script and make it better readable.

    while the solution seems quite correct it is a rather horrible, basic way of doing so.
    it fully defeats the purpose of using rsync in the first place (only syncing changes) by putting each backup into a seperate folder and therefore doing a full backup each day.

    the solution to your problem is something like rdiff-backup. maybe ask the bot about it ;-)

    Hadn't heard of rdiff-backup yet

  • @aj_potc said:
    I'd strongly suggest looking into rsnapshot, which is a wrapper script for rsync that does a great job of handling daily/weekly/monthly backups in a space efficient way. I've been using it for many years.

    Thanks for the advice. I will check it out. Also looking into rdiff-backup as @Falzo mentioned. Really looking for a good way to have daily, weekly and year backups as described on my first message on this post.

    Maybe rsync is not the best option...

  • If it works, it's good. Otherwise not. Have you run it before?

  • @nqservices said:

    Maybe rsync is not the best option...

    Rsync is an excellent option if you want a simple, reliable, file-based backup. It won't compress or encrypt your backups, and it can't do block-level backups like more advanced software. But rsync can't be beat when it comes to restoration: you don't need any special software; just copy back your files.

    For my important data, I use a mix of backup solutions. Rsync/rsnapshot is great to keep snapshots of my files, and I other software to do more advanced things like compression and deduplication, or to handle full-system bare metal backups.

    Thanked by 1kalimov622
  • WebProjectWebProject Host Rep, Veteran

    @aj_potc said:

    @nqservices said:

    Maybe rsync is not the best option...

    Rsync is an excellent option if you want a simple, reliable, file-based backup. It won't compress or encrypt your backups, and it can't do block-level backups like more advanced software. But rsync can't be beat when it comes to restoration: you don't need any special software; just copy back your files.

    For my important data, I use a mix of backup solutions. Rsync/rsnapshot is great to keep snapshots of my files, and I other software to do more advanced things like compression and deduplication, or to handle full-system bare metal backups.

    It’s sounds that you haven’t tried rclone? As you will be able to encrypt and backup and more other options than just sync files with rsync!

    Thanked by 1vyas11
  • Me: oh hai ChatGPT, please create a backup script for me

    ChatGPT: rm -rf /

    Me: FFFFUUUUUUUUUUUUUUUUUUUUUUU

    Thanked by 2farsighter ariq01
  • How borg is comparable to rsync or rsnapshot?

  • @WebProject said:

    @aj_potc said:

    @nqservices said:

    Maybe rsync is not the best option...

    Rsync is an excellent option if you want a simple, reliable, file-based backup. It won't compress or encrypt your backups, and it can't do block-level backups like more advanced software. But rsync can't be beat when it comes to restoration: you don't need any special software; just copy back your files.

    For my important data, I use a mix of backup solutions. Rsync/rsnapshot is great to keep snapshots of my files, and I other software to do more advanced things like compression and deduplication, or to handle full-system bare metal backups.

    It’s sounds that you haven’t tried rclone? As you will be able to encrypt and backup and more other options than just sync files with rsync!

    I definitely have tried rclone, and it's a great project. I use it for copying to/from cloud storage. But I find that rsync/rsnapshot serve a different purpose: making very fast file-based snapshots that require no special software to read or restore.

    For more features (logging, monitoring, notifications, encryption, deduplication, bare metal restore), I use commercial backup products like Veeam. Those aren't cheap, but it also gives me access to their support if I need it.

  • Here are a few suggestions for improving the script:

    Add comments to explain what each part of the script is doing. This will make it easier for you or someone else to understand and maintain the script in the future.

    Consider using variables for the backup types (e.g., "daily", "weekly", "monthly") instead of hardcoding them in multiple places throughout the script. This will make it easier to change the types of backups being performed if needed.

    Instead of using the find command to delete old backups, you could use the ls command to list the directories in each backup type and delete the oldest ones until you reach the desired number of backups to keep. This would be more efficient than using find to search for all directories and then deleting the ones that are too old.

    You may want to add some error handling to the script in case any of the commands fail. For example, you could check the return value of the rsync command to make sure it ran successfully, and you could also add a trap command to handle any signals that might cause the script to exit unexpectedly.

    I hope these suggestions are helpful! Let me know if you have any questions.

    Thanked by 1kkrajk
  • TimboJonesTimboJones Member
    edited December 2022

    @justchil said:
    Here are a few suggestions for improving the script:

    Add comments to explain what each part of the script is doing. This will make it easier for you or someone else to understand and maintain the script in the future.

    There's literally a comment per line of code. Any more comments and you'll be made fun of by people who don't even code.

    Consider using variables for the backup types (e.g., "daily", "weekly", "monthly") instead of hardcoding them in multiple places throughout the script. This will make it easier to change the types of backups being performed if needed.

    The only hardcoded places make sense and suit no purpose for being a variable. The weekly prune will never be anything but weekly.

    Instead of using the find command to delete old backups, you could use the ls command to list the directories in each backup type and delete the oldest ones until you reach the desired number of backups to keep. This would be more efficient than using find to search for all directories and then deleting the ones that are too old.

    That seems like more processing and more chances for error.

    You may want to add some error handling to the script in case any of the commands fail. For example, you could check the return value of the rsync command to make sure it ran successfully, and you could also add a trap command to handle any signals that might cause the script to exit unexpectedly.

    Good call.

    ChatGPT: "shit, you didn't ask for error handling, not my bad".

    Thanked by 1kkrajk
  • LowEndTalk is a forum and community website for people interested in low-end virtual private servers (VPS), shared hosting, and other budget hosting solutions. The website features discussions and reviews of different hosting providers, as well as a marketplace where users can buy and sell hosting services.

    LowEndTalk is aimed at users who are looking for affordable hosting solutions, and the discussions on the forum often focus on finding the best deals and discounts on hosting services. The website also has sections dedicated to topics such as website development, server management, and security.

    In addition to the forum, LowEndTalk also has a blog and a Twitter account where users can stay up to date on the latest news and developments in the world of budget hosting.

Sign In or Register to comment.