About Me

My photo
I know the last digit of PI

Monday, December 25, 2017

Backup samba shares from raspbery pi box

Here is a simple script that backups samba shares from linux box.
  1. Create backup script

    Create backup script and save it as /root/backup.sh
    #!/bin/bash
    
    # Gets the latest date and time
    DATE=`date '+%Y%m%dT%H%M%S'`
    
    # The root directory that contains the backups
    BACKUP_ROOT=/home/thexman/backup
    
    # Current backup directory
    BACKUP_DIR=$BACKUP_ROOT/$DATE
    
    # Current zip file
    BACKUP_ZIP=$BACKUP_ROOT/$DATE.zip
    
    echo "Creating backup directory $BACKUP_DIR"
    
    #create backup directory if it doesn't exists
    mkdir -p "$BACKUP_DIR"
    
    echo "Mounting samba share..."
    
    # Mount windows shares to /mnt
    mount -t cifs //192.168.99.93/c$ -o "username=administrator,password=1234" /mnt
    
    echo "Creating backup copies..."
    
    # Copy files from windows machine to backup directory
    rsync -rzvh "/mnt/temp" "$BACKUP_DIR"
    #rsync "/mnt/source_directory1" $BACKUP_DIR
    #rsync "/mnt/source_directory2" $BACKUP_DIR
    #rsync "/mnt/source_directory2" $BACKUP_DIR
    
    echo "Creating zip archive $BACKUP_ZIP"
    
    # Archive the backup directory to zip file
    cd "$BACKUP_ROOT" && zip -r "$BACKUP_ZIP" "$DATE"
    
    echo "Removing backup directory $BACKUP_DIR"
    
    # Remove the backup directory
    rm -rf "$BACKUP_DIR"
    
    echo "Umounting samba shares..."
    
    # Release windows shares
    umount /mnt
    
  2. Schedule backup script

    Execute following command "sudo crontab -e" and append following at the end of the file (it will execute the script at 21:15 every day)
    # m h  dom mon dow   command
    #15 21 * * * /root/backup.sh