Forum

Results 1 to 10 of 10
  1. #1
    Join Date
    June 2011
    Location
    Baghdad, Iraq
    Posts
    367

    Using Snapshot scripts for daily backup ?

    Hello

    since i just fixed my server database and saved myself from a total remake of 74 channel and 13 group and lots of extra permissions -.-

    i want to set the Snapshot pull script to automatic daily backup

    but since am not experienced Linux shell user i need help with this

    how can i set a script to auto run the
    Code:
    ./pullsnapshot.sh serveradmin 123213123ggadsa213 voice.teamspeak-systems.de 10011 backup.txt 1
    - Run every 24 hour.
    - Set the backup.txt file name to something like backup_[date&time].txt.
    - Store the backup files in specified folder.

    so whats the best code / script to use ?

    Thanks

  2. #2
    Join Date
    May 2010
    Posts
    6,310
    Hello

    You can use this script :
    Code:
    #! /bin/sh
    
    #Path of the backup folder (absolute path)
    BACKUP_PATH=/home/florian/backup
    
    #Name of the snapshot file
    FILE_NAME=backup_`date  +%Y-%m-%d_%H-%M-%S`.txt
    
    
    ./pullsnapshot.sh serveradmin florian 192.168.1.100 10011 ${FILE_NAME} 1
    
    mv ./${FILE_NAME} ${BACKUP_PATH}/${FILE_NAME}
    About the path, you have to write the absolute path (not ./backup but /home/florian/backup)

    About the date format, for more information open the help : man date

    How to use it :
    • Put this script in the same place of the pullsnapshot.sh
    • Add the exec right : chmod 744 cronsnapshot.sh


    For test you can start it manualy : ./cronsnapshot.sh

    Now to start it evry 24 hours :
    For that you have to use the cron system (more info on google or man crontab)

    • Open the cron tab : crontab -e
    • Add this line : 0 0 * * * /home/florian/cronsnapshot.sh

    0 0 to start script at midnight (0h00) You can chage for another specific hour
    * * * for evry day, month
    Attached Files Attached Files
    Last edited by florian_fr40; August 12th, 2011 at 01:03 AM.

  3. #3
    Join Date
    June 2011
    Location
    Baghdad, Iraq
    Posts
    367
    Thanks


  4. #4
    Join Date
    June 2011
    Location
    Baghdad, Iraq
    Posts
    367
    something is wrong here .. when i test the script i get

    Code:
    [email protected]:/home/teamspeak# sh ./cronsna.sh
    : not foundh: 2:
    : not foundh: 5:
    : not foundh: 8:
    : not foundh: 9:
    [: 80: 0: unexpected operator
    [: 80: 0: unexpected operator
    [: 80: 0: unexpected operator
    [: 80: 0: unexpected operator
    [: 80: 0: unexpected operator
    [: 80: 0: unexpected operator
    [: 80: 0: unexpected operator
    snapshot.txt file being written to as we speak? Something is very wrong here...
    exit: 80: Illegal number: -9
    : not foundh: 11:
    mv: cannot move `./ts_backup_2011-08-11_18-01-42.txt\r' to `/home/teamspeak/backup\r/ts_backup_2011-08-11_18-01-42.txt\r\r': No such file or directory
    using Ubuntu server

    cant move or even delete the backup.txt files

  5. #5
    Join Date
    May 2010
    Posts
    6,310
    I remade the script. It should work normaly now.

    If you use Windows to download it before put it on Linux system, this can cause this problem.
    Use wget on linux.
    Last edited by florian_fr40; August 12th, 2011 at 12:13 PM.

  6. #6
    Join Date
    June 2011
    Location
    Baghdad, Iraq
    Posts
    367
    What i did was edited the script added "bash" before the pull....sh

    Code:
    #! /bin/sh
    
    #Path of the backup folder (absolute path)
    BACKUP_PATH=/home/teamspeak/backup
    
    #Name of the snapshot file
    FILE_NAME=backup_`date  +%Y-%m-%d_%H-%M-%S`.txt
    
    
    bash ./pullsnapshot.sh serveradmin florian 192.168.1.100 10011 ${FILE_NAME} 1
    
    mv ./${FILE_NAME} ${BACKUP_PATH}/${FILE_NAME}
    but still got errors when using crontab so fixed by adding the full path to the pullsnapshot.sh like :

    Code:
    bash /home/ahmed/backup/pullsnapshot.sh serveradmin florian 192.168.1.100 10011 ${FILE_NAME} 1
    and now it works like charm

  7. #7
    Join Date
    December 2013
    Location
    Brazil
    Posts
    23
    Dude, how can i pull the snapshot of more than 1 server? Someone can help me?

  8. #8
    Join Date
    December 2013
    Location
    Brazil
    Posts
    23
    How can i use it for all the virtual servers?

  9. #9
    Join Date
    February 2012
    Location
    Germany
    Posts
    576
    From the knowledge out of the various snapshot script demos, I made this script:

    It needs netcat (nc) for operation.

    save as /usr/bin/ts3snapshot
    Code:
    #!/bin/sh
    
    usage="\
    $0 - create or deploy a snapshot of a virtual Teamspeak 3 server instance
    
    Usage:
    $0 [-h host] [-p <port>] [-i <instance>] [-f <file>] [-u <user>] [-P <password>] [cmd]
    
    Commands:
      create        create snapshot (read from server and save to file)
      deploy        deploy snapshot (read from file and write to server)
    
    Options:
      -h <hostname> server hostname   (default: localhost)
      -p <port>     server query port (default: 10011)
      -i <instance> virtual server    (default: only on create: all instances)
      -f <file>     snapshot file     (default: ts3-snapshot-%i.txt
      -u <user>     admin query user name
      -P <password> admin query user password
    
    Notes:
    - if you provide an instance number, only this virtual server is processed.
    - if you provide a file name with -f, %i is replaced with the instance number
    - settings from /etc/teamspeak3/snapshot are used as defaults"
    
    
    # read server response from input, return 0 on success
    # return 1 and print message, if server returns error
    check_ts3_response() {
      local prefix=$1 kw=$2
    
      read -r kw id msg
      if [ "$kw" != "error" ]; then
        echo $prefix
        echo "Unexpected server response \"$kw $id $msg\""
        return 2
      fi
      [ "$id" = "id=0" ] && return 0
      echo $prefix
      msg=${msg#msg=}
      echo "Server message: \"${msg//\\s/ }\""
      return 1
    }
    
    # perform signon to Teamspeak server, return 0 on success
    # return 1 and print message, if server returns error
    check_ts3_signon() {
      local line line1
    
      if ! read -r line; then
        echo "Unable to connect to the Teamspeak Server"
        echo "host=\"$1\""
        echo "port=\"$2\""
        return 1
      fi
      if [ "$line" != "TS3" ]; then
        echo "Invalid greeting line(1), expected \"TS3\", got \"$line\""
        return 1
      fi
    
      read -r line line1
      if [ "$line" != "Welcome" ]; then
        echo "Invalid greeting line(2), expected \"Welcome to the TeamSpeak 3 ServerQuery interface[...]\", got \"$line $line1\""
        return 1
      fi
      return 0
    }
    
    # deploy a snapshot to given virtual server instance
    snapshot_deploy() {
      local _host=$1 _port=$2 _instance=$3 _user=$4 _pass=$5 _file=$6
    
      if [ ! -f $_file ]; then
        echo "Unable to find input file '$_file', aborting"
        exit 1
      fi
      echo -e "use $_instance\nlogin $_user $_pass\n serversnapshotdeploy `cat $_file`\nquit" | \
      nc $_host $_port | tr -d "\r" | {
        check_ts3_signon $_host $_port || return 1
        check_ts3_response "Unable to select server instance \"$_instance\"" || return 1
        check_ts3_response "Unable to login to the Teamspeak server" || return 1
        check_ts3_response "Unable to deploy snapshot" || return 1
      } || return 1
      return 0
    }
    
    # create a snapshot from given vitual server instance and write to file
    snapshot_create() {
      local dir line _host=$1 _port=$2 _instance=$3 _user=$4 _pass=$5 _file=$6
    
      dir=`dirname "$_file"`
      echo -e "use $_instance\nlogin $_user $_pass\nserversnapshotcreate\nquit" | \
      nc $_host $_port | tr -d "\r" | {
        check_ts3_signon $_host $_port || return 1
        check_ts3_response "Unable to select server instance \"$_instance\"" || return 1
        check_ts3_response "Unable to login to the Teamspeak server" || return 1
        read -r line
        [ -z "$dir" -o -d "$dir" ] || mkdir -p "$dir"
        echo -n "$line" > "$_file"
        check_ts3_response "Unable to get snapshot" || { rm -f "$_file"; return 1; }
      } || return 1
      return 0
    }
    
    # get a list of all virtual servers and create snapshots for each of them
    # Put a %i into the filename to avoid that the snaphots overwrite each other
    snapshot_create_all() {
      local line _host=$1 _port=$2 _user=$3 _pass=$4 _file=$5
    
      echo -e "login $_user $_pass\nserverlist -short\nquit" | \
      nc $_host $_port | tr -d "\r" | {
        check_ts3_signon $_host $_port || return 1
        check_ts3_response "Unable to login to the Teamspeak server" || return 1
        read -r line
        check_ts3_response "Unable to get server instance list" || return 1
    
        for i in `echo "$line"|xargs -n 1 -d "|"|grep "virtualserver_status=online"|sed -re 's/virtualserver_id=([0-9]+).*/\1/g'`; do
          if ! snapshot_create "$_host" "$_port" "$i" "$_user" "$_pass" "${_file//\%i/$i}"; then
            echo "Snapshot for server instance $i failed."
          fi
        done
      } || return 1
      return 0
    }
    
    if [ ! -x /usr/bin/nc ]; then
      echo "/usr/bin/nc not found - please install NetCat!"
      exit 1
    fi
    
    [ -f /etc/teamspeak3/snapshot ] && . /etc/teamspeak3/snapshot
    
    while getopts "f:h:i:p:u:P:" key ; do
      case "$key" in
        f) file="$OPTARG" ;;
        h) host="$OPTARG" ;;
        i) instance="$OPTARG" ;;
        p) port="$OPTARG" ;;
        u) user="$OPTARG" ;;
        P) pass="$OPTARG" ;;
        *) exec echo "$usage" ;;
      esac
    done
    shift $(( $OPTIND -1 ))
    cmd=${1:-}
    host=${host:-localhost}
    port=${port:-10011}
    file=${file:-ts3-snapshot-%i.txt}
    
    if [ -z "$user" -o -z "$pass" ]; then
      echo -e "error: no username or password given!\n"
      exec echo "$usage"
    fi
    
    case "$cmd" in
      deploy)
        if [ -z "$instance" ]; then
          echo -e "deploy requires an instance number\n"
          exec echo "$usage"
        fi
        snapshot_deploy "$host" "$port" "$instance" "$user" "$pass" "${file//\%i/$instance}"
        ;;
      create)
        if [ -z "$instance" ]; then
          snapshot_create_all "$host" "$port" "$user" "$pass" "$file"
        else
          snapshot_create "$host" "$port" "$instance" "$user" "$pass" "${file//\%i/$instance}"
        fi
        ;;
      *)
        echo -e "Unknown command \"$cmd\"\n"
        exec echo "$usage"
        ;;
    esac
    It reads defaults from /etc/teamspeak/snapshot, which looks like this:

    save as /etc/teamspeak/snapshot
    Code:
    user="serveradmin"
    pass="xxxxxxxxxx"
    file="/var/lib/teamspeak3/snapshots/%i/ts3-snapshot-%i-`date +%Y%m%d-%H%M%S`.txt"
    Adjust the query admin password.

    If used with this file, you don't need to hardcode the credentials in some script. Set the permissions accordingly, so only root and the teamspeak3 user can access it:

    $ chown root:teamspeak3 /etc/teamspeak/snapshot
    $ chmod 640 /etc/teamspeak/snapshot

    (teamspeak3 is the user my TS3 server is running under)

    From cron, I call it this way:

    save as /usr/bin/ts3backup_cron
    Code:
    #!/bin/sh
    
    umask 0027
    
    # remove snapshots older than 60 days
    find /var/lib/teamspeak3/snapshots -type f -mtime +60 -exec rm -f {} \;
    
    # create new snapshots of all virtual servers
    ts3snapshot create
    
    # stop here, if sqlite database not in use
    [ -f /var/lib/teamspeak3/ts3server.sqlitedb ] || exit 0
    
    # remove database backups older than 60 days
    find /var/lib/teamspeak3/backups -type f -mtime +60 -exec rm -f {} \;
    
    # perform a clean copy of the sqlite database
    sqlite3 /var/lib/teamspeak3/ts3server.sqlitedb ".backup \
      /var/lib/teamspeak3/backups/ts3server.sqlitedb.`date +%Y%m%d-%H%M%S`"
    This makes 2 backups in fact, one Teamspeak snapshot backup and one sqlite3 backup for the whole database. You may have to adjust the paths in the script.


    The crontab for this:

    Code:
    MAILTO=root
    # scheduled backups of the Teamspeak 3 server database
    #
    45 3 * * * teamspeak3 /usr/bin/ts3backup_cron

  10. #10
    Join Date
    May 2006
    Location
    Europe/Czech Rep.
    Posts
    1,616
    One might mention flaws of snapshot system. I haven't look into possible workaround for the hash.
    - snapshot string contain hash and decrease of maxclients makes snapshot un-usable. That's not handy at all.
    - files/virtualserver_*/icons aka group&channel icons are not included in so called snapshot.
    - this is understandable but some might want to take this into account, entire filetransfer(files/virtualserver_*/channel_*) for each channel is therefore not included as well.

    Just to hint people that might not test their backups until they need them ;-)
    Therefore copy of sqlite file isn't gonna cut it for full backup, and server snapshot is same thing.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Snapshot helper scripts
    By Peter in forum Linux / FreeBSD
    Replies: 50
    Last Post: November 11th, 2019, 10:39 AM
  2. Permissions backup w/o snapshot
    By lesauvage in forum Server Support
    Replies: 3
    Last Post: November 14th, 2013, 10:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •