Skip to content

Instantly share code, notes, and snippets.

@techris45
Forked from enoch85/backup-servername.sh
Created September 24, 2022 16:42
Show Gist options
  • Save techris45/9dc1460cb4d2e204e349f274c8cd78f2 to your computer and use it in GitHub Desktop.
Save techris45/9dc1460cb4d2e204e349f274c8cd78f2 to your computer and use it in GitHub Desktop.

Revisions

  1. Daniel Hansson revised this gist Jul 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/bin/bash
    #!/bin/sh
    #
    # This script is used on a QNAP TS-269 PRO. https://www.en0ch.se/qnap-and-rsync/
    #
  2. Daniel Hansson revised this gist Jun 27, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -93,7 +93,7 @@ ssh -p22 [email protected] "mysqldump \
    | bzip2" > $SHAREUSR/$TODAY/db/$TODAY.sql.bz2

    # Un-hash this if you want to remove old backups (older than 2 days)
    # rm -R $OLDBACKUP
    # rm -R $SHAREUSR/$OLDBACKUP

    # Writes a log of successful updates
    echo "BACKUP success-$TODAY" >> $LOG
  3. Daniel Hansson revised this gist Jun 23, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ RSYNC="/usr/bin/sudo /usr/bin/rsync"

    # Set the folderpath on the QNAP
    # Dont't forget to mkdir $SHAREUSR
    SHAREUSR="/share/yourserver.se"
    SHAREUSR="/share/CACHEDEV1_DATA/yourserver.se"

    # This is a list of files to ignore from backups.
    # Dont't forget to touch $EXCLUDES
  4. Daniel Hansson revised this gist Jun 23, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ SOURCE="[email protected]:/folder-you-want-to-backup"
    DESTINATION="$SHAREUSR/$TODAY/"

    # Keep database backups in a separate directory.
    mkdir -p /$SHAREUSR/$TODAY/db
    mkdir -p $SHAREUSR/$TODAY/db

    # This command rsync's files from the remote server to the local server.
    # Flags:
    @@ -85,7 +85,7 @@ rsync -zavx -e 'ssh -p22' \
    # preferable to back up each database to a separate file. If you do that, I
    # suggest adding a configuration file that is looped over with a bash for()
    # loop.
    ssh [email protected] "mysqldump \
    ssh -p22 [email protected] "mysqldump \
    --user=root \
    --password=SUPERSECRET \
    --all-databases \
  5. Daniel Hansson revised this gist Jun 21, 2015. 2 changed files with 3 additions and 4 deletions.
    3 changes: 2 additions & 1 deletion backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,8 @@
    # 1. $SHAREUSR
    # 2. $EXCLUDES (if you want o change the name of the file servername.excludes)
    # 3. $SOURCE & $DESTINATION
    # 4.
    # 4. [email protected] for the mysqldump
    # 5. --password=SUPERSECRET

    TODAY=`date +"%Y%m%d"`
    YESTERDAY=`date -d "1 day ago" +"%Y%m%d"`
    4 changes: 1 addition & 3 deletions servername.excludes
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,4 @@
    /run
    /lost+found
    /var/tmp
    /var/lib/mysql/


    /var/lib/mysql/
  6. Daniel Hansson revised this gist Jun 21, 2015. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,12 @@
    #!/bin/bash
    #
    # This script is used on a QNAP TS-269 PRO. https://www.en0ch.se/qnap-and-rsync/
    #
    #
    # You have to change:
    # 1. $SHAREUSR
    # 2. $EXCLUDES (if you want o change the name of the file servername.excludes)
    # 3. $SOURCE & $DESTINATION
    # 4.

    TODAY=`date +"%Y%m%d"`
    YESTERDAY=`date -d "1 day ago" +"%Y%m%d"`
    @@ -16,7 +21,7 @@ SHAREUSR="/share/yourserver.se"

    # This is a list of files to ignore from backups.
    # Dont't forget to touch $EXCLUDES
    EXCLUDES="/share/yourserver.se.excludes"
    EXCLUDES="$SHAREUSR/servername.excludes"

    #LOG file
    # Dont't forget to touch $LOG
    @@ -81,7 +86,7 @@ rsync -zavx -e 'ssh -p22' \
    # loop.
    ssh [email protected] "mysqldump \
    --user=root \
    --password=Danielen0ch# \
    --password=SUPERSECRET \
    --all-databases \
    --lock-tables \
    | bzip2" > $SHAREUSR/$TODAY/db/$TODAY.sql.bz2
  7. Daniel Hansson revised this gist Jun 21, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ SHAREUSR="/share/yourserver.se"

    # This is a list of files to ignore from backups.
    # Dont't forget to touch $EXCLUDES
    EXCLUDES="$SHAREUSR.excludes"
    EXCLUDES="/share/yourserver.se.excludes"

    #LOG file
    # Dont't forget to touch $LOG
  8. Daniel Hansson revised this gist Jun 21, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -81,7 +81,7 @@ rsync -zavx -e 'ssh -p22' \
    # loop.
    ssh [email protected] "mysqldump \
    --user=root \
    --password="SUPERSECRETPASSWORD" \
    --password=Danielen0ch# \
    --all-databases \
    --lock-tables \
    | bzip2" > $SHAREUSR/$TODAY/db/$TODAY.sql.bz2
  9. Daniel Hansson revised this gist Jun 21, 2015. 1 changed file with 17 additions and 6 deletions.
    23 changes: 17 additions & 6 deletions backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -69,13 +69,24 @@ mkdir -p /$SHAREUSR/$TODAY/db
    # authentication use a password-less SSH key only allowed read permissions by
    # the backup server's root user.
    rsync -zavx -e 'ssh -p22' \
    --rsync-path="$RSYNC" \
    --exclude-from=$EXCLUDES \
    --numeric-ids \
    --delete -r \
    --link-dest=../$YESTERDAY $SOURCE $DESTINATION
    --rsync-path="$RSYNC" \
    --exclude-from=$EXCLUDES \
    --numeric-ids \
    --delete -r \
    --link-dest=../$YESTERDAY $SOURCE $DESTINATION

    # Un-hash this if you want to remove old backups (older than 2 days)
    # Backup all databases. I backup all databases into a single file. It might be
    # preferable to back up each database to a separate file. If you do that, I
    # suggest adding a configuration file that is looped over with a bash for()
    # loop.
    ssh [email protected] "mysqldump \
    --user=root \
    --password="SUPERSECRETPASSWORD" \
    --all-databases \
    --lock-tables \
    | bzip2" > $SHAREUSR/$TODAY/db/$TODAY.sql.bz2

    # Un-hash this if you want to remove old backups (older than 2 days)
    # rm -R $OLDBACKUP

    # Writes a log of successful updates
  10. Daniel Hansson revised this gist Jun 21, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash
    #
    # This script is used on a QNAP TS-269 PRO.
    # This script is used on a QNAP TS-269 PRO. https://www.en0ch.se/qnap-and-rsync/
    #

    TODAY=`date +"%Y%m%d"`
  11. Daniel Hansson revised this gist Jun 21, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    #!/bin/bash
    #
    # This script is used on a QNAP TS-269 PRO.
    #

    TODAY=`date +"%Y%m%d"`
    YESTERDAY=`date -d "1 day ago" +"%Y%m%d"`
  12. Daniel Hansson revised this gist Jun 21, 2015. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -71,6 +71,12 @@ rsync -zavx -e 'ssh -p22' \
    --numeric-ids \
    --delete -r \
    --link-dest=../$YESTERDAY $SOURCE $DESTINATION

    # Backup all databases. I backup all databases into a single file. It might be
    # prefer

    # Un-hash this if you want to remove old backups (older than 2 days)
    # rm -R $OLDBACKUP

    # Writes a log of successful updates
    echo "BACKUP success-$TODAY" >> $LOG

    # Clean exit
    exit 0
  13. Daniel Hansson revised this gist Jun 21, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion servername.excludes
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,9 @@
    /mnt
    /swapfile
    /tmp
    /run
    /lost+found
    /var/tmp
    /var/lib/mysql/
    /var/lib/varnish/servername


  14. Daniel Hansson revised this gist Jun 21, 2015. 1 changed file with 8 additions and 18 deletions.
    26 changes: 8 additions & 18 deletions backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -23,9 +23,10 @@ LOG="$SHAREUSR/BACKUP_success.log"
    # backups that are particularly large (other than the initial backup), but that
    # you will be creating thousands of hardlinks on disk that will consume inodes.

    DESTINATION="$SHAREUSR/$TODAY/"

    # Source and Destination
    SOURCE="[email protected]:/folder-you-want-to-backup"

    DESTINATION="$SHAREUSR/$TODAY/"

    # Keep database backups in a separate directory.
    mkdir -p /$SHAREUSR/$TODAY/db
    @@ -34,11 +35,14 @@ mkdir -p /$SHAREUSR/$TODAY/db
    # Flags:
    # -z enables gzip compression of the transport stream.
    # -e enables using ssh as the transport prototcol.
    # -a preserves all file attributes and permissions.
    # -x (or --one-file-system) Don’t cross filesystem boundaries
    # -v shows the progress.
    # --rsync-path lets us pass the remote rsync command through sudo.
    # --archive preserves all file attributes and permissions.
    # --exclude-from points to our configuration of files and directories to skip.
    # --numeric-ids is needed if user ids don't match between the source and
    # destination servers.
    # --delete -r(ecursive) Deletes files from $DESTINATION that are not present on the $SOURCE
    # --link-dest is a key flag. It tells the local rsync process that if the
    # file on the server is identical to the file in ../$YESTERDAY, instead
    # of transferring it create a hard link. You can use the "stat" command
    @@ -69,18 +73,4 @@ rsync -zavx -e 'ssh -p22' \
    --link-dest=../$YESTERDAY $SOURCE $DESTINATION

    # Backup all databases. I backup all databases into a single file. It might be
    # preferable to back up each database to a separate file. If you do that, I
    # suggest adding a configuration file that is looped over with a bash for()
    # loop.
    ssh [email protected] "mysqldump \
    --user=root \
    --password="supersecretpassword" \
    --all-databases \
    --lock-tables \
    | bzip2" > $SHAREUSR/$TODAY/db/$TODAY.sql.bz2

    rm -R $OLDBACKUP

    echo "BACKUP success-$TODAY" >> $LOG

    exit 0
    # prefer
  15. Daniel Hansson revised this gist Jun 21, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -81,6 +81,6 @@ ssh [email protected] "mysqldump \

    rm -R $OLDBACKUP

    echo "BACKUP success-`date +"%Y%m%d"`" >> $LOG
    echo "BACKUP success-$TODAY" >> $LOG

    exit 0
  16. Daniel Hansson revised this gist Jun 21, 2015. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -6,23 +6,29 @@ OLDBACKUP=`date -d "2 days ago" +"%Y%m%d"`

    # Set the path to rsync on the remote server so it runs with sudo.
    RSYNC="/usr/bin/sudo /usr/bin/rsync"

    # Set the folderpath on the QNAP
    # Dont't forget to mkdir $SHAREUSR
    SHAREUSR="/share/yourserver.se"

    # This is a list of files to ignore from backups.
    EXCLUDES="/share/yourserver.se.excludes"
    # Dont't forget to touch $EXCLUDES
    EXCLUDES="$SHAREUSR.excludes"

    #LOG file
    LOG="/share/yourserver.se/BACKUP_success.log"
    # Dont't forget to touch $LOG
    LOG="$SHAREUSR/BACKUP_success.log"

    # Remember that you will not be generating
    # backups that are particularly large (other than the initial backup), but that
    # you will be creating thousands of hardlinks on disk that will consume inodes.

    DESTINATION="/share/yourserver.se/$TODAY/"
    DESTINATION="$SHAREUSR/$TODAY/"

    SOURCE="[email protected]:/folder-you-want-to-backup"

    # Keep database backups in a separate directory.
    mkdir -p /share/yourserver.se/$TODAY/db
    mkdir -p /$SHAREUSR/$TODAY/db

    # This command rsync's files from the remote server to the local server.
    # Flags:
    @@ -71,7 +77,7 @@ ssh [email protected] "mysqldump \
    --password="supersecretpassword" \
    --all-databases \
    --lock-tables \
    | bzip2" > /share/yourserver.se/$TODAY/db/$TODAY.sql.bz2
    | bzip2" > $SHAREUSR/$TODAY/db/$TODAY.sql.bz2

    rm -R $OLDBACKUP

  17. Daniel Hansson revised this gist Jun 21, 2015. 1 changed file with 23 additions and 24 deletions.
    47 changes: 23 additions & 24 deletions backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -1,31 +1,28 @@
    #!/bin/bash

    # This is a simple backup script that uses rsync to backup files and does
    # complete mysql table dumps. Note that this script DOES NOT EXPIRE OLD BACKUPS.
    # Place it in /etc/cron.daily to execute it automatically. For hourly backups,
    # it will need to be placed in /etc/cron.hourly, and the $TODAY / $YESTERDAY
    # variables will need to be changed.


    TODAY=`date +"%Y%m%d"`
    YESTERDAY=`date -d "1 day ago" +"%Y%m%d"`
    OLDBACKUP=`date -d "2 days ago" +"%Y%m%d"`

    # Set the path to rsync on the remote server so it runs with sudo.
    RSYNC="/usr/bin/sudo /usr/bin/rsync"

    # This is a list of files to ignore from backups.
    EXCLUDES="/etc/servername.excludes"
    EXCLUDES="/share/yourserver.se.excludes"

    # I use a separate volume for backups. Remember that you will not be generating
    #LOG file
    LOG="/share/yourserver.se/BACKUP_success.log"

    # Remember that you will not be generating
    # backups that are particularly large (other than the initial backup), but that
    # you will be creating thousands of hardlinks on disk that will consume inodes.
    DESTINATION="/media/servername/$TODAY/"

    # I like to keep the separate volume unmounted to save me from accidentally
    # nuking backups.
    mount /media/servername
    DESTINATION="/share/yourserver.se/$TODAY/"

    SOURCE="[email protected]:/folder-you-want-to-backup"

    # Keep database backups in a separate directory.
    mkdir -p /media/servername/db
    mkdir -p /share/yourserver.se/$TODAY/db

    # This command rsync's files from the remote server to the local server.
    # Flags:
    @@ -58,24 +55,26 @@ mkdir -p /media/servername/db
    # Note the NOPASSWD option in the sudo configuration. For remote
    # authentication use a password-less SSH key only allowed read permissions by
    # the backup server's root user.
    rsync -z -e "ssh" \
    rsync -zavx -e 'ssh -p22' \
    --rsync-path="$RSYNC" \
    --archive \
    --exclude-from=$EXCLUDES \
    --numeric-ids \
    --link-dest=../$YESTERDAY rsync@servername:/ $DESTINATION

    --delete -r \
    --link-dest=../$YESTERDAY $SOURCE $DESTINATION

    # Backup all databases. I backup all databases into a single file. It might be
    # preferable to back up each database to a separate file. If you do that, I
    # suggest adding a configuration file that is looped over with a bash for()
    # loop.
    ssh rsync@servername "mysqldump \
    ssh [email protected] "mysqldump \
    --user=root \
    --password="my-super-secure-password" \
    --password="supersecretpassword" \
    --all-databases \
    --lock-tables \
    | bzip2" > /media/servername/db/$TODAY.sql.bz2
    | bzip2" > /share/yourserver.se/$TODAY/db/$TODAY.sql.bz2

    rm -R $OLDBACKUP

    # Remove this if you keep the backup directory mounted.
    umount /media/servername
    echo "BACKUP success-`date +"%Y%m%d"`" >> $LOG

    exit 0
  18. @deviantintegral deviantintegral revised this gist Apr 17, 2012. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions servername.excludes
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    /dev
    /proc
    /sys
    /media
    /mnt
    /swapfile
    /tmp
    /var/tmp
    /var/lib/mysql/
    /var/lib/varnish/servername

  19. @deviantintegral deviantintegral revised this gist Apr 17, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backup-servername.sh
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ DESTINATION="/media/servername/$TODAY/"
    mount /media/servername

    # Keep database backups in a separate directory.
    mkdir -p /media/media/servername/db
    mkdir -p /media/servername/db

    # This command rsync's files from the remote server to the local server.
    # Flags:
  20. @deviantintegral deviantintegral renamed this gist Apr 17, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  21. @deviantintegral deviantintegral created this gist Apr 17, 2012.
    81 changes: 81 additions & 0 deletions gistfile1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    #!/bin/bash

    # This is a simple backup script that uses rsync to backup files and does
    # complete mysql table dumps. Note that this script DOES NOT EXPIRE OLD BACKUPS.
    # Place it in /etc/cron.daily to execute it automatically. For hourly backups,
    # it will need to be placed in /etc/cron.hourly, and the $TODAY / $YESTERDAY
    # variables will need to be changed.

    TODAY=`date +"%Y%m%d"`
    YESTERDAY=`date -d "1 day ago" +"%Y%m%d"`

    # Set the path to rsync on the remote server so it runs with sudo.
    RSYNC="/usr/bin/sudo /usr/bin/rsync"

    # This is a list of files to ignore from backups.
    EXCLUDES="/etc/servername.excludes"

    # I use a separate volume for backups. Remember that you will not be generating
    # backups that are particularly large (other than the initial backup), but that
    # you will be creating thousands of hardlinks on disk that will consume inodes.
    DESTINATION="/media/servername/$TODAY/"

    # I like to keep the separate volume unmounted to save me from accidentally
    # nuking backups.
    mount /media/servername

    # Keep database backups in a separate directory.
    mkdir -p /media/media/servername/db

    # This command rsync's files from the remote server to the local server.
    # Flags:
    # -z enables gzip compression of the transport stream.
    # -e enables using ssh as the transport prototcol.
    # --rsync-path lets us pass the remote rsync command through sudo.
    # --archive preserves all file attributes and permissions.
    # --exclude-from points to our configuration of files and directories to skip.
    # --numeric-ids is needed if user ids don't match between the source and
    # destination servers.
    # --link-dest is a key flag. It tells the local rsync process that if the
    # file on the server is identical to the file in ../$YESTERDAY, instead
    # of transferring it create a hard link. You can use the "stat" command
    # on a file to determine the number of hard links. Note that when
    # calculating disk space, du includes disk space used for the first
    # instance of a linked file it encounters. To properly determine the disk
    # space used of a given backup, include both the backup and it's previous
    # backup in your du command.
    #
    # The "rsync" user is a special user on the remote server that has permissions
    # to run a specific rsync command. We limit it so that if the backup server is
    # compromised it can't use rsync to overwrite remote files by setting a remote
    # destination. I determined the sudo command to allow by running the backup
    # with the rsync user granted permission to use any flags for rsync, and then
    # copied the actual command run from ps auxww. With these options, under
    # Ubuntu, the sudo line is:
    #
    # rsync ALL=(ALL) NOPASSWD: /usr/bin/rsync --server --sender -logDtprze.iLsf --numeric-ids . /
    #
    # Note the NOPASSWD option in the sudo configuration. For remote
    # authentication use a password-less SSH key only allowed read permissions by
    # the backup server's root user.
    rsync -z -e "ssh" \
    --rsync-path="$RSYNC" \
    --archive \
    --exclude-from=$EXCLUDES \
    --numeric-ids \
    --link-dest=../$YESTERDAY rsync@servername:/ $DESTINATION

    # Backup all databases. I backup all databases into a single file. It might be
    # preferable to back up each database to a separate file. If you do that, I
    # suggest adding a configuration file that is looped over with a bash for()
    # loop.
    ssh rsync@servername "mysqldump \
    --user=root \
    --password="my-super-secure-password" \
    --all-databases \
    --lock-tables \
    | bzip2" > /media/servername/db/$TODAY.sql.bz2

    # Remove this if you keep the backup directory mounted.
    umount /media/servername