Full Script Bash Add Volumes to Directadmin in Digitalocean
Version 1:
#!/bin/bash
if [[ "$1" == "get" ]];then
if [ -z "$2" ];then
echo "if need format using get + name"
ls /dev/disk/by-id/
exit
else
if [[ "$2" == "name" ]];then
ls /dev/disk/by-id/
exit
else
sudo mkfs.ext4 /dev/disk/by-id/$2
#install if not yet
rpm -qa | grep rsync
sudo apt-get install alien
sudo alien -i package_file.rpm
apt-get -y install rsync
exit
fi
fi
else
echo "..."
fi
if [[ "$1" == "add" ]]
then
if [[ "$2" == "home" ]];then
if [ -z "$3" ]
then
note="require name volume"
ls /dev/disk/by-id/
echo $note
exit
else
mkdir -p /clone/disk/home
rsync -av /home/* /clone/disk/home/
mount /dev/vda1 /clone/disk/
mount -o discard,defaults,noatime /dev/disk/by-id/$3 /home
echo '/dev/disk/by-id/$3 /home ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstab
rsync -av /clone/disk/home/* /home/
echo "success home"
fi
elif [[ "$2" == "mysql" ]];then
if [ -z "$3" ];then
note="require name volume"
ls /dev/disk/by-id/
echo $note
exit 1
else
mkdir -p /clone/disk/backup_mysql
rsync -av /var/lib/* /clone/disk/backup_mysql/
mount -o discard,defaults,noatime /dev/disk/by-id/$3 /var/lib/mysql
echo '/dev/disk/by-id/$3 /var/lib/mysql ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstab
rsync -av /clone/disk/backup_mysql/* /var/lib/
service mysqld restart
echo "success mysql"
fi
else
note="require choose home or mysql"
ls /dev/disk/by-id/
echo $note
exit 1
fi
else
note="require choose home or mysql"
ls /dev/disk/by-id/
echo $note
exit 1
fi
Ver 2:
#!/bin/bash
if [[ "$1" == "retore" ]];then
if [[ "$2" == "home" ]];then
if [ -z "$3" ]
then
note="require name volume"
ls /dev/disk/by-id/
echo $note
exit
else
mkdir -p /clone/disk/home
#rsync -av /home/* /clone/disk/home/
mount /dev/vda1 /clone/disk/
mount -o discard,defaults,noatime /dev/disk/by-id/$3 /home
echo '/dev/disk/by-id/'$3' /home ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstab
#rsync -av /clone/disk/home/* /home/
echo "success home"
fi
elif [[ "$2" == "mysql" ]];then
if [ -z "$3" ];then
note="require name volume"
ls /dev/disk/by-id/
echo $note
exit 1
else
mkdir -p /clone/disk/backup_mysql
#rsync -av /var/lib/* /clone/disk/backup_mysql/
mount -o discard,defaults,noatime /dev/disk/by-id/$3 /var/lib/mysql
echo '/dev/disk/by-id/'$3' /var/lib/mysql ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstab
#rsync -av /clone/disk/backup_mysql/* /var/lib/
service mysqld restart
echo "success mysql"
fi
else
note="require choose home or mysql"
ls /dev/disk/by-id/
echo $note
exit 1
fi
else
note="require choose home or mysql"
ls /dev/disk/by-id/
echo $note
exit 1
fi
if [[ "$1" == "get" ]];then
if [ -z "$2" ];then
echo "if need format using get + name"
ls /dev/disk/by-id/
exit
else
if [[ "$2" == "name" ]];then
ls /dev/disk/by-id/
exit
else
sudo mkfs.ext4 /dev/disk/by-id/$2
#install if not yet
rpm -qa | grep rsync
sudo apt-get install alien
sudo alien -i package_file.rpm
apt-get -y install rsync
exit
fi
fi
else
echo "..."
fi
if [[ "$1" == "add" ]]
then
if [[ "$2" == "home" ]];then
if [ -z "$3" ]
then
note="require name volume"
ls /dev/disk/by-id/
echo $note
exit
else
mkdir -p /clone/disk/home
rsync -av /home/* /clone/disk/home/
mount /dev/vda1 /clone/disk/
mount -o discard,defaults,noatime /dev/disk/by-id/$3 /home
echo '/dev/disk/by-id/'$3' /home ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstab
rsync -av /clone/disk/home/* /home/
echo "success home"
fi
elif [[ "$2" == "mysql" ]];then
if [ -z "$3" ];then
note="require name volume"
ls /dev/disk/by-id/
echo $note
exit 1
else
mkdir -p /clone/disk/backup_mysql
rsync -av /var/lib/* /clone/disk/backup_mysql/
mount -o discard,defaults,noatime /dev/disk/by-id/$3 /var/lib/mysql
echo '/dev/disk/by-id/'$3' /var/lib/mysql ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstab
rsync -av /clone/disk/backup_mysql/* /var/lib/
service mysqld restart
echo "success mysql"
fi
else
note="require choose home or mysql"
ls /dev/disk/by-id/
echo $note
exit 1
fi
else
note="require choose home or mysql"
ls /dev/disk/by-id/
echo $note
exit 1
fi