# Back up with rdiff

> Use the rdiff tool for backup operations.

Use `rdiff` on a Linux machine to perform both cold copy backups and hot copy backups.

`rdiff` uses a `dst tmp aux` folder to store the full backup. At the beginning of each week, `rdiff` resets the folder and creates a new backup. Each following day of the week, `rdiff` uses `rdiff-backup` to add incremental backups. Every day, the contents of the `dst tmp aux` folder is compressed into a `tar.gz` file and stored in a `week_xx` folder, which replaces any older `tar.gz` files in that folder.

## Commands

### Create backups

Create a backup. If you reuse the target path, you create an incremental backup.

```text
sudo rdiff-backup "/media/codice/New Volume/jet_ubuntu" "/media/codice/New Volume/rdiff_backups/"
```

### List backups

List the incremental backups stored that you can restore them later:

```text
sudo rdiff-backup --list-increments "/media/codice/New Volume/rdiff_backups/rep_4"
[sudo] password for codice:
Found 3 increments:
rep_4.2017-01-16T08:28:58-08:00.dir Mon Jan 16 08:28:58 2017
rep_4.2017-01-16T09:34:15-08:00.dir Mon Jan 16 09:34:15 2017
rep_4.2017-01-17T01:54:14-08:00.dir Tue Jan 17 01:54:14 2017

Current mirror: Tue Jan 17 02:01:13 2017
```

### Restore an incremental backup

The following command restores the last backup prior to 8 hours and 30 minutes ago. The example uses the `--tempdir` path to avoid using the system temp path and running out of space on the disk.

```text
sudo rdiff-backup -r 8h30m --force --tempdir "/media/codice/New Volume/tmp_path" "/media/codice/New
Volume/rdiff_backups/rep_4" "/media/codice/New Volume/jet_ubuntu/rep_4"
```

## Cold copy example

This example shows the setup for a weekly and incremental backup using `rdiff-backup` on a Linux machine:

1. Install `rdiff-backup 1.2.8` in your server.
2. Configure `autofs` to auto mount backup `NFS` folder in the `/cifs/backup` folder.
3. Add a cron job that executes everyday at 3:30 AM local time:

```bash
#!/bin/bash
export weekNumber='date +%V'
export year='date +%Y'
export today='date +%Y%m%d'
export lastActinLogFile="/root/backup_jet.log"

export dirName="week_$weekNumber"
export baseBackupDstLocation="/cifs/backup/backups/myserver_jet"
export backupFinalDstLocation="$baseBackupDstLocation/$year/$dirName"
export weekIncrementsTarGzDstFile=$backupFinalDstLocation/myserver_ddbb_jetfs.rdiff-backup_$year_week_$weekNumber.tar.gz

#rdiff-backup source and destination params
export srcJetFolder="/home/mydata/jet"
#export rdiffBackupWeekIncrementsAuxDir="/home/mydata/local_jet_backup_rsync"
export rdiffBackupWeekIncrementsAuxDir="$baseBackupDstLocation/tmp"

echo "starting daily backup $today on dst dir: $backupFinalDstLocation" > $lastActionLogFile

#when a new week starts, reset the increments dst dir so rdiff-backup starts a fresh backup from scratch
if [ ! -d "$backupFinalDstLocation" ]; then
  mkdir -p $backupFinalDstLocation
  #reset weekly backup local folder
  rm -rf $rdiffBackupWeekIncrementsAuxDir
fi

echo "about to stop and targz $today" > $lastActionLogFile
/usr/sbin/plasticsd stop
sleep 20
rdiff-backup $srcJetFolder $rdiffBackupWeekIncrementsAuxDir
rm $weekIncrementsTarGzDstFile
tar cvzf $weekIncrementsTarGzDstFile  $rdiffBackupWeekIncrementsAuxDir
sleep 20
/usr/sbin/plasticsd start
echo "done! $today" > $lastActionLogFile
```

## Hot copy example

As this is a hot copy, you need to switch the server between normal and read-only modes ( `cm admin readonly` ).

1. Install `rdiff-backup 1.2.8` in your server.
2. Configure `autofs` to automount backup `NFS` folder in the `/cifs/backup` folder.
3. Add a cron job:

```bash
#!/bin/bash
export weekNumber='date +%V'
export year='date +%Y'
export today='date +%Y%m%d'
export lastActinLogFile="/root/backup_jet.log"

export dirName="week_$weekNumber"
export baseBackupDstLocation="/cifs/backup/backups/myserver_jet"
export backupFinalDstLocation="$baseBackupDstLocation/$year/$dirName"
export weekIncrementsTarGzDstFile=$backupFinalDstLocation/myserver_ddbb_jetfs.rdiff-backup_$year_week_$weekNumber.tar.gz

#rdiff-backup source and destination params
export srcJetFolder="/home/mydata/jet"
export rdiffBackupWeekIncrementsAuxDir="$baseBackupDstLocation/tmp"

echo "starting daily backup $today on dst dir: $backupFinalDstLocation" > $lastActionLogFile

#when a new week starts, reset the increments dst dir so rdiff-backup starts a fresh backup from scratch
if [ ! -d "$backupFinalDstLocation" ]; then
  mkdir -p $backupFinalDstLocation
  #reset weekly backup local folder
  rm -rf $rdiffBackupWeekIncrementsAuxDir
fi

echo "about to stop and targz $today" > $lastActionLogFile
cm admin readonly enter
sleep 20
rdiff-backup $srcJetFolder $rdiffBackupWeekIncrementsAuxDir
rm $weekIncrementsTarGzDstFile
tar cvzf $weekIncrementsTarGzDstFile  $rdiffBackupWeekIncrementsAuxDir
sleep 20
cm admin readonly leave
echo "done! $today" > $lastActionLogFile
```
