Tuesday, December 13, 2016

cron automation

Use cron to setup automated log file removal or other periodic tasks in linux environment is very useful. Here are the steps to automate that.

1. Run the following command to export the current crontab:
 
crontab -u john -l > john-cron-backup.txt

2. Edit the exported john-cron-backup.txt file to your need.

3. Run the following command to restore from the edited file to make it taking effects:
    
crontab -u john john-cron-backup.txt
 
Here is an example backup file: 

MAILTO=""
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin

# m h  dom mon dow   command
0 1 * * 1 rm -r -f /var/log/**/*.gz
0 1 * * 1 rm -r -f /var/log/**/*.log.1
0 1 * * 1 rm -r -f /var/log/*.gz
 
    
 

Tuesday, December 6, 2016

How to backup and restore alive Ubuntu server

Backup and restore alive Ubuntu system is useful for developers who maintain and install systems all the time, the follow procedure should help when your system is using lvm file system.

Backup the live Ubuntu system by following the below steps:

Use df -h command to figure out what is the logical volume where the root file system is on. Also look into /dev directory for the logical volumes. In the example below, the logical volume that holds the entire system is /dev/vg00/vg00-lv01

Only do this once when the system is clean:
1. Create a logical volume from a volume group (vg01) which will store the backup:
           lvcreate -L 40G -n space vg01
2. Create the file system on the new logical volume:
           mkfs -t ext4 /dev/vg01/space
3. Create snapshot of the root logical volume:
           lvcreate --size 6G -s -n cleansystem /dev/vg00/vg00-lv01
4. Create a directory to mount the logical volume:
           mkdir /space
           mount /dev/vg01/space /space           mkdir -p /space/snap /space/backup
5. Mount snapshot and save everything
          mount /dev/vg00/cleansystem /space/snap
          cd /space/snap
          tar -pczvf /space/backup/cleansystem.tar.gz *
          umount /space/snap
          lvremove /dev/vg00/cleansystem

After these steps, a tar.gz file named cleansystem.tar.gz should be produced in /space/backup directory. This is the file should be kept for restore later.

Restore the Ubuntu sytem by following the below steps:

The follow steps are to recover the system from the saved tar.gz in /space/backup directory, assume that the logical volume which contains the backup tar.gz file has been mounted on /space.

lvcreate —size 20G -s -n resetpoint /dev/vg00/vg00-lv01

mount /dev/vg00/resetpoint /space/snap

cd /space/snap

rm -r -f *

tar -xvf /space/backup/cleansystem.tar.gz

umount /space/snap

lvconvert —merge /dev/vg00/resetpoint